Many to many relationship Laravel 7

Viewed 18

Database image

OrderController.php

 public function create()
{
    $user = Auth::user();

    //shows only the dishes of the logged user
    $dishes = Dish::orderBy("name", "asc")->where('user_id', Auth::id())->get();

    return view("admin.orders.create", compact("dishes"));
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    $validatedData = $request->validate([
        "name" => "required|min:3",
        "lastname" => "required|min:3",
        "address" => "required",
        "email" => "required",
        "phone" => "required|min:8",
        "total" => "required",
        "dishes" => "required",
    ]);

    $order = new Order();
    $order->fill($validatedData);

    $order->user_id = Auth::id();

    $order->save();

    //attach dishes to order
    foreach ($request->dishes as $dish) {
        $order->dishes()->attach($dish, [
            "quantity" => $dish["quantity"],
            "subtotal" => $dish["subtotal"]
        ]);
    }



    return redirect()->route("admin.orders.show", $order->id);
}

/**
 * Display the specified resource.
 *
 * @param  \App\Order  $order
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    return view('admin.orders.show', [
        'order' => Order::findOrFail($id)
    ]);
}

Order model

protected $fillable = [
    "name", "lastname", "address", "email", "phone", "total", "user_id"
];

public function dishes()
{
    return $this->belongsToMany('App\Dish')
        ->withPivot('quantity', 'subtotal');
}

I have a problem linking the orders table with the dishes one. In the pivot table, I also have other columns to link.

What can I do to do this many-to-many relation?

The view I'm doing has the only purpose of trying to save the data into the pivot table.

View create.blade.php

{{-- quantity pivot table --}}
                @foreach ($dishes as $dish)
                    <div class="form-group">
                        <label for="dish-quantity">Quantità di {{ $dish->name }}</label>
                        <input id="dish-quantity" type="number" name="dishes[{{ $dish->id }}]"
                            class="form-control @error('dishes') is-invalid @enderror"
                            placeholder="Inserisci la quantità" value="{{ old('dishes') }}">
                        @error('dishes')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                @endforeach

                {{-- subtotal table --}}

                @foreach ($dishes as $dish)
                    <div class="form-group">
                        <label for="dish-subtotal">Subtotale di {{ $dish->name }}</label>
                        <input id="dish-subtotal" type="number" name="subtotals[{{ $dish->id }}]"
                            class="form-control @error('subtotals') is-invalid @enderror"
                            placeholder="Inserisci il subtotale" value="{{ old('subtotals') }}">
                        @error('subtotals')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                @endforeach



                {{-- total price --}}
                <div class="form-group">
                    <label for="total_price">Prezzo totale</label>
                    <input id="total_price" type="text" name="total"
                        class="form-control @error('total') is-invalid @enderror" placeholder="Inserisci il titolo"
                        value="{{ old('total') }}" required>
                    @error('total')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>
0 Answers
Related