I have below model, with the fields
relationship defined.
//Stream.php
/**
* A stream can have many fields
*
* @return Illuminate\Database\Eloquent\Model
*/
public function fields()
{
return $this->hasMany(Field::class);
}
In my database, I have two streams:
//table: streams
id | name
1 | doc_stream
2 | email_stream
And 4 fields:
//table: fields
id | stream_id | name
1 | 1 | Field #1
2 | 1 | Field #2
3 | 1 | Field #3
4 | 2 | Field #4
So by this setup, I thought I could simply do:
//streams/1/fields/1
public function show(Stream $stream, Field $field)
{
dd($stream->with('fields')->get());
}
Which would then return all the fields, only for the $stream.id = 1
.
However, the relationship loads all streams:
Am I not able to load a relationship only for the specific resource I am showing?
field
right? Laravel searches for column names, so it should probably bename
where you have writtenfield
.1
does not make it dynamic? I am using route model binding to get thestream
, and thus would like to return all associatedfields
for only that specific stream.dd($stream->fields);