this is query
$product = Product::with('categories')->take(5)->get();
I can't access relation on image
thank for answer
// for clarity name this plural as this is a collection too
$products = Product::with('categories')->take(5)->get();
// then in your view:
#foreach ($products as $product)
$product->categories; // accessible like that
Related
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
I have some trouble with pagination. I need to take only one category with movies and paginate it. Now I write some code, but I don't think it's optimized.
$category = Category::with(['movies' => function ($query) {
$query->orderBy('id', 'desc')->paginate(18);
}])->where('slug', $slug)->first();
$catMoviesPaginate = $category->movies()->paginate(18);
You can do it with lazy loading as here I mention the code which can help to you.
$category = Category::where('slug', $slug)->first();
$movies= $category->movies()->paginate(18); //lazy loding.
return view('example', compact('category', 'movies'));
You can render the pagination in view file as well.
#foreach ($movies as $movie)
{{ $movie->id }}
#endforeach
{!! $movies->render() !!}
The most efficient way would be to create an inversed relation in movies for categories. Then you query the movies in X category by slug. You should end up with something like this
Movies::whereHas('categories', function($q) use($slug) {
$q->where('slug', $slug)
})->paginate(18);
You can take it a step further and create a scope in the Movies model to simplify your code. With the proper scope it can be as simple as Movies::inCategory($slug)->paginate(18)
I've found many question realated to my problem but couldn't found an answer yet. It's about my foreach loop in my blade.
I want to print all product-names in my blade but I couln't figure out how to do that.
thats how I'm getting the products:
--- current code:
// controller
$id_array = Input::get('id');
$products= Products::whereIn('id', $id_array)->get();
$product_name = [];
foreach($products as $arr)
{
$product_name= $arr->lists('name');
}
returning $product_name gives me this as a output:
["football","cola","idontknow","freshunicorn","dummy-data"]
In my blade is just a simple:
#foreach($products as $product)
{{ $product}}
#endforeach
Error: htmlentities() expects parameter 1 to be string, array given
Thanks for your help and time.
It seems you are getting an object in an array in an array.
Like this:
array(
array(
object
)
)
It happens because you use the get() function to retrieve you model. The get() function always "wants" to retrieve multiple models. Instead you will have to use the first() function.
Like this:
foreach($id_array as $arr)
{
$want2editarray[] = Product::where('id', $arr)->first();
}
Hope it helps :)
Edit after #Wellno comment
That's probably because Product::where('id', $arr)->first(); returns null because it did not find anything.
I forgot to add a check after the retrieving of the product.
This can be done like this:
foreach($id_array as $arr)
{
// First try to get model from database
$product = Product::where('id', $arr)->first();
// If $product insert into array
if ($product) $want2editarray[] = $product;
}
Why do you use loop with IDs? You can find all products by IDs:
$products = Product::whereIn('id', $id_array)->get();
And then use $products in the blade template
#foreach($products as $product)
{{ $product->name }}
#endforeach
try to use Model/Eloquent to fetch data.
View should only display the data and not fetching directly from DB or do heavy calculations.
I have this code to try to generate a report on all records in a table:
$items = Line::all();
$data = $items->toArray();
$pdf = PDF::loadView('pdf.catalogue', $data);
return $pdf->stream();
How do I access the array in my Blade template? I have tried
#foreach ($data as $item)
but that does not work.
How you usually pass these variables into blade is by a named array, meaning you would assign it as so.
$data['items'] = $items->toArray();
In your template you should have the variables $items containing the array.
#foreach($items as $item)
...
#endforeach
I'm new to MVC and do not understand it very much at all. I have a database with one table called products
I have a Model called Productwhich looks like
<?php
class Product extends Eloquent {
// MASS ASSIGNMENT -------------------------------------------------------
// define which attributes are mass assignable (for security)
// we only want these 3 attributes able to be filled
protected $fillable = array('type', 'brand', 'image');
}
I have a Route like so
Route::get('products', function()
{
return View::make('products');
});
And my View looks like this
#extends('master')
#section('content')
PRODUCTS
#foreach($products as $product)
{{ $product->type }}
#endforeach
#stop
I see you can get all the rows like so $products = Product::all(); but I don't understand WHERE this goes. Does it go in the View? Does it go in the Model? Does it go in the Route? My current #foreach loop just results in an undefined variable: products
Can someone please clarify?
This should get you started for now:
Route::get('products', function()
{
$products = Product::all();
return View::make('products')->with('products', $products);
});