Lined paddle mixed relationship. How can i do this?

I have a question. I have two tables: devices and faces

In these databases, one device can be assigned to one person (this is one to one relationship), but one person can have many devices (so this is a ratio from one to many). How can I do this in Laravel? For one to one, it's a little easier, and in the model I can do this:

Devices

class Devices extends Model
{
    public function persons(){

        return $this->hasOne('App\Persons','id');

    }
}

Faces

class Persons extends Model
{
    public function devices(){

        return $this->belongsTo('App\Devices');

    }
}

SO I think this relationship is one to one: one device is assigned to one person.

How can I get many devices for one person? Has changed. Who owns the "Man in Man" model? Will it be good for these options?

+4
source share
2

a one-to-many, :

class Persons extends Model
{
    public function devices(){

        return $this->hasMany('App\Devices');

    }
}

class Devices extends Model
{
    public function person(){

        return $this->belongsTo('App\Persons');

    }
}
+2

:

public function devices() {
    return $this->hasMany('App\Devices');
}

, .

public function persons() {
    return $this->belongsTo('App\Persons');
}

, .


: http://laravel.com/docs/5.3/eloquent-relationships#one-to-many

+2

Source: https://habr.com/ru/post/1664196/


All Articles