Laravel tip (Part 3)

Tram Ho


Tip 26 Artisan command parameters.

In the process of creating a new command with artisan , you can request the input in a number of ways such as $ this-> confirm (), $ this-> anticipate (), $ this-> choice (). as follows

Tip 27 Preview mailables.

If you use Mailables to send email, you can preview the results without sending the mail, which can be identified on the brosser,

Tip 28 Route :: view (), don’t create a Controller.

If in your Controller function, only returns the view, then it may not use the controller.

In fact, in the project, all must write to the controller, because currently, can only return the view, however, if adding some logical processing, such as adding policies, then it takes effort to write to the controller.

Tip 29 Blade @auth

Instead of using the if structure to check if the user is logged in or not, in the blade you can use the @auth directive.

Shorter spelling

There is also an @guest directive, which is for un logged in users

Tip 30 Model all: columns

While using the :: all of Model, we can specify which columns are retrieved

Note: this tip is also used with the get () function of Eloquent Model

Tip 31 Localhost in .env

Don’t forget to change the APP_URL variable in the .env file from http: // localhost to the actual URL. It is the main cause leading to some errors of invalid path cases.

Tip 32 What’s behind the routes?

If you want to know the actions behind routes, and really want to understand the mechanism behind the basic route commands. For example, behind what are Auth :: routes . For versions from Laravel 7 and above, check the file /vendor/laravel/ui/src/AuthRouteMethods.php

On lower versions Laravel 7.0 can be checked at /vendor/laravel/framework/src/illuminate/Routing/Router.php.

Tip 33 To Fail

Along with the findOrFail () method , Eloquent also provides the firstOrFail () method , which will return the first record that meets the condition, or return to the 404 page if no records match. The difference between them is that while findOrFail requires an id parameter, firstOrFail () is often used in queries.

Tip 34 Change column names

In Eloquent Query Builder to change the name of the returned column, just like SQL can use as .

Tip 35 Log with the parameters.

Log :: info can be used with the parameter passed to better know exactly what happened.

Share the news now

Source : Viblo