Did you know about view()->exists() function?

When you load a View from the Controller, there could be a situation when you don’t actually know which view to load – the name could be dynamic. For example, you would use different Blade templates for sending emails. There is a neat function to assure your chosen View file actually exists.

It goes as simple as this:

if (view()->exists('emails.' . $template))
{
    // ... sending an email to the customer
}

I personally would consider this approach a bad practice – you should catch that error in Controller or elsewhere in the logic before actually calling the View. But if for some reason you get to this point – function exists() can be handy.

More info about Views – in the official documentation.

Like our articles?
Check out our Laravel online courses!

1 COMMENT

  1. Nice! Also, if you want to see whether a view has been fired up without using ::composer, you can echo the view and see the HTML or if you need to use it as a parameter of some function you just put it in a variable like this $html = view(‘view_name’, $data);

LEAVE A REPLY

Please enter your comment!
Please enter your name here