How to check if a record is new in Laravel?

后端 未结 5 1885
星月不相逢
星月不相逢 2020-12-30 19:11

I recently started using Eloquent.

When I used PHP Active Record, there was a nice function that checked if a record was loaded from the database or is a new instan

5条回答
  •  滥情空心
    2020-12-30 19:18

    $article = new Article;
    var_dump($article->id); == null
    
    $article = Article::find(1);
    var_dump($article->id); == string(1) "1"
    

    so

    if ($article->id) {
         // I am existing
    } else {
        // I am new
    }
    

提交回复
热议问题