I just created a model called GrupoInstructor, the table's name is grupoinstructor
When I want to create a query for that model it shows "base table grupo_instructors doesn't exist". Why is that? why laravel adds a _ to the table's name?
Is there a way to tell lavarel that my table's name is grupoinstructor? I just realized that have some errors in my previous migrations so my last one doesn't rollback...
Thanks in advance.
In your model you can override the name of your table to suit your needs
protected $table = 'grupoinstructor';
Laravel and most of the frameworks assume that capital letters are used for separation hence the reason why it expects your table name to be called grupo_instructor which in my mind is more readable way anyway instead of concatenating the words together.
Related
first image
second image
I want to get Id from students table where i want insert this id in differs table . I don't really know how to explain it but maybe if you see my codes, you would understand. I have already finish making the relationship between this 2 tables already
my student controller
this my differ controller but I do not know any idea about if this code true or no
first: 'app\Differ'? but Differ is in App\Models
you can use hasOne(Differ::class); when Student model and differ in the same folder
the second thing you should know, when using functions of relationship in laravel such as hasOne, a foreign key should be named by student_id in Differ or
you show it in function like hasOne(Differ::class,'id_student','id')
I'm currently watching the Laravel Tutorial Episode 7
https://laracasts.com/series/laravel-from-scratch-2017/episodes/7
I created the database and populated its data on the previous episode,
it is only this time, the tutorial introduces model, upon creating the model name "Task" via php artisan make:model Task, it automatically connects to my table "tasks" without any clue how it happened.
So how did a freshly out of the box model knows it?
It's general definition standard.
If you create a table with plural name and create model for this table as singular, then model automatically connects corresponding table, otherwise you should define table name in model like:
protected $table = 'task_table';
It's a convention Laravel follows. Laravel will search for a table that is the snake cased Model's name in plural unless you indicate another table.
If you generate the model with the migration flag (e.g. php artisan make:model Task --migration), it will also create a table with the model's name in plural automatically (in this case, Tasks).
You can check more about it in the documentation.
Table Names
Note that we did not tell Eloquent which table to use for our Flight
model. By convention, the "snake 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 Flight model stores records
in the flights table. You may specify a custom table by defining a
table property on your model (...)
In our app few of our Eloquent models need only a specific columns always. What is the right way to have my method? As of now there is a new method written which does,
DB::table('MyTable')->selectRaw('required_column_list')
I feel it is odd since the table name is hard-coded. What is the right way to do?
If you don't want to hard-code table name, so you have to hard code ModelName?
Use model name with getTable function
DB::table(YourModel::create()->getTable());
checklist = model("user_checklist").findAll(include="tb_machine_checklist");
Above is the query I am using to fetch records from a table called user_checklist and it is related to table called tb_machine_checklist.
Now there is a table called "tb_machine_checklist", however it it gives me an error saying;
The "tb_machine_checklists" table could not be found in the database.
Why is the "s" being added when I didn't specify?
Be sure to read the chapter in the documentation about Conventions.
CFWheels adopts a Rails-style naming conventions for database tables and model files. Think of a database table as a collection of model objects; therefore, it is named with a plural name. Think of a model object as a representation of a single record from the database table; therefore, it is named with a singular word.
Fortunately, CFWheels lets you override most conventions with a little bit of extra code.
In your tb_machine_checklist model's init method, add this line of code:
<cffunction name="init">
<cfset table("tb_machine_checklist")>
</cffunction>
Then any queries that CFWheels writes for you will use your singular table name instead of the conventional plural one.
I guess the include in findAll specifies the model name, not the table Name. Wheels ORM pluralizes in some occasions the model Name and maps it to the table Name. Maybe you could explicitely set the table name for the model in the model?
I solve this matter, My models were not named the same as my database tables. and once that was done properly, this problem went away.
When you create model in laravel for eloquent, the User model is treated like a users table. Why is that? Can we use an exact table name for the model?
I think more than anything it's convention. I don't see why you couldn't create a Users model for your users table, but a User is an instance of users, which is why it's done that way. You can always specify the table the model uses with:
protected $table = 'name_of_table';
which can be different than the model name. For example, a Data model can use the table userdata, as long as you specify that.
Hope that helps.