简体   繁体   中英

Laravel eloquent table structure

I am quite new to the Laravel and Eloquent world and I'm loving it so far, but I am having trouble understating how to structure my tables and models in order for me to be able to CRUD my data with eloquents relationships. So far I have structured my tables like this:

Users -----------------has_one------->onsite_teachers
onsite_teachers -------has_many------>train_lines
train_lines -----------has_many------>train_stations

train_lines -----------belongs_to---->text_train_lines
train_stations --------belongs_to---->text_train_stations

My question is how would be best to construct my models to me able to CRUD using the relationships. Because I have tried the following with no success:

$user = User::find(1);
$user->onsite_teacher->train_lines()->save($data);
$user->onsite_teacher->train_lines->train_stations()->save($data);

It looks like your query is wrong

Try this:

$user = User::find(1);
$user->onsite_teacher()->first()
     ->train_lines()->save($data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM