Hi.
The best practice to send this to the view is to try to get as many of these properties with scopes or methods on your models I think. This way you can pass through the object to the view and call methods on them within blade to retrieve this information. This keeps your controller nice and clean while keeping the logic related to the model where it should be.
Also, you should set up relationships so that you can retrieve other related objects. This way you limit the data you need to pass in the view to your controller.
Take newest product for example:
On Product Model:
public function category
{
return $this->belongsTo(Category::class);
}
public function scopeLatestFirst($query, $categoryId)
{
return $query->where('category_id', $categoryId)-> orderBy('created_at', 'desc')->get();
}
In controller:
public function index()
{
$products = Product::latestFirst('insert wanted categoryid');
return view('products.someview', compact('products'));
}
In view:
@foreach ($products as $product)
<h1>{{ $product->title }}</h1>
<h2> {{ $product->category->title }}</h2>
@endforeach