User模型关联一对多
public function books(){
return $this->hasMany(UserBooks::class,'user_id');
}
新建UserBooks模型
控制器调用
$user = User::find(1);
return $user->books;
显示数据
[
{
"id":1,
"user_id":1,
"book_price":"500.00",
"book_name":"Linux从入门到入土",
"created_at":"2020-06-20T14:58:56.000000Z",
"updated_at":"2020-06-20T14:58:58.000000Z",
"deleted_at":null
},
{
"id":10,
"user_id":1,
"book_price":"500.00",
"book_name":"Mysql从入门到入土",
"created_at":"2020-06-20T14:58:56.000000Z",
"updated_at":"2020-06-20T14:58:58.000000Z",
"deleted_at":null
},
{
"id":12,
"user_id":1,
"book_price":"500.00",
"book_name":"golang从入门到入土",
"created_at":"2020-06-20T14:58:56.000000Z",
"updated_at":"2020-06-20T14:58:58.000000Z",
"deleted_at":null
}
]
如何列表 想获取指定的条数 可以用如下方法