2

I wan't to get the id of sub-category that is under category. I tried passing the value of id in the route file using

$categories->subcategory->id

However it gives me an error of:

Property [subcategory] does not exist on this collection instance.

Here's my relationship:

Category

class Category extends Model
{
    protected $fillable = ([
        'name'
    ]);

    public function subcategory(Type $var = null)
    {
        # code...
        return $this->hasMany(Subcategory::class,'category_id','id');
    }
}

And here's my blade

<form action="{{route('subcategory.update', $categories->subcategory->id)}}">
    <div class="modal-body">
        ...
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
    </div>
</form>

Controller

public function create()
{
    $categories = Category::all();
    // dd($categories);
    return view('settings.create', compact('categories'));
}
dparoli
  • 8,891
  • 1
  • 30
  • 38
kwestionable
  • 496
  • 2
  • 8
  • 23
  • Possible duplicate of [Property \[title\] does not exist on this collection instance](https://stackoverflow.com/questions/41366092/property-title-does-not-exist-on-this-collection-instance) – dparoli Sep 05 '19 at 08:31
  • 2
    As @dparoli said, `subcategory` is a `hasMany` relation, so in `$category->subcategory` you have multiple in a collection, not one `subcategory`. you need to access one of them to get the ID `$category->subcategory[0]->id` if there is at least one result. – N69S Sep 05 '19 at 08:37
  • `$categories` is already a collection, and the `subcategory()` relationship is a collection too because it is a `hasMany` and should be named `subcategories()`, so now you have this error on `$categories` but you will have the same error on property `id` if you do `$categories[0]->subcategory->id`. So you have to decide if you want to retrieve multiple records or only one, but you didn't post the controller code. – dparoli Sep 05 '19 at 08:49
  • I will edit post to show Controller code @dparoli – kwestionable Sep 05 '19 at 08:51
  • The function posted is a `create()` so you are probably trying to show the creation form for some model, it's unclear if it is a `Category` or `Subcategory` one. But the form posted has an action that point to an update route. I cannot understand what you are trying to do but I don't want to debug all your code, this is not how SO works. The error posted has already good answers in the link provided, after you solved that error you can open a new question with other error you would have and the minimal sample code required. – dparoli Sep 05 '19 at 09:07

1 Answers1

3

It sounds like your $categories variable is a collection of Category::class ?

Keep in mind that the subcategory function will return multiple items, because of the HasMany

You could try the following:

$categories->first()->subcategory->first()->id

OR

$categories->subcategory->first()->id

If not, you could add the subcategory relationship to the Category::class to the $with array, so it's automatically loaded in. And try again.

To use the $with array:

class Category extends Model
{
    protected $with = ['subcategory'];
}
Community
  • 1
  • 1
  • How does that `$with` array syntax looks like? I'm sorry I'm new to Laravel – kwestionable Sep 05 '19 at 08:49
  • No problem! it is : `protected $with = ['subcategory'];` I will update the answer with an example – Stefano Groenland Sep 05 '19 at 08:53
  • Hello, I got it working now. How does `$categories->first()->subcategory->first()->id` work? – kwestionable Sep 05 '19 at 09:28
  • 1
    The `$categories` variable contains an Collection of categories if you defined it in your controller with `$categories = Category::all()` or `$categories = Category::get()` so calling `->first()` on it returns the first instance out of the collection. The same goes for `subcategory` because it is a `HasMany` it returns an Collection instance instead of a single Model instance. finally you want the `->id` attribute from a single subcategory. You can only get the id on a single instance, or by using `subcategory->pluck('id')` to get them all. Hope this helps! – Stefano Groenland Sep 05 '19 at 12:38