Home  >  Article  >  PHP Framework  >  Finally know the importance of specifying table names in Laravel

Finally know the importance of specifying table names in Laravel

藏色散人
藏色散人forward
2020-12-04 17:11:3611806browse

The following Laravel framework tutorial column will introduce to you the importance of specifying table names in Laravel. I hope it will be helpful to friends in need!

Finally know the importance of specifying table names in Laravel

Since Laravel will automatically associate the corresponding table name when creating Model, the specific process for encountering problems is as follows:

After entering the following command in Terminal, a customer.php file <pre class="p1 prettyprint" style="overflow:hidden; font-family:"courier new"; padding:10px 15px; margin-top:20px; margin-bottom:20px; line-height:20px; color:rgb(248,248,212); word-break:break-all; word-wrap:break-word; background:rgb(39,40,34); border:none">php artisan make:model Customer</pre>

will be created in the

App directory. However, it needs to be added to the database by default. The table is customers instead of customers, which means that the system will automatically add the plural "s" according to the name of the Model. In general, there is no problem here, but if you encounter things such as person changing to people or various things that our Chinese people cannot easily distinguish. Form, so affordable will cause problems for our development; in addition, it is also possible that we do not want the system to automatically match the database, but to customize the table name.

What we need to do is actually very simple. Add a rule specifying the table name in the created Model file function:

<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class Customer extends Model{

 protected $table = &#39;customer&#39;;

}

As above, we added protected $table = 'customer';, force the database corresponding to customer.php to be designated as customer instead of the system default customers. We can also see this in the User.php that comes with the framework. In order for the program to run stably and without errors, this step should be applied in every Model.

If you encounter a problem, simply record it...

The above is the detailed content of Finally know the importance of specifying table names in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete