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!
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>
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:
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!