I want to naming tables in my project, I have a table for tour services.
What is the best naming method for table and model? camelCase, snake_case or PascalCase (upper camelCase)?
is it right for table 'tourServices' and for model 'TourService'?
For table names: Snake Case and the name in plural (if pivot table you could use the singular version of each model and order it alphabetically):
'tour_services' // regular table
'users' // regular table
'roles' // regular table
'role_user' // <-- pivot table
For model names: Pascal Case (also in singular)
'TourService'
'User'
'Role'
Check this other answer that touches this subject. Also, check this other post that talks about case styles.
You can name the model and let Laravel name the table for you. Tables are mostly named using snake_case, and plural from the model, but you can just run this, and Laravel will do the rest:
php artisan make:model TourService -m
The -m flag creates a migration with the name of the table based on the given model.
But then again, I am not sure that I will go with a Service name in the model, maybe just a Tour is a good name for the model, I don't know what is the context of the model.
Related
I am a newbie at Laravel framework and trying to work me in.
I already understand how to generate N:M relationships and handle them inside the models. Now I am asking you how to fill an additional field inside the many to many tables?
For example:
Table Foo
Table User_Foo
user_id
foo_id
is_owner (bool)
Table User
Now I want to declare which of the foo users is the real owner.
In my opinion, the N:M Table has stored this information an not the Foo itself.
So how is it possible to declare those additional fields inside of my Foo and User model?
Retrieve additional fields you can with withPivot() method
return $this->belongsToMany('App\User')->withPivot('is_owner');
Fill you can with sync() or attach() methods.
Laravel relations doc
Laravel provides us with concept of pivotwhen defining N-M relationships. By default the table will have the both connected keys. But if you want to add extra fields in that bridge table.
$model->belongsToMany('Model')->withPivot('column1', 'column2');
In above case, your pivot table will have two additional columns and you can access these columns as:
$model->pivot->column1
$model->pivot->column2
If I have database table suggestions_votes, what would be the correct name of Laravel (5.1) Class (SuggestionsVote or SuggestionVote)?
Table was created by migration, using
Schema::create('suggestions_votes', ...
Laravel recommends certain conventions, but they also provide you with options to override them.
If your model is "SuggestionVote", then the table associated with that model will be the snake case plural name of the class. In other words, it would look for the table "suggestion_votes". If you want to override the associated table name, you can add this property to your model:
protected $table = 'suggestions_votes';
If you are actually creating a pivot table for the models "Suggestion" and "Vote", then Laravel will by convention join the two related model names in alphabetical order. In other words, it will look for the pivot table "suggestion_vote". You can override this though when you define the relationship. For example:
return $this->belongsToMany('App\Suggestion', 'suggestions_votes');
Where 'App\Suggestion' would be fully namespaced path to your Suggestion class.
It depends. You can make any name work.
If you have no control of the database, the model 'should' be SuggestionsVote
If you do have control over the database, I would rename the table to suggestion_votes and the model name would be SuggestionVote
I would like to use Laravel 5.0 many to many relationship and as we know the naming convention is alphabetically joining the two table names... But what if one of the tables is a two word name itself?
For example we have "sub_categories" and "products" tables.
Should the pivot table name be: "product_sub_category"?
And yes I know that we can specify the table name as the second argument when we specify our relationship in our models: $this->belongsToMany('App\Product', 'table_pivot');
But I wanna know if there's a naming convention for this too! So that we can simply leave the second argument and follow the standard conventions...
You can name as you want, and specify the table name as the 2nd param on the relationship call in your models:
return $this->belongsToMany('Model', 'table_name');
Official Documentation
I just used Artisan CLI to make a migration for a model called story:
php artisan make:model Story
And it created a migration file that creates a table called stories and not storys. Even though it is grammatically correct, it makes me wonder what other non-conventional corrections it can make. In other words, what are rules that CLI follows to create a migration file? Also, do these "correct" names apply to column names or not? Will the migration table for a polymorphic tags table be taggable_id or tagable_id? Bear in mind that Eloquent doesn't expect a taggable_id by default.
here is exactly your question you can find out why in this link .
https://laracasts.com/discuss/channels/general-discussion/makemodel-also-creates-a-migration
Laravel follows simple naming convention. table name should be plural to the model name. But if you want to specify the table name you add this property in the model
protected $table = 'myclients'
According to this Eloquent has its own conventions. For example a Travel model retrieve and store information from our travels database table.
But the important point is that Laravel Migration doesn't force us to choose table name that it wants!
The "snake case", plural name of the class will be used as the table
name unless another name is explicitly specified.
So , U can easily change the name of referenced table to whatever u want.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Travel extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'whatever_you_want';
}
I'm just wondering why the schema builder in laravel automatically convert all camel case to lower case in table naming
E.g
Schema::create('myTable', function(Blueprint $table)
{
....
});
It creates tablename: mytable
Why is that? Is that a convention of laravel? I don't see it in the laravel docs Schema Builder page.
Thanks
It's a common practice to use snake case in table names and field names as well and it's not only related to laravel but most people follow this convention. In Laravel's old (4x) documentation, it's been mentioned that:
Note that we did not tell Eloquent which table to use for our User
model. The lower-case, plural name of the class will be used as the
table name unless another name is explicitly specified. So, in this
case, Eloquent will assume the User model stores records in the users
table. You may specify a custom table by defining a table property on
your model:
class User extends Eloquent {
protected $table = 'my_users';
}
So, yes, Laravel uses strtolower function in many places and probably this is better to follow the common convention and it's (my_table) known as snake case.