Its simple there are two ways could achieve this either use JSON to save the order or if you do not want to use JSON because of foreign_key use another table to store the items-order with the library as the foreign key
Jul 11, 2023
4
Level 2
Creating unique order
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();
});
}
public function store(Request $request)
{
try {
DB::transaction(function () use ($request) {
$order = Order::create([
'total_payment' => 0,
'user_id' => $request->user()->id
]);
foreach ($request->carts as $cart) {
foreach ($cart['books'] as $book) {
$order->library_id = $book['library_id'];
$order->save();
$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']
]);
}
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);
}
}
what i want, but let's say the Library A have books A,B and Library B have books A,B. When the user order from library A the books A,B it should be in one order even he also at the same time order from Library B the books A,B would be another order. This is what i get when i create orders https://pasteboard.co/GXhllDhyQa6f.png I want to group the order by the library_id
will appropriate your help.
Level 2
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);
}
}
Thanks everyone.
Please or to participate in this conversation.