Please help me. I have these 3 tables, and I have problems on how to call the category name based on the restaurant id from Controller to view. Thank you in advance.
Table items
Table categories
Table restorant
This is my Controller
public function index()
{
if (auth()->user()->hasRole('owner')) {
$items = Items::with('category')->get();
$restorant_id = auth()->user()->restorant->id;
$category = Categories::where(['restorant_id' => $restorant_id]);
}
return view('point-of-sale::index', [
'category' => $category,
'items' => $items,
]);
}
If you add get() to the end of your $category = Categories::where(['restorant_id' => $restorant_id]); statement, you'll have an Eloquent collection returned:
$category = Categories::where(['restorant_id' => $restorant_id])->get();
Pass the $category variable to your view as you are currently, consider renaming it to $categories though just to infer that there could be multiple.
Then in your view you can loop over the Category results and access the name property:
#forelse ($category as $cat)
{{ $cat->name }}
#empty
No categories.
#endforelse
Update
If you want to get items by their category_id, you can either do as you have done with $categories:
$items = Items::where(['category_id' => $category_id])->get();
Alternatively, if you have an items relationship on your Categories model you can access them that way:
$category = Categories::with('items')
->where(['restorant_id' => $restorant_id])
->get();
The above will eager load the items related to a category which you can then access in your view, for example:
#forelse ($category as $cat)
{{ $cat->name }}
#foreach ($cat->items as $item)
{{ $item->name }}
#endforeach
#empty
No categories.
#endforelse
Related
I have ShopController . Now I am displaying products in random order from database.
Here is the code
$productsLike = Product::with('categories')->where('slug', '!=', $slug)->inRandomOrder()->take(4)->get();
What i should do to display products related to the same category as current product?
I would make a local scope on your Product model to make things reusable.
public function scopeRelatedProducts($query, $count = 10, $inRandomOrder = true)
{
$query = $query->where('category_id', $this->category_id)
->where('slug' '!=' $this->slug);
if ($inRandomOrder) {
$query->inRandomOrder();
}
return $query->take($count);
}
Then you could do something like:
$related = Product::relatedProducts(4, true)->with('categories')->get();
This assumes the column your Product table stores the relationship to a Category follows the Laravel naming convention (e.g. RelatedModelName_id).
You could also replace the slug_id with the Product id
You can then use the resulting $related variable in your view, so assuming your using the above in a Product show controller method, you might do:
class ProductController
{
public function show (Product $product)
{
$related = $product->relatedProducts(4, true)->with('categories')->get();
return view('products.show', compact('product', 'related');
}
}
#foreach ($related as $item)
<h5>{{ $item->name }}</h5>
<p>{{ $item->description }}</p>
#endforeach
You can check with category id.
$productsLike = Product::with('categories')
->where('category_id_column', $category_id)
->where('slug', '!=', $slug)
->inRandomOrder()->take(4)->get();
I have 3 Models "Category" (tableName = 'categories'), "Brand" ('brands'), "Item" ('items')
Can I pass parent column value (e.g. categories) to use it in the Where clause?
Simple example:
Category::with(['brands' => function ($q) {
$q->with(['items' => function ($query) use ($localWhereHas) {
$query->whereColumn('category_id', 'categories.id');
}]);
}]);
Relations are:
Category Model:
public function brands(): BelongsToMany
{
return $this->belongsToMany(Brand::class, 'category_brands')->withPivot('is_visible', 'addon_price');
}
Brand Model:
public function items(): HasMany
{
return $this->hasMany(Item::class, 'brand_id');
}
Item Model:
public function category(): BelongsTo
{
return $this->belongsTo(Category::class, "category_id");
}
The main idea is that I need the structure to be nested like this Category->Brands->Items
The backup plan is to fetch All needed Categories and Foreach them to get as many as categories I have all Brands->Items, using categoryId ($category->category_id) .... but I don't like it
No, you can't pass it directly because it is not the same context.
If you want to have a display like this :
Category
-brand
-item
Then you should maybe just group your items by brand :
$categories = Category::with('items.brand')->get();
#foreach($categories as $category)
{{ $category->name}}
#foreach($category->items->groupBy('brand_id') as $brand_id => $items)
{{ $items->first()->brand->name }}
#foreach($items as $item)
{{ $item->name }}
#endforeach
#endforeach
#endforeach
how can I list a musician of a certain category in my blade file with an if?
If you just want to list or show musicians of a particular category on the view then you can filter the musicians in the controller method and then send the filtered data to view
public function group()
{
return view('pages.group', [
'musicians' => Musician::where('category_id', 10)->get()
]);
}
And then in blade view
#foreach($musicians as $musician)
<p>{{ $musician->musician_name }} {{ $musician->musician_surname }}</p>
#endforeach
it works thanks. but if I wanted to print on one line the musicians of the category with id 10 and on another line the musicians of the category with id 40? always on the same blade file
public function group()
{
return view('musicians', [
'categories' => Musician::get()->groupBy(function($item) {
return $item->category_id;
})
]);
}
#foreach($categories as $catetory)
#foreach($category as $key => $musician)
<p>{{ $musician->musician_name }} {{ $musician->musician_surname }}</p>
#endforeach
#endforeach
I am working on an E-commerce application, I want to retrieve an order together with it's associated products but I couldn't get pass that error, I don't know what am missing there. Thanks for help. Here is my code.
public function show(Order $order, Request $request)
{
$order = Order::where('id', $request->order->id)->first();
$products = $order->products();
// dd($products);
return view('orders.order')->with([
'products' => $products
]);
}
And if I dd($products) I got null
This is my views
#foreach ($products as $product)
<li class="">
{{ $product->id }}
</li>
#endforeach
Assuming that products() is a relationship in your Order model, $order->products() will return an instance of Builder.
To get the products, you can simply do:
$order->products
I'm trying to update a notification's data (for displaying it in a view):
$notifications = $user->notifications()->paginate(15);
foreach ($notifications as $notification)
{
$post = Post::where('id', $notification->data['post_id'])
->first();
$notification->data = $post;
}
But when I do this in my view:
#foreach ($notifications as $notification)
{{ gettype($notification->data) }}
#endforeach
It shows that the data is an array. Why does it convert the Post model object to an array and how can I stop this from happening?
Your $notification object is an instance of Illuminate\Notifications\DatabaseNotification. This is an Eloquent Model that has the following $casts property defined:
protected $casts = [
'data' => 'array',
'read_at' => 'datetime',
];
Because of this, when you access $notification->data, it will automatically be cast to an array.
To resolve your issue, and to reduce the number of database queries, you should just build a separate lookup collection of posts, and pass that into the view.
First, build an array of all the Post ids. Then, run one query to get all the Post|s for those ids. After that, re-key the resulting Collection of Post|s by the id for easy lookup later in the view. Make sure to pass your Collection of Post|s to the view.
$notifications = $user->notifications()->paginate(15);
$postIds = [];
foreach ($notifications as $notification) {
$postIds[] = $notification->data['post_id'];
}
$posts = Post::whereIn('id', array_unique($postIds))->get()->keyBy('id');
Then, in your view:
#foreach ($notifications as $notification)
{{ $posts->get($notification->data['post_id']) }}
#endforeach
You were not updating in $notifications that's why you are seeing old array not the new object you wanted to save.
foreach ($notifications as $key => $notification)
{
$post = Post::where('id', $notification->data['post_id'])->first();
$notifications[$key]->data = $post;
}