How to use laravel ORM even Table check flexibly

    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;//
    
    $mess = Message::with("User")->where("type",$type)->get();

similar to the query above, $role = User::find (1)-> Roles only finds the data of roles. If I want to query several fields of user and all (part of) fields of roles, how to write this orm method. For example: select user.name,user.age,role.* from.

May.11,2022

pseudo code.

Message::with(['User'=>function($query){
    return $query->select( );//where
}])
Menu