Home > Back-end >  store user_id and Product_id session in database laravel
store user_id and Product_id session in database laravel

Time:10-11

i want store this form to database

Form

< form method="POST" action="{{ route('faktor.store') }}" >

    @csrf
                    @php $total = 0 @endphp

                    @if(session('cart'))

                    @foreach(session('cart') as $id => $details)

                   @php $total  = $details['price'] * $details['quantity'] 

@endphp

 <input type="hidden" name="name" value="{{ $details['name'] }}">

{{ $details['name'] }}

<input type="hidden" name="quantity" value="{{ $details['quantity'] }}">{{ $details['quantity'] }}
                            
{{ $details['price'] }}
      <input type="hidden" name="product_id">

                    @endforeach

                    @endif
                        <h5  className="font-weight-bold">total</h5>
                        <h5  className="font-weight-bold"><input type="hidden" name="price"
                            value="{{ $total }}">
                            {{ $total }}$</h5>

                    <button type="submit"
                            className="btn1 btn-lg btn-block btn-primary">
                        <h5  className="font-weight-semi-bold text-light">
                            pay
                        </h5>
                    </button>
< / form>

and store controller

public function store(Request $request,$id)

    {

        $request->validate([

        'total' => 'required',

    ]);
        $input = $request->all();

        $input['user_id'] = Auth::id();

        $product = Product::find($id);

        if($request->session()->exists('cart'))

        {
            $oldCart = $request->session()->get('cart');
        }
        else
        {
            $oldCart=false;
        }

        $Cart = new Faktor($oldCart);

        $Cart->Add($product, $id);

        $request->session()->put('cart',$Cart);

        Faktor::create($input);

        return redirect()->back();

    }

product table

Schema::create('products', function (Blueprint $table) {

            $table->id();

            $table->unsignedBigInteger('user_id');

            $table->unsignedBigInteger('category_id');

            $table->string('name')->nullable();

            $table->text('desc')->nullable();

            $table->string('image')->nullable();

            $table->string('price');

            $table->string('active')->default('0');

            $table->string('ok')->default('0');

            $table->foreign('user_id')->references('id')->on('users')
                ->onDelete('cascade');

            $table->foreign('category_id')->references('id')->on('categories')
                ->onDelete('cascade');

            $table->timestamps();

faktor table

 Schema::create('faktors', function (Blueprint $table) {

            $table->id();

            $table->unsignedBigInteger('user_id');

            $table->unsignedBigInteger('product_id');

            $table->string('total');

            $table->string('send')->default('0');

            $table->foreign('user_id')->references('id')->on('users')
                ->onDelete('cascade');

            $table->foreign('product_id')->references('id')->on('products')
                ->onDelete('cascade');

            $table->timestamps();
        });

how write my store code in controller

my error

Too few arguments to function App\Http\Controllers\FaktorController::store(), 1 passed in G:\test\shop\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 2 expected

CodePudding user response:

Your issue here is that the number of arguments your store function expects, does not match the number of arguments you provide it with in your form action.

public function store(Request $request, $id)
{
    // stuff ...
}

You've defined the function as expecting 2 arguments (your first argument - $request is a special case and will be injected by Laravel so does not require you to provide it). Compare that to your form action.

<form method="POST" action="{{ route('faktor.store') }}">

To resolve the error, you need to provide the second argument ($id) in your action:

<form method="POST" action="{{ route('faktor.store', ['id' => $id]) }}">

CodePudding user response:

thank you but how can i get id from this session ? i do it from movie and it's important that do end it i want store data form into database

@php

$total = 0

@endphp

@if(session('cart'))

@foreach(session('cart') as $id => $details)

@php

$total = $details['price'] * $details['quantity']

@endphp

< form method="POST" action="{{ route('faktor.store',$details['id']) }}" enctype="multipart/form-data" >

@csrf

{{ $details['name'] }}

{{ $details['quantity'] }}

{{ $details['price'] }}

total

{{ $total }} $

Pay

@endforeach

@endif

this is my error for this Form

Undefined array key "id"

  • Related