Skip to content

Instantly share code, notes, and snippets.

@tulik
Created January 13, 2018 18:12
Show Gist options
  • Save tulik/54b234352af9a6aa8abe502718d1c339 to your computer and use it in GitHub Desktop.
Save tulik/54b234352af9a6aa8abe502718d1c339 to your computer and use it in GitHub Desktop.
public function vendors(): Collection
{
return $this->belongsToMany(Vendor::class);
}
public function index(): Response
{
$products = Product::with('vendors')->get();
if (!$products) {
$productsNotFoundMessage = 'Nie znaleziono produktów';
return view('products.index', compact('productsNotFoundMessage'));
}
return view('products.index', compact('products', 'productWithoutSuppliers'));
}
view:
{{ productstNotFoundMessage }} // Not sure if it will interpolate it as ''
@foreach($product->vendors as $vendor)
{{ $vendor->name ?: 'Nie znaleziono dostawców dla tego produktu' }}
@endforeach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment