Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lara_Love's avatar

Error in Store session in database

Hello all I am seeing different errors and problems when I want to save the session in the database, please help me to save this form in the database. Thanks to all of you friends

FORM SEND TO CONTROLLER

			  @php $total = 0 @endphp

                @if(session('cart'))

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

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

                        <div>

                            <div class="row">

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

                                <div class="col-7">
                                    {{ $details['name'] }}
                                </div>
                                <div class="col-2">
                                    <input type="hidden" name="quantity" value="{{ $details['quantity'] }}">
                                    {{ $details['quantity'] }}
                                </div>
                                <div class="col-3">
                                    <input type="hidden" name="price" value="{{ $details['price'] }}">
                                    {{ $details['price'] }}
                                </div>
                            </div>
                        </div>
                    @endforeach
                    <div >
                          <input type="hidden" name="total" value="{{ $total }}">{{ $total }}
                                $
                     </div>
                    <div>
                        <a href="{{ route('ad') }}" type="submit" >
                           PAY
                        </a>
                    </div>
                @endif

public function ad(Request $request)

{
    $input['user_id'] = Auth::id();
    $product = $request->session()->get('cart');

// dd($product)

    $factor = Faktor::create([
        'user_id',
        'product_id',
        'total',
    ]);
    $factor->products()->attach($product);
    return redirect()->back();

Model Faktor

protected $fillable = [

    'product_id',
    'user_id',
    'total',
];



public function products()
{
    return $this->belongsToMany(Product::class);
}

public function product()
{
    return $this->hasMany(Product::class);
}

public function user()
{
    return $this->belongsTo(User::class);
}

Model Product

public function faktors() { return $this->belongsToMany(Faktor::class); }

public function faktor()
{
    return $this->belongsTo(Faktor::class);
}

Table Pivot

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

            $table->id();
            $table->foreignId('user_id')->constrained();
            $table->foreignId('faktor_id')->constrained();
        });

Table Faktors 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();
    });

Now it gives user error because I don't know how to save it. dd works and is correct

0 likes
6 replies
AungHtetPaing__'s avatar
Level 22

does this

    $factor = Faktor::create([
        'user_id',
        'product_id',
        'total',
    ]);
    $factor->products()->attach($product);
    return redirect()->back();

should be

    $factor = Faktor::create([
        'user_id' => Auth::id(),
        'product_id' => $product->id,
        'total' => $request->total,
    ]);
    $factor->products()->attach($product);
    return redirect()->back();

?

1 like
Lara_Love's avatar

@Aung Htet Paing__ Hello thank you dear friend It gives this error. How to get the Id of the products?

ErrorException Attempt to read property "id" on array here: 'product_id' => $product->id,

sazvader's avatar

@LoverCode $product is an array, so you have to get the value using this $product['id']

1 like
Lara_Love's avatar

@Aung Htet Paing__ If you mean to write it like this 'product_id' => $product['id'], Signals an error.

ErrorException Undefined array key "id"

I think sending it from the form might be a problem

AungHtetPaing__'s avatar

@LoverCode do you see $product data when you dd($product). undefined array key "id" mean there is no "id" key in your $product array. did you successfully store cart session?

Please or to participate in this conversation.