Home> PHP Framework> Laravel> body text

Finally know the importance of specifying table names in Laravel

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

The followingLaravelframework 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 creatingModel, the specific process for encountering problems is as follows:

After entering the following command in Terminal, acustomer.phpfile

php artisan make:model Customer
Copy after login

will be created in the

Appdirectory. 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:


        
Copy after login

As above, we addedprotected $table = 'customer';, force the database corresponding tocustomer.phpto be designated ascustomerinstead of the system defaultcustomers. 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 everyModel.

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!