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列表查询用户信息