Example analysis of creating and using laravel 5.1 framework model

Time:2021-10-19

This paper describes the creation and use of laravel 5.1 framework model. Share with you for your reference, as follows:

Laravel’s model also accesses the database. It is more object-oriented. A model corresponds to a table. We can use the model to add, delete, modify and query the data.

1 create model

You can create models using the artisan console:


php artisan make:model Article

Generally, I prefer to generate it together with migration:


php artisan make:model Article -m

↑ after executing the artisan command above, we can find the model we just created under / APP and generate the migration.

2 common attributes of model

2.1 user defined table name

A model corresponds to a table. By default, the table name corresponding to the model is the plural of the model name, that is, article (model) corresponds to articles (table), and user (model) corresponds to users (table).

Of course, if your model does not want to use this default method, you can also customize it:


class Article extends Model
{
  protected $table = 'K_Articles';
}

2.2 user defined primary key

The primary key of the model is ID by default. You can also customize it:


class Article extends Model
{
  protected $primaryKey = 'article_id';
}

2.3 timestamp

There is create in our data table_ At and update_ At two timestamps are automatically managed by laravel. If you don’t want to automatically manage these two columns, you can do this:


class Article extends Model
{
  public $timestamps = false;
}

2.4 white list and blacklist

When we use batch creation and batch modification, we must declare the blacklist or whitelist. The attributes in the whitelist can be filled, and the attributes in the blacklist cannot be filled:


class Article extends Model
{
  protected $fillable = ['title', 'body'];
}

Note: it doesn’t matter if you don’t know how to use it. In 3.2, we will say batch creation.

3 create data (model)

There are two methods to create models. The first is ordinary creation, and the second is batch creation.

3.1 creating a common model

Normal creation is to create a new model:


  public function getCreateArticle()
  {
    $article = new Article();
    $article->title = 'One';
    $article->body = 'Learn Laravel Model';
    $article->save();
  }

3.2 batch creation

Batch assignment is to use the Create method to generate a model and return the newly inserted model. Before you use create, you must check whether the blacklist or whitelist is declared in the model:


class Article extends Model
{
  protected $fillable = ['title', 'body'];
}

↑ this is our white list


  public function getCreateArticle()
  {
    $article = Article::create(['title'=>'Two', 'body'=>'Test Create Model', 'comment_count'=>10]);
  }

↑ this is a batch creation model. There is a detail. Ha, we didn’t add comment in fillable_ Count, but the array in the Create method has comment_ The key value of count… What’s the result_ Count is not populated because it is not on the white list or it is on the blacklist.

4 update

Update is very similar to insert. There are two methods: ordinary and batch.

4.1 General update


  public function getUpdateArticle()
  {
    $article = Article::find(1);
    $article->title = 'Article_One';
    $article->save();
  }

4.2 batch update


  public function getUpdateArticle()
  {
    $article = Article::find(3);
    $article->update(['title' => 'Three', 'body' => 'Third']);
  }

5 find

In fact, the search of model is very similar to the builder we have learned before.

5.1 obtain all models


  public function getIndex()
  {
    $articles = Article::all();
    foreach ($articles as $article) {
      echo $article->title . '<br />';
      echo $article->body . '<br />';
    }
  }

5.2 obtaining a single model

Use find to find through the primary key:


  public function getIndex()
  {
    $article = Article::find(1);
    dd($article->body);
  }

We can also judge whether the model is obtained:


  public function getIndex()
  {
    $article = Article::find(2);
    if (!isset($article)){
      abort(404,'Not Found');
    }
    dd($article);
  }

When an error is automatically reported when no value is obtained, findorfail can also be used to deal with this scenario:


  public function getIndex()
  {
    $article = Article::findOrFail(2);
    dd($article);
  }

5.3 model acquisition by constraints

This is similar to the builder. You can constrain it like a constraint builder. I won’t say more here. Let’s give an example:


  public function getIndex()
  {
    $article = Article::where('id','>',2)->orderBy('comment_count','desc')->lists('title')->toArray();
    dd($article);
  }

Let’s write so much first. The next article will talk about the deletion function of model.

For more information about laravel, readers who are interested can see the special topics on this site: introduction and advanced tutorial of laravel framework, summary of PHP excellent development framework, introduction to PHP object-oriented programming, introduction to PHP + MySQL database operation, and summary of common database operation skills for PHP

I hope this article will be helpful to your PHP Programming Based on laravel framework.