Open Laboratory for Technocrats

Grab the developer role, learn concepts & prepare with senior software engineers to get solutions for problems in a software job. Coding and Programming Solutions crafted for you.

Laravel 5.8 - Blade - OR Operator - Solution

Laravel 5.8 - Blade - OR Operator
Laravel 5.8 - Blade - OR Operator 
OR operator in the PHP Laravel 5.7 blade template engine is deprecated.

On using this we get the undefined variable error on the development machine.

What OR operator used to do in BLADE?
OR operator used to save a few extra words of code on the developer machine.
Example
{{$testVariable or 'TestValue' }}
This will compile to
isset( $testVariable ) ?  $testVariable : 'TestValue';

What's the solution then?

Laravel 5.8 - Blade - OR Operator
Laravel 5.8 - Blade - OR Operator 

Now since Laravel 5.7, this OR operator has been deprecated we can get the same result with PHP's built-in ?? "null coalesce" operator

Example for the same as goes
{{$testVariable ??  'TestValue' }}
So now we can use this null coalesce operator to get the desired result which OR operator was giving in the Blade templating engine.

 Keep Coding and Keep Smiling :)

Laravel - Undefined Variable Event Dispatcher::fire()

Laravel - Undefined Variable Event Dispatcher::fire()
Laravel - Undefined Variable Event Dispatcher::fire() 
The problem arouses when we were upgrading one of the in house project to the latest version of Php framework Laravel 5.8

The error was clean as it looked
Call to undefined method Illuminate\Events\Dispatcher::fire() in Laravel 5.8
But we went into the documentation over the official website and figured out that this was decided to take down on the previous version of Laravel version.

Why this happened?
fire() function is an alias of the dispatch() method
What needs to fix this? 
Use the dispatch() method instead of fire() method
Example
Initial Code

Illuminate\Events\Dispatcher::fire()
Updated Code

Illuminate\Events\Dispatcher::dispatch()
Once you change the fire() method to dispatch() method that specific Undefined variable error will be gone.

Let me us know in comments if there is any issue with respect to this article.

Keep Coding and Keep Smiling :)


Laravel - env file not working - solution

Laravel - env file not working - solution
Laravel - env file not working - solution

I was playing around the Laravel which is a nice framework for PHP developers. It has awesome features. If you are a PHP developer you must try it.

Now let's dive into the problem which our developer faced while developing the application in this framework.

After developing the application we were on our testing server but our config files which is DOT ENV in our case was not loading the data which we have put it on the system.

We had config URLs, custom rules and another key which needs to be according to the services which we have used in our case.

The application is up and running on our server and can be accessed but the custom rules and config URLs are pointing to old env files WHY?

Reason for the same is Cache.

Laravel has cache system enabled which keeps the cache of various things that need to be updated or clear before you can check the new details. Even if you are working on the local system you need to clear the cache to reflect the changes in the application.

For so we need to use following commands on our CLI:

  • Config caching
For config files related issue we need to play around Config cache
php artisan config:cache
Above command will collect all the different config files to a single file and cache the details at single place
php artisan config:clear
Above command will clear the config cache and will update the cache with the new details in the config files

  • Routes caching
For routes related issue we need to play around Route cache
php artisan route:cache
Above command will collect all routes and place the cache of all the routes for faster URL mapping.
php artisan route:clear
Above command will clear the cache for routes on the application and reload the fresh details for the route cache

  • Classmap optimization
For Classmap optimization, we might need to do it with force
php artisan optimize --force

Keep Learning and Keep Coding :)


Laravel - Run Controller in Terminal

How to run a controller function via command line or terminal in Laravel?


Well, there are several ways to do it. Like making a command shell which will use the code to get in contact with the controller and then play around it.

Above method sounds good and for a long term goal, it should be implemented. The things which we will need to take care with this will be that input taken from the terminal should be properly taken and then return should be the same.

If you need to do this once only then you might be not wondering to write that whole bunch of code if it's just 10 lines yes we developers are too lazy to even write a single line of code.

In this scenario, Laravel 5 gives a good tool so far that I have used is "tinker".


This is going to be super easy with simple and effective use cases.

Let see now how to play around tinker.
  • Go to the Project folder
  • Run command
    • $ php artisan tinker
  • Now we are at the shell prompt of tinker
  • Next, we need to get the script call for our controller
  • Run this command under tinker shell - Assuming controller name as MyController
    • $ $controller = app()->make('App\Http\Controllers\MyController');
  • Now as code is self-explanatory we have initiated the controller to object, next we will call the function
  • The function call can be done as:
    • $ app()->call([$controller, 'myMethodName'], []);
This function call and Enter will execute the function.

How to pass the arguments?
  • $ app()->call([$controller, 'myMethodName'], [user_id=>123]);
That's so simple and easy, but this is good if you are testing something. Make a proper command shell if its a long term task for the team.

Please let us know about the article in comments if there is anything that we can improve or should consider about growing let us know.



Top #3 Articles

Most Read