Any Ideas?
Laravel, Creating unique order on foreach
I know it should be simple :(
My problem is, I have an app for libraries and students, so libraries can have many books in their library this is the tables.
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->string('book_name')->unique()->nullable();
$table->string('author_name')->nullable();
$table->string('edition_number')->nullable();
$table->string('volume_number')->nullable();
$table->timestamps();
});
}
public function up(): void
{
Schema::create('libraries', function (Blueprint $table) {
$table->id();
$table->string('name')->unique()->nullable();
$table->string('phone')->unique()->nullable();
$table->string('CR')->nullable();
$table->string('district')->nullable();
$table->string('city')->nullable();
$table->string('google_maps')->nullable();
$table->foreignIdFor(User::class, 'user_id')
->constrained('users', 'id')
->cascadeOnDelete();
$table->timestamps();
});
}
public function up(): void
{
Schema::create('book_library', function (Blueprint $table) {
$table->id();
$table->string('qty')->nullable();
$table->decimal('price', 10, 2)->nullable();
$table->text('offer')->nullable();
$table->foreignIdFor(Book::class, 'book_id')
->constrained('books', 'id')
->cascadeOnDelete();
$table->foreignIdFor(Library::class, 'library_id')
->constrained('libraries', 'id')
->cascadeOnDelete();
});
}
As you can see every libeary can have the same book.
where is the problom here:
public function store(Request $request)
{
try {
DB::transaction(function () use ($request) {
foreach ($request->carts as $book) {
foreach ($book['books'] as $libraryBook) {
$order = Order::firstOrCreate(
[
'library_id' => $book['library_id'],
'user_id' => $book['user_id'],
],
[
'total_payment' => 0
]
);
$detail = Orderdetail::create([
'order_id' => $order->id,
'book_library_id' => $book['book_library_id'],
'book_id' => $libraryBook['book_id'],
'price' => $libraryBook['price'],
'total_price' => $libraryBook['price']
]);
$order->increment('total_payment', $libraryBook['price']);
}
if (!$order->latestStatus(Order::STATUS['sent_to_library']['key'])) {
$order->setStatus(Order::STATUS['sent_to_library']['key']);
}
BookLibrary::findOrFail($detail->book_library_id)->decrement('qty');
}
$request->user()->carts()->delete();
});
return redirect()->route('user.order.index');
} catch (\Exception $e) {
Log::error($e);
}
}
I here
$order = Order::firstOrCreate(
[
'library_id' => $book['library_id'],
'user_id' => $book['user_id'],
],
[
'total_payment' => 0
]
);
I want to create uniqe order even if the order exists, i mean the student could add to card book A and place order order id now is 1, and than same user add the book A again to the cart and place order i want a new order not updating the exist order. But also if he adds more then one book it will keep creating orders if I change firstOrCreate to create!!
will appropriate your help.
@tompe Thank you for helping, I solve it by doing this.
public function store(Request $request)
{
try {
DB::transaction(function () use ($request) {
$libraries = [];
foreach ($request->carts as $cart) {
$libraryId = $cart['library_id'];
if (!isset($libraries[$libraryId])) {
$libraries[$libraryId] = Order::create([
'total_payment' => 0,
'user_id' => $request->user()->id,
'library_id' => $libraryId,
]);
}
$order = $libraries[$libraryId];
foreach ($cart['books'] as $book) {
$order->increment('total_payment', $book['price']);
$detail = Orderdetail::create([
'order_id' => $order->id,
'book_library_id' => $cart['book_library_id'],
'book_id' => $book['book_id'],
'price' => $book['price'],
'total_price' => $book['price']
]);
BookLibrary::findOrFail($detail->book_library_id)->decrement('qty');
}
if (!$order->latestStatus(Order::STATUS['sent_to_library']['key'])) {
$order->setStatus(Order::STATUS['sent_to_library']['key']);
}
}
$request->user()->carts()->delete();
});
return redirect()->route('user.order.index');
} catch (\Exception $e) {
Log::error($e);
}
}
Please or to participate in this conversation.