Home > PHP Framework > Laravel > body text

Finally know the importance of specifying table names in Laravel

藏色散人
Release: 2020-12-31 09:04:28
forward
11819 people have browsed it

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 <div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div>

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;;

}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template