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

Lara_Love's avatar

i want it for invoice

hi dear friend's it function is correct but i want it for invoice .now it is foreach and data in table store in mane row's.and when i show in blade it make many invoice .

public function ad(Request $request,$id)

{

$products = $request->session()->get('cart',$id);

foreach ($products as $product)

{

Faktor::create([

    'user_id' => Auth::id(),

    'product_id' => $product['product_id'],

    'name' => $product['name'],

    'quantity' => $product['quantity'],

    'price' => $product['price'],

]);

}

return redirect('bill');
0 likes
29 replies
tykus's avatar

@lovercode do you have anything that groups/identifies all of the Faktor instance together; e.g. a UUID? This would make it easier to display all of the items together that belong together.

Aside, I don't understand what the ad method means to do - it is not clear if you are persisting these items in the database to represent an Order's items or something like that? Similarly, the last line before the redirect; what it that doing?

public function ad(Request $request,$id)
{
    $products = $request->session()->get('cart',$id);
    $faktor = [];
    foreach ($products as $product) {
        $faktor [] = Faktor::create([ 
            'user_id' => Auth::id(), 
            'product_id' => $product['product_id'], 
            'name' => $product['name'],
            'quantity' => $product['quantity'],
            'price' => $product['price'], 
        ]);
    }
    Faktor::create($faktor); // what is this????
    return redirect('bill',compact('faktor'));
}
1 like
Lara_Love's avatar

@tykus hi dear friend ./

This function now uses a loop to store each product in an id from the factors table, which we should show the user how many shopping carts when displaying it. This function should store and display everything that a user has purchased, and it should be stored and displayed in an id from the factors table.

Thank you for helping me so much.

this is mistake in my code and i clean it from Controller

Faktor::create($faktor);

it is correct

public function ad(Request $request,$id) { $products = $request->session()->get('cart',$id);

foreach ($products as $product)

{

     Faktor::create([ 

        'user_id' => Auth::id(), 

        'product_id' => $product['product_id'], 

        'name' => $product['name'],

        'quantity' => $product['quantity'],

        'price' => $product['price'], 

    ]);

}

return redirect('bill');

}

tykus's avatar

@LoverCode I would generate a UUID and assigned to every model you are creating; then redirect with new UID to identify all the items that are on the invoice

1 like
Lara_Love's avatar

@tykus Sorry, I don't know much about uuid. But do you think this function is correct for saving shopping cart products? And now if, for example, our shopping cart has 5 products, the invoice should store them in one ID, but it stores them in 5 IDs. Another example is showing two products in our shopping cart

id  user_id 	 product_id 	/name 	/price 	/quantity 	
29        1           2 	   i9 gen12     2000     2	
30        1            1 	    ssd 	     1000     1 
tykus's avatar

You don't need the temporary $faktor array because you don't use it:

public function ad(Request $request,$id)
{
    $products = $request->session()->get('cart',$id);
    $uuid = str()->uuid();
    foreach ($products as $product) {
        Faktor::create([ 
            'uuid' => $uuid,
            'user_id' => Auth::id(), 
            'product_id' => $product['product_id'], 
            'name' => $product['name'],
            'quantity' => $product['quantity'],
            'price' => $product['price'], 
        ]);
    }
   // you probably should clear the Cart from the session here...
    return redirect('bill')->with('uuid', $uuid);
}

You will need to add a column to the faktors (?) table to store the same UUID for each product in the Session.

Then in the bill controller action get the Products using the UUID in the Session:

$products = Faktor::where('uuid', session('uuid'))->get();

return view('...', compact('products'));
1 like
Lara_Love's avatar

@tykus

Thank you for spending so much time and coding for me.

I now need to form the uuid column

$table->uuid('id')->primary();

Create in the factors table?

this error now

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'uuid' in 'where clause'

Lara_Love's avatar

@tykus i change faktors table to

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

        $table->uuid('id')->primary();
        $table->unsignedBigInteger('user_id');
        $table->unsignedBigInteger('product_id');
        $table->string('name');
        $table->string('price');
        $table->string('quantity');
        $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();
tykus's avatar

@LoverCode no. the same UUID will be reused for every item in the Order (?), so it should not be uniquely identifying a faktor record (I suppose UUID led you incorrectly...)

Schema::create('faktors', function (Blueprint $table) {
    $table->id();
    $table->string('uuid', 36);
    $table->unsignedBigInteger('user_id');
    // ...
1 like
Lara_Love's avatar

@tykus I apologize for asking too many questions. i store it in databade . Controller Store in database

public function ad(Request $request,$id)

{

    $products = $request->session()->get('cart',$id);
    $uuid = str()->uuid();
    foreach ($products as $product) 

{

        Faktor::create([
            'uuid' => $uuid,
            'user_id' => Auth::id(),
            'product_id' => $product['product_id'],
            'name' => $product['name'],
            'quantity' => $product['quantity'],
            'price' => $product['price'],
        ]);
    }
    return redirect('bill')->with('uuid', $uuid);
}

and show blade bill public function bill() {

    $products = Faktor::where('uuid', session('uuid'))->get();
    return view('shop.bill', compact('products'));

Now this is in Faktor Model

public static function boot()

{

    parent::boot();
    static::creating(function ($model) {
        $model->uuid = Str::uuid();
    });
}

I added to the model and the controller and blade were done according to your code. But it doesn't show anything and that the uuid in the database is like this

id uuid

1 b12e17cc-dfc6-438c-9068-71fabb8792f1

2 21f5c26f-ee07-424c-88e7-d3fa43093a9c

tykus's avatar

@LoverCode no. That approach will generate a unique UUID for each item - we don't want that. What we want is some unique value to group all of the items in the Cart together as (I suppose) an Order. This "order number" can be anything unique, so I suggested a UUID.

Remove the boot method from the model; it is unnecessary for what we're trying to achieve.

1 like
Lara_Love's avatar

@tykus without boot its error

SQLSTATE[HY000]: General error: 1364 Field 'uuid' doesn't have a default value

tykus's avatar

@LoverCode did you include it here in the attributes:

$uuid = str()->uuid();
foreach ($products as $product) {
    Faktor::create([ 
        'uuid' => $uuid,
        // ... etc

and make it $fillable on the Model if needed

1 like
Lara_Love's avatar

@tykus

yes i fillable it now

but in blade bill

not show anything

Controller bill

public function bill()

{
    $products = Faktor::where('uuid', session('uuid'))->get();
    return view('shop.bill', compact('products'));

and

in bill blade

@foreach($products as $item)

   <h4 style="color: #F81D2D;"><strong>{{ $item->user->name }}</strong></h4>
    <p>{{ $item->user->mobile }}</p>
     <p>{{ $item->user->email }}</p>
     <p>{{ $item->name }}</p>
       <p>{{ $item->price}}</p>

@endforeach

Lara_Love's avatar

@tykus

Thank you for your efforts and love

It displays once in a jumbled manner. That is, because I have two products, it displays the user's name and mobile number twice, and after refreshing, it no longer displays it.

dd($products)

^ Illuminate\Database\Eloquent\Collection {#1529 ▼ #items: [] #escapeWhenCastingToString: false

session delete in blade

and data in faktore table

id uuid user_id

11 a6cf79b4-af50-4436-a436-03940c5498aa 1

12 a6cf79b4-af50-4436-a436-03940c5498aa 1

tykus's avatar

@LoverCode

it displays the user's name and mobile number twice

Obviously it will display the User information for every item in the Collection; that is exactly what you have asked it to do.

after refreshing, it no longer displays it

Because the UUID was flashed to the Session - it lasts for that Request only. You could have defined the Route to expect this UUID

Route::get('bill/{uuid}', [BillController::class, 'bill'])->name('bill.show');

and redirect instead to the Route:

return redirect()->route('bill.show', $uuid);

**However, it seems that what you really wanted was an Order model (I will continue to call it Order ???) which should contain the User information, order date etc. and then the faktors table would contain only the order items (products) information, along with a foreign key order_id (instead of the uuid we implemented earlier)?

1 like
Lara_Love's avatar

@tykus

Yes, dear teacher

You were able to create it in the best way.

it displays the user's name and mobile number twice

tykus's avatar

@LoverCode so, how do you want to solve this? Extra table for Orders, or a hack where you display the User information only on the first iteration of the loop?

@foreach($products as $item)
    @if ($loop->first)
        <h4 style="color: #F81D2D;"><strong>
            {{ $item->user->name }}</strong>
        </h4>
        <p>{{ $item->user->mobile }}</p>
        <p>{{ $item->user->email }}</p>
    @endif
    <p>{{ $item->name }}</p>
    <p>{{ $item->price}}</p>
@endforeach
1 like
Lara_Love's avatar

@tykus

Do you mean that the site will be hacked with this status? Next question, I found a problem now web.php

Route::get('bill/{uuid}', [ProductController::class, 'bill'])->name('bill.show');

controller bill

public function bill()

{
    $products = Faktor::where('uuid', session('uuid'))->get();
    return view('bill.show', compact('products'));
}

or

return redirect()->route('bill.show', $uuid);

it's Error

404 Not Found

Lara_Love's avatar

@tykus

Until now, I thought that because:

  • Laravel has several layers

-the data sent from the user's side should always be filtered with php codes and then sent to the ORM side

  • with manage data that user sent

Can't be hacked

tykus's avatar

@LoverCode

Do you mean that the site will be hacked with this status?

No.

it's Error 404 Not Found

When does this 404 happen; whenever you redirect to the URL for the Invoice (e.g. /bill/abc-123-def-456 )?

1 like
Lara_Love's avatar

@tykus

But if we write the site with a lot of jQuery and unknown packages, it can be hacked it's true ?

our route define

Route::get('bill/{uuid}', [ProductController::class, 'bill'])->name('bill.show');

in controller we need uuid instead session

public function bill()

{

    $products = Faktor::where('uuid', session('uuid'))->get();
    return view('bill.show', compact('products'));
}
tykus's avatar

@LoverCode obviously if there is a wildcard in the Route URI, then you should accept that in the action:

public function bill($uuid)
{
    $products = Faktor::where('uuid', $uuid)->get();

But if we write the site with a lot of jQuery and unknown packages, it can be hacked it's true ?

Who's to know... maybe our applications will not be interesting enough to be hacked 🤷‍♂️ (jk)

1 like
Lara_Love's avatar

@tykus

"Who's to know... maybe our applications will not be interesting enough to be hacked 🤷‍♂️ (jk)"

Exactly the same site that I am building

I apologize my friend. I asked too many questions tonight. Sorry. Will you do a review because it won't find the blade

send to ad

public function ad(Request $request)

{
    $products = $request->session()->get('cart');
    $uuid = str()->uuid();
    foreach ($products as $product)

{ Faktor::create ([

            'uuid' => $uuid,
            'user_id' => Auth::id(),
            'product_id' => $product['product_id'],
            'name' => $product['name'],
            'quantity' => $product['quantity'],
            'price' => $product['price'],

        ]);
    }
    return redirect('bill')->with('uuid', $uuid);
}

our route define

Route::get('bill/{uuid}', [ProductController::class, 'bill'])->name('bill');

public function bill($uuid)

{
    $products = Faktor::where('uuid', $uuid)->get();
    return view('shop.bill', compact('products'));
}
Lara_Love's avatar

it end of function is correct ?

return redirect('bill')->with('uuid', $uuid);

tykus's avatar
tykus
Best Answer
Level 104

@LoverCode no, the URL should have a UUID segment now

return redirect()->route('bill.show', $uuid);

or

return redirect()->to("bill/{$uuid}");
1 like
Lara_Love's avatar

@tykus

You did very well. We took a lot of your time and tired you until now. I apologize for asking so many questions

Lara_Love's avatar

@tykus

if i want sum many price that has uinque uuid in invoice

this query how to change ?

$pro = Faktor::where('uuid', $uuid)->sum('price')->get();

tykus's avatar

@LoverCode you can calculate the total using a separate query

$pro = Faktor::where('uuid', $uuid)->get();
$total = Faktor::where('uuid', $uuid)->sum('price');

or , using the Collection returned from the first query

$pro = Faktor::where('uuid', $uuid)->get();
$total = $pro->sum('price');
Lara_Love's avatar

Thank you very much for your efforts dear friend and teacher

Please or to participate in this conversation.