简体   繁体   English

Laravel雄辩的表结构

[英]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. 我很新的LaravelEloquent的世界,我爱它,到目前为止,但我无法低估如何构建我的表和模式,以便我能够与eloquents关系到CRUD我的数据。 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. 我的问题是,如何最好地使用我的关系为我建立模型以使我能够CRUD。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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