All Posts By

Kunal Gautam

I wish I could tell you…

By | Editorial, Poem

I wish I could tell you, what’s going on in my mind…

I wish I could tell you…

A thousand things I want to have…. and a thousand things I want to do…
But as I get near to them… I feel that my reality isn’t even true.

I wish I could tell you the things going around.
I was just looking for simple happiness. Even that I could not found…

There are voices in my head, and I listen to music to shut them down.
But all I could listen are just noises… to calm your soul there aren’t enough sounds…

There are a million things I want to show and a million things I want to say.
I hope that God is looking down on us… and just to get enough time, I pray…

Not sure if there are enough words or there is enough time.
And to tell you all that…I’m just making this rhyme…

 

-Anurag Upadhaya

Controllers in Laravel 5.3

By | Laravel

As we have seen in previous example of passing arguments to view, the route file actually looks ugly and hard to read. As we know Laravel use MVC (Model View Controller) , it is best to use a controller for that example. In short it is best to include any logic in controller. A controller may have one or more than one action.

Lets understand controller with an example. To create an boilerplate code for an controller we have to pass following command in root of the project directory.

php artisan make:controller UsersController

The above command will create UsersController.php file under app/Http/Controllers/ directory with the required boilerplate.

Now we will delete the users route and replace this with following code:

By saving the file and running laravel development server, we will see the same output. This time logic are placed inside controller file. This not only make code readable, but very easy to understand. Consider scenario where there are lot of users related function such as create, delete etc. All logic will be placed inside this controller file under different function/method. And we will call required function in the routes file. Example is given below.

Route::post('users', 'UsersController@create');

Route::delete('users', 'UsersController@delete');

 

 

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);