Postingan lainnya
Cara Mengupdate Nilai pada Session menggunakan form input
Selamat sore, aku ada pertanyaan nih kak, gimana ya cara update nilai pada session? saya sudah berhasil membuat add to cart, dan menampilkan cart, sekarang saya mau belajar bagaimana cara update nilai pada session melalui form input, tampilan update cart nya begini kak : Rencananya saya mau buat shopping cart, mohon bantuannya kak.. Terimakasih :)
App\Cart.php
<?php
namespace App;
class Cart
{
public $items = null;
public $totalQty = 0;
public $totalHarga = 0;
public function __construct($oldCart)
{
if ($oldCart) {
$this->items = $oldCart->items;
$this->totalQty = $oldCart->totalQty;
$this->totalHarga = $oldCart->totalHarga;
}
}
public function add($item, $id)
{
$storedItem = ['qty' => 0, 'harga' => $item->harga, 'item' => $item];
if ($this->items) {
if (array_key_exists($id, $this->items)) {
$storedItem = $this->items[$id];
}
}
$storedItem['qty']++;
$storedItem['harga'] = $item->harga * $storedItem['qty'];
$this->items[$id] = $storedItem;
$this->totalQty += $storedItem['qty'];
$this->totalHarga += $item->harga;
}
}
ProductsController.php
public function addToCart(Request $request, $id)
{
$product = Products::find($id);
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id);
$request->session()->put('cart', $cart);
return redirect()->to('/');
}
public function Cart()
{
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
return view('shop.cart')->with(['products' => $cart->items,'totalHarga' => $cart->totalHarga ]);
}
public function updateCart(Request $request, $id)
{
//
}
cart.blade.php
@foreach($products as $data)
<tr>
<td>{{$data['item']['nama_product'] }}</td>
<td><img src="{{ asset('images/'.$data['item']['pict']) }}" width="100"></td>
<td>{{$data['item']['harga'] }}</td>
<td>
<form method="post" action="{{ route('product.updatecart', $data['item']['id']) }}">
{{csrf_field()}}
<div class="form-group" style="width:75px">
<input class="form-control" type="number" value="{{ $data['qty'] }}">
</div>
<div class="form-group">
<button class="btn btn-success">Update</button>
</div>
</form>
</td>
<td>
<a href="#" onclick="return confirm('beneran?')"><button class="btn btn-danger">Hapus</button></a>
</td>
</tr>
@endforeach
web.php
Route::get('/cart','ProductsController@Cart')->name('product.cart');
Route::get('/add-to-cart/{id}','ProductsController@addToCart')->name('product.addtocart');
Route::post('/update-cart/{id}','ProductsController@updateCart')->name('product.updatecart');
3 Jawaban:
itu kok dibawah namespace App gak ada repository nya dah? gmn mau kerubah nilai jumlahnya, contoh:
namespace App; use App\Repositories\cartRepository;
public function __construct(cartRepository $cartRepo){
$this->cartRepository = $cartRepo;
}
iya gan, saya mau buat shopping cart tanpa adanya package/repository tambahan, ane mau buat secara manual dari awal dengan laravel. aku pingin ngembangin dengan adanya update yg dilakukan oleh user, seperti kebanyakan ecommerce lain
ohh gak pake repository, tapi kan repository berperan penting juga di laravel.