Skip to content
Advertisement

Laravel 6.11: Shopping cart HasMany::fisrt() in Accessor not working

EDIT: typos corrected (made while translating some of the code)

I’m building a shopping cart with Laravel 6.11 (MAMP). I am getting a couple of errors that are driving me nuts. Please help! First I’ll give you the code, afterwards the errors:

Table carts:

JavaScript

Table cart_details:

JavaScript

Please note that the two tables are EMPTY at this point, no records inserted just yet.

A cart can have many cartdetails. So in Cart.php I have:

JavaScript

CartDetailController contains:

JavaScript

In User.php I have:

JavaScript

The .blade (which will show the administrator the details of the order):

JavaScript

And now the errors, two… certainly related 🙁

  1. In a product page, after hitting “Add to the Cart”:

    BadMethodCallException Call to undefined method IlluminateDatabaseEloquentRelationsHasMany::fisrt()

If I print $this->carts()->where('status','Active') it works, it is when I add ->fist() that the error shows

  1. When accesing the blade as an administrator would do to deal with the order:

    FacadeIgnitionExceptionsViewException Call to undefined method IlluminateDatabaseEloquentRelationsHasMany::fisrt() (View: /Applications/MAMP/htdocs/app-shop/resources/views/home.blade.php)

If I print auth()->user() it works, it is auth()->user()->cart_identification that causes the error.

What am I missing??

Thanks a ton!!

Advertisement

Answer

I think you have a typo error.

It should be

JavaScript

Not

JavaScript

And in your model change your code to this.

JavaScript

and call in to your controller like this

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement