I have created a wishlist for a shopping cart.So when a non authenticate user wants to see his wishlist after he added to the wishlist. How to show the wishlist based on him before he logged in?
This is my wishlist i have made for only authenticate users:
public function addWish(Request $request)
{
if(Auth::check()){
$name = $request->name;
$product = Product::where('name' , '=', $name)->first();
$product_id = $product->id;
$product = DB::table('wishes')
->where('wishes.product_id','=',$product_id)
->where('wishes.status','=',1)
->select('wishes.product_id')
->first();
if(!$product){
$wish = new Wish();
$wish->user_id =Auth::user()->id;
$name = $request->name;
$product = Product::where('name' , '=', $name)->first();
$wish->product_id = $product->id;
// $product = Product::find($cart->product_id);
$wish->price =$product->price;
$wish->status = 1;
$wish->save();
return redirect('shop-wish');
}
else{
return redirect('shop-wish');
}
}
And this one is for show the list :
public function getWishPage()
{
$id = Auth::user()->id;
$wishList = \DB::table('wishes')
->join('products','wishes.product_id','products.id')
->select('products.feature_image','products.name','products.price as p_price','wishes.id')
->where('wishes.status','=',1)
->where('wishes.user_id','=',$id)
->get();
return view('cart.wishlist',compact('wishList'));
}
But how do i show the non-authenticate users wishlist? Any suggestion or solution would be appreciable?
public function addWish(Request $request)
{
if (Auth::check()){
// ...
} else {
$name = $request->name;
$product = Product::where('name' , '=', $name)->first();
$product_id = $product->id;
if (\Session::has("wishList.$product_id") === true) {
return redirect('shop-wish');
}
\Session::put("wishList.$product_id", $product_id);
return redirect('shop-wish');
}
}
and:
public function getWishPage()
{
if (Auth::check()) {
$wishListId = \Session::get('wishList');
dd($wishListId);
} else {
$id = Auth::user()->id;
$wishList = \DB::table('wishes')
->join('products','wishes.product_id','products.id')
->select('products.feature_image','products.name','products.price as p_price','wishes.id')
->where('wishes.status','=',1)
->where('wishes.user_id','=',$id)
->get();
return view('cart.wishlist',compact('wishList'));
}
}
Related
i am trying to make an add to cart button but there is an error that is happening in this line if (Cart::where('prod_id', $product_id)->where('user_id', Auth::id()->exists())) {
and the error is: Expected type 'object'. Found 'int|string|null'. it is from the Auth::id in the course that i am taking it is not making any error and it is working fine
class CartController extends Controller
{
public function addProduct(Request $request)
{
$product_id = $request->input('product_id');
$product_qty = $request->input('product_qty');
if (Auth::check()) {
$prod_check = Product::where('id', $product_id)->first();
if ($prod_check) {
if (Cart::where('prod_id', $product_id)->where('user_id', Auth::id()->exists())) {
return response()->json(['status' => $prod_check->name . ' Already Added to Cart']);
} else {
$cartitem = new Cart();
$cartitem->prod_id = $product_id;
$cartitem->user_id = Auth::id();
$cartitem->prod_qty = $product_qty;
$cartitem->save();
return response()->json(['status' => $prod_check->name . ' Added to Cart']);
}
}
} else return response()->json(['status' => 'Login to Continue']);
}
}
You are mixing your parenthesis in ->where('user_id', Auth::id()->exists()) section. exists should be outer side of queryBuilder. You should call ->where('user_id', Auth::id()) after that you should chain ->exists().
if (Cart::where('prod_id', $product_id)->where('user_id', Auth::id())->exists()) {
I create order system in Laravel, I can add new order as user and I can display all order as admin user. I have only problem with edit this order as user.
This is my controller
public function index(){
$orders = Orders::with('category', 'type')->where('user_id', auth()->user()->id)->orderBy('id', 'desc')->paginate(6);
return view('account.orders', compact('orders'));
}
public function store(Request $request) {
$order = new Orders;
$order->user_id = auth()->user()->id;
$order->category_id = $request->input('category_id');
$order->type_id = $request->input('type_id');
$order->name = $request->input('name');
$order->description = $request->input('description');
$order->price = $request->input('price');
$order->save();
return back()->with([
'status'=> [
'type'=>'success',
'content'=>'Zmiany zostaĆy zapisane',
]
]);
}
public function edit(Orders $order_id){
$categories = Categories::get();
$types = OrdersCategories::get();
$order = Orders::where('user_id', auth()->user()->id)->findOrFail($order_id);
return dd($order_id);
}
When I click edit in user dashboard not show data from database.
Try This,in your controller edit function.
try {
$categories = Categories::get();
$types = OrdersCategories::get();
$order = Orders::where('user_id', auth()->user()->id)
->findOrFail($order_id);
return redirect()->action('Admin\OrderController#index');
} catch (\Exception $exception) {
return back()->withError($exception->getMessage())->withInput();
}
Because your model is called Orders. You should be using /{orders}/ instead of /{order}/ in your routes if you want to make use of route-model binding.
Change the route to Route::get('/{orders}/edit', 'OrderController#edit')->name('edit');
I have controller
public function category(Categories $category)
{
$product_random = DB::table('products')->where('is_active', 1);
$currency = DB::table('currency')->where('id', 1)->get();
if(count($category->children))
{
$categories = Categories::where('parent_id', $category->id)->get();
return view('catalog2', compact('category', 'categories', 'product_random', 'currency', 'category_id'));
}
else
{
$categories = Categories::where('parent_id', null)->get();
$categories_pod = Categories::where('parent_id', $category->id)->get();
$product_s = Product::where('is_active', 1)->get();
$product = product::where('category_id', $category->id)->where('is_active', 1)->get();
if($price = request('individualprice'))
{
$product->orderBy('individualprice', $price);
}
return view('products', compact('category', 'product', 'categories', 'currency', 'product_s','categories_pod'));
}
}
and BLADE
<div>Sort by price descending</div>
<div>Sort by price</div>
I'm creating a wishlist where the user can save his own products and save them. What I want is that a user can't save a product more than one time.
So, what I'm doing is, if the product_id exists in the DB, I don't want to save the new record.
This is my code, what am I doing wrong?
public function addWishlist(Wishlist $wishlist) {
$wishlist->user_id = request('user_id');
$wishlist->product_id = request('product_id');
if(DB::table('wishlists')->where('product_id' !== $wishlist->product_id)) {
$wishlist->save();
return redirect()->back();
}
}
what about this
public function addWishlist(Wishlist $wishlist) {
$userId = request('user_id');
$productId = request('product_id');
$found = Wishlist::where('user_id', $userId)->where('product_id', $productId)->count();
if($found == 0) {
$wishlist->user_id = $userId;
$wishlist->product_id = $productId;
$wishlist->save();
return redirect()->back();
}
}
public function addWishlist(Wishlist $wishlist) {
if(DB::table('wishlists')->where(['user_id'=>$user_id,'product_id'=>$wishlist->product_id])->exists()){
// record already exist
return redirect()->back();
}else{
// record not exist , save the record
$withlist = new Wishlist();
$wishlist->user_id = request('user_id');
$wishlist->product_id = request('product_id');
$wishlist->save();
return redirect()->back();
}
}
You can use firstOrNew and you need to check with user_id and product_id combination
$wishlist::firstOrNew(array('user_id' => request('user_id'),'product_id'=>request('product_id')));
$wishlist->user_id = request('user_id');
$wishlist->product_id = request('product_id');
$wishlist->save();
return redirect()->back();
Have you tried firstOrCreate()
$user = User::firstOrCreate(['email' => $email])
I want to add a product to my cart But for that i want to logged in the user.Means after he logged in only he can see his cart. But after logged in if he sees the cart, he should see the the product just he added to cart but he saw the old one.Though I put the url on session in controller.How do i make it correct?
Here is show Login form :
public function showLoginForm()
{
Session::put('url.intended',\URL::previous());
return view('cart.login');
}
Add to Cart controller :
public function addCart(Request $request)
{
if(Auth::check())
{
$name = $request->name;
$product = Product::where('name' , '=', $name)->first();
$product_id = $product->id;
$product = DB::table('carts')
->where('carts.product_id','=',$product_id)
->where('carts.status','=',1)
->select('carts.product_id')
->first();
if(!$product){
$cart = new Cart();
$checkBox = Input::get('additionals');
if (is_array($checkBox)){
$cart->additionals = array_sum($checkBox);
}
else {
$cart->additionals =0;
}
$cart->user_id =Auth::user()->id;
$name = $request->name;
$product = Product::where('name' , '=', $name)->first();
$cart->product_id = $product->id;
// $product = Product::find($cart->product_id);
$cart->price =$product->price;
$cart->status = 1;
$cart->save();
return redirect('shop-cart');
}
else {
return redirect('shop-cart');
}
}
else{
return redirect('/login');
}
}
And Login controller :
protected function login(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
if (auth()->attempt(array('email' => $request->input('email'),
'password' => $request->input('password'))))
{
if(auth()->user()->is_activated == '0'){
Auth::logout();
return back()->with('warning',"First please active your account.");
}
else{
return Redirect::to(Session::get('url.intended'));
}
}
else{
return back()->with('error','your username and password are wrong.');
}
}
Add to Cart Route:
Route::get('add-to-cart/{name}','CartController#addCart');
public function addcart(Request $request)
{
$id=$request->id; $time=60*24*14; /*60 * 24 * 14 = 14 drays 60=minutes 24=hours 14=days*/
$value=0;
if( Cookie::get('cart')!==null ){
$anonim=Cookie::get('cart');
DB::table("cart")->insert(["anonim"=>$anonim,"product_id"=>$id]);
return 0;
}else{
$value=DB::table("cart")->max("anonim")+1;
if(empty($value)){
$value=0;
}
DB::table("cart")->insert(["anonim"=>$value,"product_id"=>$id]);
$cookie = cookie('cart', $value, $time);
return response()->cookie($cookie);
}
}