Search code examples
jsonlaravel-5laravel-bladehas-one

How to get the object for has one


i have a model request that has a foreign key to category and a collection of buyers

class Request extends Model {

// TABLE NAME
protected $table = 'requests';

// MASS ASSIGMENTS
protected $guarded = ['id'];

// FK
public function category()
{
    return $this->hasOne('App\Objects\Category', 'id');
}

// FK
public function buyers()
{
    return $this->hasMany('App\Objects\RequestBuyer');
}


}

i have a query that get's all request records with the collection of buyers

$requests = Request::with('buyers')->get();

this return a json that contains all records with the collection of buyers but i want to include the category details in this json response.

How do i accomplish this?


Solution

  • do as following,

    $requests = Request::with('buyers', 'category')->get();
    

    By the way what is the relationship between Request and Category?