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