Laravel 6.11: Accessor 中的购物车 HasMany::fisrt() 不工作

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

编辑:拼写错误已更正(在翻译部分代码时出现)

我正在使用 Laravel 6.11 (MAMP) 构建购物车。 我遇到了一些让我发疯的错误。请帮忙! 首先我会给你代码,然后是错误:

Table 购物车:

id
order_date
arrived_date
status
user_id

Table cart_details:

id
cart_id
product_id
quantity
discount

请注意,此时这两个表为空,尚未插入任何记录。

一个购物车可以有很多购物车详细信息。所以在 Cart.php 我有:

class Cart extends Model
{
    public function details()
    {
        return $this->hasMany(CartDetail::class);
    }
}

CartDetailController 包含:

class CartDetailController extends Controller
{
    //let's insert data in  cart_details so the order can be created
    public function store(Request $request)
    {

        $cartDetail = new CartDetail();

        $cartDetail->cart_id = auth()->user()->cart_identification;/* cart_id from the logged user. In User.php I have getCartIdentifitacionAttribute, I'm going to put the code right after this */

        $cartDetail->product_id = $request->product_id;
        $cartDetail->quantity = $request->quantity;

        $cartDetail->save();

        return back();
    }
}

在 User.php 我有:

  public function carts()
    {
        return $this->hasMany(Cart::class); //a user can have many carts
    }
    public function getCartIdentificationAttribute()
    {
        $cart = $this->carts()->where('status','Active')-> first();

        if($cart)
        {
            return $cart->id;
        }
        else// if there's no cart, let's create a new one
        {
            $cart = new Cart();
            $cart->status = 'Active';
            $cart->user_id = $this->id;
            $cart->save();
            return $cart->id;

       }
    }

.blade(将向管理员显示订单的详细信息):

@foreach (auth()->user()->cart_identification->details as $detail)

      <ul>
            <li>{{ $detail }}</li>
        </ul>
@endforeach

现在的错误,两个...当然相关:(

  1. 在产品页面中,点击 "Add to the Cart" 后:

    BadMethodCallException 调用未定义的方法 Illuminate\Database\Eloquent\Relations\HasMany::fisrt()

如果我打印 $this->carts()->where('status','Active') 它有效,当我添加 ->fist() 时错误显示

  1. 当以管理员身份访问blade时会做以下处理:

    Facade\Ignition\Exceptions\ViewException 调用未定义的方法 Illuminate\Database\Eloquent\Relations\HasMany::fisrt() (视图:/Applications/MAMP/htdocs/app-shop/resources/views/home.blade.php)

如果我打印 auth()->user() 它有效,是 auth()->user()->cart_identification 导致了错误。

我错过了什么??

非常感谢!!

Auth()->user()->cart_identificacion哪里来的? 您可以使用这样的关系:

Auth::user()->carts->id

或者你这样使用你的函数:

Auth::user()->getCartIdentificationAttribute()

我认为你有 typo error

应该是

$cart = $this->carts()->where('status','Active')->first();

没有

 $cart = $this->carts()->where('status','Active')->fisrt();

然后在您的模型中将您的代码更改为此。

public function scopeCartIdentification()
    {
        $cart = $this->carts()->where('status','Active')->first();

        if($cart)
        {
            return $cart->id;
        }
        else// if there's no cart, let's create a new one
        {
            $cart = new Cart();
            $cart->status = 'Active';
            $cart->user_id = $this->id;
            $cart->save();
            return $cart->id;

       }
}

然后像这样调用你的控制器

public function store(Request $request)
    {

        $cartDetail = new CartDetail();

        $cartDetail->cart_id = auth()->user()->CartIdentification();/* cart_id from the logged user. In User.php I have getCartIdentifitacionAttribute, I'm going to put the code right after this */

        $cartDetail->product_id = $request->product_id;
        $cartDetail->quantity = $request->quantity;

        $cartDetail->save();

        return back();
    }