php - Laravel Eloquent hasOne returns empty -



php - Laravel Eloquent hasOne returns empty -

i have 2 tables named 'works' , 'slides' 2 classes extend eloquent named 'work' , 'slide'. within 'works' table have 'id' column, 'title' column, 'description' column , 'image' column , within 'slides' table have composite key using foreign key 2 columns named 'id' , 'work_id'.

this how database relationship looks:

this code slide class:

class slide extends eloquent { public $timestamps = false; public function workid() { homecoming $this->hasone('work', 'work_id', 'id'); } }

and code work class:

class work extends eloquent { public $timestamps = false; }

inside index page trying work object calling 'workid()' method within slide object returns empty code:

$slides = slide::all(); $works = new \illuminate\database\eloquent\collection; foreach ($slides $slide) { $works->push($slide->workid()); } printf($works);

however, if replace '$slide->workid()' phone call with:

work::find($slide->work_id)

then finds row no problem.

what should function homecoming work object rather calling work::find()?

slide belongs work because foreign key works within slides table. you're using wrong naming relationship function , wrong syntax hasone.

you need use:

class slide extends eloquent { public $timestamps = false; public function work() { homecoming $this->belongsto('work'); //or homecoming $this->belongsto('work', 'work_id', 'id'); } }

to work belongs slide: $slide->work

note: utilize "association()" belongsto, not "push()"

php mysql laravel eloquent foreign-key-relationship

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -