Commit 94b0a557 authored by Artem's avatar Artem

add store order module

parent 0bfb296e
......@@ -4,7 +4,6 @@ namespace App;
//use Illuminate\Database\Eloquent\Model;
use App\Product;
use DB;
class Cart /*extends Model*/
{
......
<?php
namespace App\Http\Controllers;
use App\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Session;
class OrderController extends Controller
{
function store()
{
if (!Session::has('cart')) {
return redirect('cart');
}
$cart = Session::get('cart');
$order = Order::create([
'user_id' => Auth::id(),
'total_price' => $cart->total_price,
'total_qty' => $cart->total_qty,
]);
$order_id = $order->id;
Session::forget('cart');
return view('cart', compact('order_id'));
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
protected $fillable = ['user_id', 'total_price', 'total_qty'];
public function user()
{
return $this->belongsTo(User::class);
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('total_price');
$table->string('total_qty');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
}
<?php
use Illuminate\Database\Seeder;
use App\User;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
User::create([
'name' => 'Artem',
'email' => 'ololo@info.com',
'email_verified_at' => now(),
'password' => '123123123',
'remember_token' => Str::random(10),
]);
}
}
......@@ -72,7 +72,11 @@
</table>
@else
<div>There are no products in the cart. <a href="{{ route('homepage') }}">Check our products</a></div>
@if(isset($order_id))
<div class="alert alert-success">Thank you, order succesfully placed. Your order number is <b>{{ $order_id }}</b>. We will contact you shortly.</div>
@else
<div>There are no products in the cart. <a href="{{ route('homepage') }}">Check our products</a></div>
@endif
@endif
</div>
......
......@@ -36,5 +36,7 @@ Route::prefix('cart')->group(function() {
Route::post('/remove/{product}', 'CartController@removeFromCart')->name('cart.removeFromCart');
Route::post('/clear', 'CartController@clearCart')->name('cart.clearCart');
});
//
Route::redirect('order', '/'); // TODO to ask if there is a way to show 404 and not use redirect
Route::post('order', 'OrderController@store')->name('order.sendOrder')->middleware('auth');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment