All Posts By

Kunal Gautam

Getting Started – Passing arguments to view in Laravel 5.3

By | Laravel

An application is incomplete if it is not dynamic. Although some people may prefer to use Laravel for fetching static files, but in my opinion fetching static files would be real wastage of resources, be it space or server memory.

So to make dynamic application, we have to pass variables, arguments or objects to the files. Let see in this tutorial how to pass them in Laravel.

To understand this, we will create a new route called users in our routes file and call the users blade template. We will pass JSON objects to the blade template file and try to retrieve it.

Open routes/web.php file and add the following code.

 

In above example we have created a json object and assigned it to variable called users. In view method, we have passed that variable using compact() function, the compact() function create array from users variable and pass the array as same name.

Now we will create a blade template files at resources/views/users.blade.php with following lines

You can refer to our tutorial on for loop to understand how loops works in blade template.

Save all the files and you will see the passed arguments successfully.

You can return multiple arrays to the view. to do that just seperate them in compact function using “,” comma.

For example

return view('users', compact('users', 'variable1', 'variable2'));

Now there is always multiple way to program an application. Some users may feel not using compact function. the can follow following to get the work done:

return view('users')->with('users', $users);

In above example an array with name users is created that will be passed from users variable. For multiple arguments:

return view('users')->with('users', $users)->with('var1', $var1)->with('var2', $var2);

Working With If Else in Laravel 5.3 using Blade

By | Laravel

As we have seen in earlier tutorial about Loops, let see how to use if statements in Blade Template engine.

If Else Condition

In above code, we have checked if $var is empty, if it is empty output It is empty, else output It is not empty. if condition can also be used for comparison. For example:

Unless statement

Unless is generally “if not” condition. For example:

In this example if $var is empty, it will not process this loop and exit.

Blade Template – Beginners level tips for Laravel 5.3

By | Laravel

As we have seen in previous tutorial we have echoed date. In this tutorial we will learn some more advance stuffs in blade template engine

Now the above example can be further simplified using only following code :

{{  echo date('l jS \of F Y h:i:s A'); }}

The curly brackets used are template shortcut for echo function. But double curly brackets are used to prevent the code from XSS attacks by stripping HTML entities. To overcome this we can use

{!!  echo date(‘l jS \of F Y h:i:s A’);  !!}

 

Lets take an example to understand this. If you pass data from your controller to view with some styling like as
$var = "<b>Hello</b>";
and if it is accessed within blade with {{ $var }}

then the output’ll be
<b>Hello</b>
and if it is accessed within blade with {!! $var !!}

then the output’ll be

Hello

Now the question arises, what if I have to use {{ $var }} in my code, for eg. in angular js, variables are called that way only, how to do it. It is pretty simple: use:

@{!! $var !!}

In case you want to comment anything in your blade file, it can be done as following:

{{ -- This is comment, and will not be rendered in HTML output --}} 

In starting you might feel little bit awkward to use these new things, but in long run it not only makes code look more cleaner, but also very easy to understand. In next tutorial we will see how to use for loop