@davy_yg Laravel has a naming convention when it comes to models, tables, foreign key and a few other things.
- Models should always be in singular form. So if you want to store information about cars, then you call that model the singular form of the word cars. So the model becomes Car. Notice the capital
C models alway start with a capital letter, Car, Book, Movie and so on.
- Database tables are always named in plural form of the model name. As in the example above, the Car model would have a table named
cars. All table names are in lower case letters. cars, books, movies and so on.
- When it comes to a many-to-many relations then the naming convention for the pivot table is the combination of the model names all in lower case letters and in alphabetical order separated by an underscore , car_user, book_user, movie_user and so on.
- If a table name need two words like
blog post then the model becomes BlogPost and the table name becomes blog_posts.
- Foreign keys should always be named the table it references in singular form followed by an underscore and the column if references, as in your example
user_id.
Now back to your questions, if you for some reason don't follow the naming conventions you can pass the custom column name as an argument. I highly recommend sticking to the naming convention that Laravel uses.
Do you need to use ->constrained(), I'd say yes to that. That makes it impossible to add a foreign key into your table that doesn't exist in the table the foreign key is referencing. It also gives you the possibility to use cascades to delete the records that has the foreign key that is deleted in the referencing table.
Let's say we have a blog post and it has a foreign key user_id with on delete cascade set, and the user is deleted from the users table then the constraint would kick in and the blog post would be deleted as well. If you don't add any cascade and you try to delete the user the database will give you an error stating that you have an integrity constraint violation and that it has child records that needs to be deleted first.
Limiting the user_id foreign key is not necessary and I would say a bad practice. I suggest you stick to the defaults even if you don't need to use unsigned big integers in your primary and foreign keys.
If you really need change make sure that you use unsigned integers, that you primary key has auto increment.