R
R
Ruslan Absalyamov2018-04-01 17:29:53
Laravel
Ruslan Absalyamov, 2018-04-01 17:29:53

How to refer to a related table one to one?

I don’t understand a little how to get data from a linked table, I seem to be doing everything according to the documentation, but problems arise
In the table
shedule
id | date | start | end | direction_id | user_id | created_at | updated_at
direction
id | name | created_at | updated_at
And I need to go to the shedule and get the data from the direction table
I organized the connection

class Shedule extends Model
{
    protected $table = 'shedule';
    protected $fillable = ['date', 'start', 'end', 'direction_id', 'type', 'user_id', 'branch_id', 'room_id'];

    public function directions()
    {
        return $this->belongsTo('Growth\Direction');
    }
}

class Direction extends Model
{
    protected $table = 'direction';
    protected $fillable = ['id_user', 'name'];

    public function shedule()
    {
        return $this->hasOne('Growth\Shedule');
    }
}

And when I access the model, it does not work, writes null
$shedules = Shedule::with('directions')->where('user_id', $user)->where('date', strtotime(date('Y-m-d')))->get();
        foreach ($shedules as $shedule){
            dd($shedule->directions);
        }

But I don’t understand why null pops up, It seems to be how the directon model should pop up

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Absalyamov, 2018-04-01
@rusline18

public function directions()
    {
        return $this->belongsTo('Growth\Direction', 'direction_id', 'id');
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question