Answer the question
In order to leave comments, you need to log in
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');
}
}
$shedules = Shedule::with('directions')->where('user_id', $user)->where('date', strtotime(date('Y-m-d')))->get();
foreach ($shedules as $shedule){
dd($shedule->directions);
}
Answer the question
In order to leave comments, you need to log in
public function directions()
{
return $this->belongsTo('Growth\Direction', 'direction_id', 'id');
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question