Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel How to query foreign relations data along with multiple main records

$rooms = Room::with(["topic", "owner", "users"])->offset(($page - 1) * $total_posts)->limit($total_posts)->orderBy("id", "desc")->get();


    public function topic()
    {
        return $this->belongsTo(Topic::class);
    }

    public function owner()
    {
        return $this->belongsTo(User::class);
    }

    public function users()
    {
        return $this->belongsToMany(User::class, "room_users");
    }
 
PREVIOUS NEXT
Tagged: #laravel #How #query #foreign #relations #data #multiple #main #records
ADD COMMENT
Topic
Name
3+1 =