我使用的是框架Laravel 8和Livewire
我想从Darryldecode cart制作cart,并且我的phpmysql中有(菜单(表,当我想要点击"时;添加购物车";,我收到错误信息:
SQLSTATE[42S22]:未找到列:1054中的未知列"menus.id"'where子句'(SQL:从
menus
中选择*,其中menus
.id
=5限制1(
迁移菜单
public function up()
{
Schema::create('menus', function (Blueprint $table) {
$table->id('id_menu');
$table->string('menu_name');
$table->text('description');
$table->integer('price');
$table->integer('quantity');
$table->string('menu_image');
$table->timestamps();
});
}
购物车视图
<section class="fullheight">
<div class="row mx-3">
<div class="col-md-8">
<div class="lqd-column col-xs-12 mb-20">
<div class="liquid-filter-items justify-content-between">
<div class="liquid-filter-items-inner pt-4">
<div class="form-group">
<input type="text" wire:model="search" class="form-control bg-athens-gray px-4" placeholder="Search Foods or Drinks">
</div>
<ul class="filter-list text-uppercase ltr-sp-1 filter-list-inline size-sm font-weight-bold py-4 mb-0">
<li class="active" data-filter="*"><span>All Categories</span></li>
<li data-filter=".Foods"><span>Foods</span></li>
<li data-filter=".Drinks"><span>Drinks</span></li>
</ul>
</div>
</div>
</div>
@foreach($data_menu as $dm)
<div class="lqd-column text-center col-md-3 col-sm-6 masonry-item '.$tm->kategori.'">
<article class="liquid-lp mb-40">
<figure class="liquid-lp-media">
<img src="{{ asset('storage/images/' . $dm->menu_image) }}" alt="{{ $dm->menu_name }}">
</figure>
<header class="liquid-lp-header">
<h2 class="liquid-lp-title text-center h4 font-size-16">{{ $dm->menu_name }}</h2>
<h2 class="liquid-lp-title text-center h4 font-size-16 pt-2">Rp. {{ number_format($dm->price) }}</h2>
</header>
<footer class="liquid-lp-footer">
<div class="btn-cart">
<div wire:click="tambah_cart({{ $dm->id_menu }})" class="btn btn-default circle btn-bordered text-uppercase font-weight-bold font-size-12 border-thin lh-15 mb-2">
<span>
<span class="btn-txt">Add Cart</span>
</span>
</div>
</div>
</footer>
</article>
</div>
@endforeach
</div>
<div class="col-md-4">
<div class="card card-shadow">
<div class="card-body px-5">
<h2 class="">Cart</h2>
<div class="fancy-box fancy-box-offer fancy-box-offer-header">
<div class="fancy-box-cell fancy-box-header" style="flex-grow: 1;">
<p>Menu Name</p>
</div>
<div class="fancy-box-cell" style="flex-grow: 0.5;">
<p>Qty</p>
</div>
<div class="fancy-box-cell">
<p>Price</p>
</div>
<div class="fancy-box-cell">
<p> </p>
</div>
</div>
@forelse($data_cart as $dc)
<div class="fancy-box fancy-box-offer fancy-box-heading-sm">
<div class="fancy-box-cell fancy-box-header" style="flex-grow: 1;">
<h3>
{{ $dc['name'] }}
</h3>
</div>
<div class="fancy-box-cell" data-text="Quantity" style="flex-grow: 0.5;">
<h5>
{{ $dc['qty'] }}
</h5>
</div>
<div class="fancy-box-cell" data-text="Price">
<h5>
Rp. {{ $dc['price'] }}
</h5>
</div>
<div class="fancy-box-cell" data-text="Availability">
<a href="" class="btn btn-sm circle btn-bordered text-uppercase font-weight-bold lh-15">
<span>
<span class="btn-txt">
<svg class="bi bi-trash" width="1em" height="1em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/>
<path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4L4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/>
</svg>
</span>
</span>
</a>
</div>
</div>
@empty
<div class="fancy-box-cell fancy-box-header">
<p>Cart is empty</p>
</div>
@endforelse
</div>
</div>
</div>
</div>
推车控制器
<?php
namespace AppHttpLivewire;
use LivewireComponent;
use AppModelsMenu as MenuModel;
use CarbonCarbon;
class Cart extends Component
{
public $tax = "0%";
public function render()
{
$data_menu = MenuModel::orderBy('created_at', 'DESC')->get();
$condition = new DarryldecodeCartCartCondition([
'name' => 'pajak',
'type' => 'tax',
'target' => 'total',
'value' => $this->tax,
'order' => 1
]);
Cart::session(Auth()->id())->condition($condition);
$items = Cart::session(Auth()->id())->getContent()->sortBy(function ($cart) {
return $cart->attributes->get('added_at');
});
if(Cart::isEmpty()) {
$cartData = [];
} else {
foreach ($items as $item) {
$cart[] = [
'rowId' => $item->id_menu,
'name' => $item->menu_name,
'qty' => $item->quantity,
'pricesingle' => $item->price,
'price' => $item->getPriceSum(),
];
}
$cartData = collect($cart);
}
$sub_total = Cart::session(Auth()->id())->getSubTotal();
$total = Cart::session(Auth()->id())->getTotal();
$newCondition = Cart::session(Auth()->id())->getCondition('pajak');
$pajak = $newCondition->getCalculatedValue($sub_total);
$summary = [
'sub_total' => $sub_total,
'pajak' => $pajak,
'total' => $total
];
return view('livewire.cart', [
'data_menu' => $data_menu,
'data_cart' => $cartData,
'summary' => $summary
]);
}
public function tambah_cart($id) {
$rowId = "Cart".$id;
$cart = Cart::session(Auth()->id())->getContent();
$cekItemId = $cart->whereIn('id_menu', $rowId);
if($cekItemId->isNotEmpty()) {
Cart::session(Auth()->id())->update($rowId, [
'quantity' => [
'relative' => true,
'value' => 1
]
]);
} else {
$data_menus = MenuModel::find($id);
Cart::session(Auth()->id())->add([
'id' => "Cart".$data_menus->id_menu,
'name' => $data_menus->menu_name,
'price' => $data_menus->price,
'quantity' => 1,
'attributes' => [
'added_at' => Carbon::now()
],
]);
}
}
}
菜单型号
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Menu extends Model
{
protected $table = 'menus';
protected $guarded = []; // Bisa di settings kolom mana saja yang mau di baca
use HasFactory;
}
Web路由
<?php
use IlluminateSupportFacadesRoute;
use AppHttpLivewireMenu;
use AppHttpLivewireCart;
Route::get('/', function () {
return view('index');
});
Auth::routes();
Route::group(['middleware' => ['auth']], function() {
Route::get('/menu', Menu::class);
Route::get('/cart', Cart::class);
Route::get('/home', [AppHttpControllersHomeController::class, 'index'])->name('home');
});
模型主键的默认名称为"id",但您的菜单表(id_menu(中有不同的主键。您应该在模型中指定主键:
protected $primaryKey = 'id_menu';