0

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:

enter image description here

Am I not able to load a relationship only for the specific resource I am showing?

8
  • The column name isn't field right? Laravel searches for column names, so it should probably be name where you have written field.
    – Loek
    Commented Jul 8, 2019 at 10:59
  • Not sure what you mean? What column name?
    – oliverbj
    Commented Jul 8, 2019 at 11:01
  • dd($stream->with('fields')->get('1')); Commented Jul 8, 2019 at 11:02
  • But adding 1 does not make it dynamic? I am using route model binding to get the stream, and thus would like to return all associated fields for only that specific stream.
    – oliverbj
    Commented Jul 8, 2019 at 11:03
  • I don't think you need to load again the fields, you can use simply : dd($stream->fields); Commented Jul 8, 2019 at 11:03

2 Answers 2

4

It looks like you are using route model binding to resolve the $field. And using $stream->with('fields')->get(); to load the relationship.

It is all working as intended, I think there is a misunderstanding here on how the route model binding and relationships work.

$stream->with('fields')->get();

This will load all streams since it starts a new query and is the same as doing Stream::with('fields')->get().

If you want a specific field for your stream you can do:

$field = $stream->fields()->findOrFail($fieldId);

In you controller method that would look something like this

// streams/1/fields/1
public function show(Stream $stream, $fieldId)
{
   // findOrFail will throw a 404 if the field could not be found for the specific stream
   $field = $stream->fields()->findOrFail($fieldId);

   dd($field);
}

$stream with id 1 (for your example url) will have been resolved by the route model binding.

Otherwise this probably already works if you have setup the correct route model binding:

// streams/1/fields/1
public function show(Stream $stream, Field $field)
{
   dd($field);
}

However this will also load a field for another stream (the field does not belong to) since it doesn't know how to scope the query for the stream with the id 1.

9
  • Ah, I see! I must've misunderstood how the with was working together with route model binding. But what if I simply pass $stream->load('fields');? Insted of using the get()?
    – oliverbj
    Commented Jul 8, 2019 at 11:11
  • $stream->load('fields'); does the same as you would access $stream->fields and loads the full relationship (internally runs something like $stream->fields = $stream->fields()->get();.
    – Alex
    Commented Jul 8, 2019 at 11:13
  • But that only returns the fields for the specific field I am accessing (which is what I want - all fields for the specific stream)
    – oliverbj
    Commented Jul 8, 2019 at 11:14
  • Did you see my comment ? only use $stream->fields and read the doc : laravel.com/docs/5.8/eloquent-relationships Commented Jul 8, 2019 at 11:15
  • @VincentDecaux Yes, but I will need to be able to access the stream properties as well. With $stream->fields only returns the fields, not the stream object as well.
    – oliverbj
    Commented Jul 8, 2019 at 11:17
0

I think streame table should have an attribut field_id and you should edit the function like below:

public function fields()
 {
    return $this->hasMany(Field::class, 'field_id', 'id');
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.