简体   繁体   中英

Laravel get attribute with hasMany

I'm making shop online with Laravel. I made Cart with records user_id and product_id with relations hasMany to products. 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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