Question: How can I run the where query for all rows in $cart, instead for just $cart[0], or [1] or [2].
I am trying to manually code a Shopping Cart in Laravel. Since I am a novice, I am stuck at a point. Please take a look at the code below:
public function showCart()
{
$user = Auth::user();
$cart = Cart::where('user_id', $user->id)->get();
$product = Products::where('id', $cart[0]->product_id)->get();
return view('showCart')
->with('cart', $cart)
->with('user', $user)
->with('product', $product);
}
This is my function to show a user's cart. Here, I am trying to show all the products that a user has in his cart, and also send a variable with the product details.
However, while I am trying to send all the products in the user's cart as a $product array, I am only getting the first product. That is because the rows returned in $cart is more than one, but I am unable to write a query for getting all the products in this line:
$product = Products::where('id', $cart[0]->product_id)->get();
. . . because, quite clearly, I am writing a query to match only the first row returned in $cart.
Thanks in Advance.
You can use the whereIn and the data_get helper function:
$product = Products::whereIn('id', data_get($cart, '*.product_id'))->get();
data_get works with both arrays and objects. Alternatively, you can use Arr::get for arrays only.
Related
I have controller with request like below
$product = $request->product; //array
$product_category = $request->product;
if($request->filled('product')){
$product = product()::whereIn('product',$product)
->where('product_category',$product_category)
->get();
}else {
$product = product()::where('product_category',$product_category)
->get();
}
how do i skip or make the eloquent still worked if $product request was not filled or null?
Currently i used the if for that query is remove where on product, but that will make me create alot of eloquent with more request
But I think this is not the best or right think to do, what is my better option for doing that where without writing a lot of if-elseing?
Use when function provided by laravel:
Product::when($request->product, function($query, $product) {
$query->whereIn($product);
})->where("product_category", $product_category)->get();
In Controller,
public function detail($id)
{
$item = Item::find($id);
return view('frontend.detail',compact('item'));
}
In blade,
{{$item->subcategory->items}} //question //this code print all the items//
You need to select the items manually if you don't want to fetch the id
Currently, you are fetching all the items by using $item = Item::find($id); it means that fetch all the fields of the particular table againt the given id.
you can use below query by modifying you required fields
Item::select('name','surname')->where('id', 1)->get();
You Need to make hidden id of the column in query Example
Item::find($id)->makeHidden(['id']);
return view('frontend.detail',compact('item'));
I'm having trouble with the whereIn() function.
I have two tables carts and items. Carts stores user IDs and item IDs, while the items table stores item IDs and item information.
What I would like to do is get all items that are in the cart table with in the signed in users ID.
Right now I'm first getting the item ID's matching the active user:
$IDs = Cart::select('item_id')->where('user_id', auth()->id());
Then I want to select all items with an ID in $IDs
$items = Item::all()->whereIn('id', function($query) {
$query->select('item_id')->from($IDs->item_id);
});
However when I try this, I get the error
Object of class Illuminate\Database\Query\Builder could not be converted to int.
When I replace $IDs with something like [1234, 4321], it works.
How can I fix this problem?
$IDs = Cart::select('item_id')->where('user_id', auth()->id());
this line return a query builder not a collection of ids ...
anyway you can get the ids in array and use it directly for whereIn :
$IDs = Cart::where('user_id', auth()->id())->pluck('item_id')->all();
$items = Item::whereIn('id',$IDs)->get();
today i was just practicing my skills and got stuck. I just want to fetch data from the database using specific ids which i put inside my session as an array. The problem is, i cant find a way to fetch all the data that includes these ids.
here's my cart checkout method
public function checkout(){
$uniqid = session('products');
$post=Post::find($uniqid);
return view('pages.checkout')->with('post',$post);
}
I am getting no data, even if i try to get something i only get one field when there's something like 7 of them.
Here's the session products array
0 "15be4aa3b55f10"
1 "15be4814ceb2a0"
2 "15be4814ceb2a0"
3 "15be4aa3b55f10"
4 "15be4aa3b55f10"
5 "15be4aa3b55f10"
6 "15be4aa3b55f10"
7 "15be4aa3b55f10"
The find query returns single row of data. If you want to fetch more than 1 data, use the all query:
$post = Post::all();
or if you want to get all data with the same id as your $uniqid, use the where query:
$post = Post::where('column_name', $uniqid)->get();
EDIT
IF your session has many id, you may want to loop and find all the data with the same id as your $uniqid
public function checkout(){
$products = [];
foreach(session('products') as $session){
$post=Post::find($session->id);
$products[] = $post;
}
return view('pages.checkout')->with('products ',$products);
}
you may try this:
public function checkout(){
$uniqid = session('products');
$products = Post::whereIn('unique_id', $uniqid)->get();
return view('pages.checkout')->with('products',$products);
}
where whereIn method takes a field name and array of values
From laravel docs:
The whereIn method verifies that a given column's value is contained within the given array
You should use whereIn() method to fetch records against multiple ids. do like this
public function checkout(){
$uniqidsArray= session('products');
$post=DB::table('yourTableName')
->whereIn('id', $uniqIdsArray)
->get();
return view('pages.checkout')->with('post',$post);
}
I have a persistent problem with all my project and I need help understanding how to make it work.
In my view =
URL = mysite.com/product/40 so here the product ID is 40
On the view I am doing a foreach loop to show all merchants that have this product. we have many merchants having many products so it is a many to many relationship.
Now on my controller
$product = Product::find($id); // to find the product
$users = $product->users; // here the user is the merchant that have the product
$store = Store::where('user_id', $user->id)->value('social');
Here i get the error :
Trying to get property of non-object
So I do want to access the store of each of the merchants in the controller how do I do this ? Because now $user is a collection.
Please first verify if the store is giving object or not by using var_dump. After that you can have a look into https://laravel.com/docs/5.6/queries for more details.
1) Firstly you can use Injection to avoid this line: $product = Product::find($id);
public function your_controller_methon(Product $product) {}
Laravel will automatically do the trick for you and $product will already contain Product object.
2) If you have relationship, you should do something similar to this:
$product->stores - to retrieve all stores which contains particular product in product_id column. And you could do: $product->stores()->pluck('social'); to retrieve list of socials from all merchants which have particular product.
About relationships you could read here: https://laravel.com/docs/5.7/eloquent-relationships
You can refactor your code to use the whereIn() query builder method since you have many users to a product. You will have something like:
$product = Product::find($id); // to find the product
$users = $product->users->pluck('id');
$stores = Store::whereIn('user_id', $users->all())->value('social');
This mean your $stores variable will contain those stores owned by the users.
PS: Be sure to check if $users is not empty or null so you don't encounter unexpected errors
According to you code, Here $user is a single value, not a collection.
Change:
$store = Store::where('user_id', $user->id)->value('social');
To
$store = Store::where('user_id', $user);
It will works.
To make the $user as a collection, execute such query so that it will return array such as:
$product = Product::find($id);
$user = Product::where('user', $product->user)->get();
This will return the collection of users of this product.
Then execute foreach loop:
foreach($user as $rowdata){
$store = Store::where('user_id', $rowdata->id)->get();
}
You should try this:
$product = Product::find($id);
$user = Product::where('user', $product->user)->get();
foreach($user as $rowdata){
$store = Store::where('user_id', $rowdata->id)->get();
}