简体   繁体   English

Laravel 使用 hasMany 获取属性

[英]Laravel get attribute with hasMany

I'm making shop online with Laravel.我正在使用 Laravel 在线购物。 I made Cart with records user_id and product_id with relations hasMany to products.我制作了带有记录 user_id 和 product_id 的购物车,其中包含与产品的hasMany关系。 My problem is that I can't get for example product's name or price, I can only get whole array with products and cart data.我的问题是我无法获得例如产品的名称或价格,我只能获得包含产品和购物车数据的整个数组。 Can someone tell me how to get it?有人可以告诉我如何得到它吗? Maybe there is a problem with a query or just view syntax.也许查询有问题,或者只是查看语法。

My migration:我的迁移:

    public function up()
    {
        Schema::create('carts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('product_id');
            $table->timestamps();
            
            $table->foreign('user_id')->references('id')->on('users');
            $table->foreign('product_id')->references('id')->on('products');        
        });

Here is my controller function:这是我的控制器功能:

  public function index(Request $request)
    {

        $query = Cart::with('product')->where('carts.user_id', $request->user()->id);
        $query = $query->get();

       return view('cart.index', ['cart' => $query]);
    }

And view to show cart并查看以显示购物车

@extends('app')
@section('content')
    @foreach ($cart as $item)
    <form method="" action="{{ route('destroycart', ['id' => $item->id]) }}">
        {{ $item['product'] }}
        <button class="btn btn-outline-dark">X</button>
    </form> 
    @endforeach
@endsection

Model:模型:

class Cart extends Model
{
    use HasFactory;

    public function product() {

        return $this->hasMany(Product::class, 'id', 'product_id');
    }
}

Is there another option for $item['product'] to get only product data? $item['product']是否有另一种选择来仅获取产品数据?

Forgot to paste what view returns:忘记粘贴视图返回的内容:

[{"id":10,"name":"lklkl","description":"klklkk","img":"przyklad.mo.jpg","price":50,"count":9,"created_at":"2022-05-24T13:13:03.000000Z","updated_at":"2022-05-24T13:13:03.000000Z"}]

I would like to get for example product's name.我想获得例如产品的名称。

You should then also access products, so:然后,您还应该访问产品,因此:

@foreach ($cart as $item)
    // Cart stuff here.

    @foreach ($item->product as $prod)
        // Product stuff here
    @endforeach
@endforeach

You can get a single cart in the controller method if you want to display only one cart at a time如果您想一次只显示一个购物车,您可以在控制器方法中获取一个购物车

public function index(Request $request)
{
    return view('cart.index', [
        'cart' => Cart::with('product')
            ->where('user_id', $request->user()->id)
            ->latest()
            ->first()
    ]);
}

Then in your view you can loop over the products in the cart然后在您的视图中,您可以遍历购物车中的产品

@extends('app')
@section('content')
    <form method="" action="{{ route('destroycart', ['id' => $cart->id]) }}">
        @foreach ($cart->product as $product)
            <p>{{ $product->name }}</p>
        @endforeach
        <button class="btn btn-outline-dark">X</button>
    </form> 
@endsection

Or if you want to show all carts for the currently logged in user then you can do it as或者,如果您想显示当前登录用户的所有购物车,那么您可以这样做

public function index(Request $request)
{
    return view('cart.index', [
        'carts' => Cart::with('product')
            ->where('user_id', $request->user()->id)
            ->latest()
            ->get()
    ]);
}

The in the view loop over the carts and with each iteration - loop over the products在视图中循环购物车和每次迭代 - 循环产品

@extends('app')
@section('content')
    @foreach($carts as $cart)
        <form method="" action="{{ route('destroycart', ['id' => $cart->id]) }}">
            @foreach ($cart->product as $product)
               <p>{{ $product->name }}</p>
            @endforeach
            <button class="btn btn-outline-dark">X</button>
        </form> 
    @endforeach
@endsection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM