84669 人學習
152542 人學習
20005 人學習
5487 人學習
7821 人學習
359900 人學習
3350 人學習
180660 人學習
48569 人學習
18603 人學習
40936 人學習
1549 人學習
1183 人學習
32909 人學習
laravel ORM 一对一、一对多、多对多、原生的MYSQL怎么写?laravel ORM 的with原理是什么?如题
认证高级PHP讲师
class User extends Model{ public function Roles() { return $this->hasOne("App\Role", 'user_id', 'id'); } } class Message extends Model{ public function User() { return $this->belongsTo("App\User", 'user_id', 'id'); } } //Controller 输出sql看 \DB::listen(function($sql,$binds){ dump($sql,$binds); }); $role = User::find(1)->Roles;//一对多类似 //select * from `users` where `id` = 1 limit 1 ; //select * from `roles` where `user_id` = 1 limit 1 ; $mess = Message::with('User')->where('type',$type)->get(); //select *from messages where type=?; 查询出用户id列表 //select * from `users` where `id` in (?);根据id列表查询用户信息
认证高级PHP讲师