Laravel tip (part 1)

Tram Ho

Source laraveldaily.com

Tip 1 Single Action Controller

If you want to create a controller with only one action, you can use the __invoke () method to create an “invokable” controller.

Routes

The command to generate this controller is

Tip 2 Unsigned Integer

To put foreign keys in migrations, instead of using integer () , use unsignedInteger () or integer () -> unsigned () , otherwise, you may get some SQL errors.

You can use unsignedBigInteger () in case the column is of type bigInteger ()

Tip 3 Use OrderBy right in Eloquent relationship

You can use orderBy right within the Eloquent relationship of the model

Tip 4 Arrange the migrations

If you want to sort the DB migrations, you just need to change the file name and timestamp For example, change from 2018_08_04_070443_create_posts_table.php to 2018_07_04_070443_create_posts_table.php , then this migrations file will change the placement by alphabetical

Tip 5 Raw DB Queries: havingRaw ()

You can use some RAW DB QUERIES queries in several places, including havingRaw () after performing groupBy ().

Tip 6 Turn the $ loop in foreach loop

In the foreach loop, you can use the $ loop variable to do a lot of things

Specific tasks that can be performed with the $ loop variable may be as follows

PropertiesDescription
$ loop-> indexGet the index of the current loop calculated starting at 0
$ loop-> iterationGet the number of iterations (starting at 0)
$ loop-> remainingNumber of loops remaining of the loop
$ loop-> countThe total number of elements that will be browsed in the loop
$ loop-> firstThe first element in the loop
$ loop-> lastThe last element in the loop
$ loop-> evenThe elements stand evenly in the loop
$ loop-> oddThe elements stand odd positions in the loop
$ loop-> depthNesting level of the current loop
$ loop-> parentWhen the loop is nested, this variable returns the parent loop

Tip 7 Eloquent where date methods

In Eloquent, we can use the functions whereDay (), whereMonth (), whereYear (), whereDate (), whereTime () to query the time correctly.

Tip 8 Route group inside a group

In Route , we can create another group within the group itself.

Tip 9 Increments and decrements

If you want a column in the DB to increment, just use increment () . It is not necessary that every increase of 1, may increase each time to 50.

Tip 10 Check if the view file exists?

You can check to see if the View files exist before loading them


You can also load the first View file if it exists in the array of view files listed
Share the news now

Source : Viblo