krie9er's avatar

how does laravel know?!

Heylo,

i've made some startexercieses and they were pretty cool.

But i still have one question:

how does laravel match a model to the equivalent of the database?

So in my lessons i had a table "articles" and a model called article, with which i could use the "articles" table.

But how does laravel identify this model?

For example if i have a table called "works" and i try to use the model "exams" how can i tell laravel which model it has to use?

A concrete example: I have a table currencies, which has an 1:n relation to for example orders.

So my model is called currency and the table is called currencies.

I moved from th doctrine project to laravel, in doctrine i could define the table and class in the *.yaml.

Is there any equivalent method in laravel?

Greetings Georg

0 likes
10 replies
thomaskim's avatar
Level 41

You can assign the table name to the model.

From the docs: http://laravel.com/docs/5.1/eloquent

Table Names

Note that we did not tell Eloquent which table to use for our Flight model. 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:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'my_flights';
}
2 likes
krie9er's avatar

wow thanks!

Haven't seen this when i looked it up really thanks :-)

sorry 4 this stupid question

2 likes
krie9er's avatar

Sorry 4 another question:

but i get them while working with the framework xD

My question belongs to realtions:

i've already checked the migration topic of the documentation, but haven't got a proper answer.

I want to make a 1:N relation between the users table and a user_groups table.

Now i'm not sure, but i would place inside the users table a column called "user_group_id".

But again how laravel knows to join through the "user_group_id" column and not through another column?

Is there something like the

<?php

 protected $joincolumn = 'xyz';

Greetings Georg

pmall's avatar

@krie9er look at the doc about relationships. It is explained how to set the foreign keys names.

The default names are determined from basic conventions.

1 like
pmall's avatar

This is not in the migration doc but in the relationship doc.

By the way you seem to think relationships are joined, this is not the case. An individual query is executed to retrieve related models.

1 like
krie9er's avatar

So in the background laravel doesn't join the data inside the query?

yes i thought it would do a join query in this setting:

// App/User
public function userGroup() {
    $this->hasOne('App/UserGroup');
}

// App/UserGroup
public function users() {
    $this->hasMany('App/User');
}

// App/Http/Controller
// inside a view ...
$userGroups = UserGroup::first();
foreach($userGroups->users as $user) {
    //....
}

how would be the query/queries?

pmall's avatar

No, relationships are not joined.

Here you have two queries. One for group and one for its users. You should read the doc on eager loading and the N+1 problem.

1 like

Please or to participate in this conversation.