2

I am trying to make a select from relation. I have the following relation in my Product model:

public function category(){
    return $this->belongsTo(Category::class);
}

In my CrudController I describe the field like that:

        [
            'type' => "relationship",
            'name' => 'category', // the method on your model that defines the relationship
        ],

Like that the select contains all category from db as option, but I need only the ones where user_id is equal to backpack_user()->id

I've tried to add where clause to relation, and adding the below parameter to the field description array, but no luck so far. Would be nice if someone could show me what I'm missing here.

 'options'   => (function ($query) {
     return $query->where('user_id', backpack_user()->id)->get();
 }),
Urudin
  • 286
  • 2
  • 14
  • 2
    according to your explanation, you should have query->where('user_id' ... Not query->where('user' ... – OMR Aug 03 '20 at 20:12
  • Well, I've mistyped here, because I've already deleted this line once. Anyway I've tried again if I mistyped also when I tried first, but still not working as expected :( – Urudin Aug 04 '20 at 15:59

1 Answers1

0

Seems I've been a bit too sleepy. The above code works as it should only I've passed it accidentally to the setColumns() method instead of addFields()

Urudin
  • 286
  • 2
  • 14