Laravel tip (Part 5)

Tram Ho


Tip 51 Don’t filter NULL values ​​in Collections.

In Eloquent, we can filter the value according to the Null value. However, in the collection, it will no longer be possible to filter by null values, but instead will be an empty character.

Tip 52 Email header default value of Laravel notifications

If you send Laravel notification and don’t set a value for the title in the toMail () function, then the default value of your message is the name of the class, capitalized (CamelCased) and separated by spaces. For example if we have a class

then when you receive the email notification, the default subject of the email is User Registration Email

Tip 53 Composer: check for a new version.

If you want to know, in the packages in package.json which packages have new releases, you can run composer outdated . You will get information about all packages that have new versions that have been released.

Tip 54 Fallback Route- When the path does not match any of the defined routes.

In the case of entering any path that does not match any of the previously defined routes. Usually page 40 will drop out. However, you can also redefine this using the fallback route. The fallback route allows to define the handling logic in the url path that doesn’t match any of the routes instead of bouncing a 404 page.

Tip 55 Create your own blade directive .

This is quite easy to do. You just add methods (methods, functions) in the file app / Providers / AppServiceProvider.php . For example, if you replace the card
with a new line, do the following:

and add directive in the boot method in AppServiceProvider

Tip 56 Use withCount () to count the number of child records.

In hasMany () relation, if you want to calculate how many child records there are, without the information of the child records. then use withCount () . For example, you want to count the number of posts (posts) and the number of comments (comments) of each user.

So above .blade you can use [relatioship] _count

Tip 57 Using groupBy in Collections with option callback function

If you want to group the results under some condition that isn’t directly in the database, then you can use the closure function. For example, you want to group the users by registration date.

Note, this groupBy is available on collections, and it is retrieved after retrieving results from the database.

Tip 58. Blade directive: IncludeIf, IncludeWhen, IncludeFirst

If you are not sure if a blade partial exists, then you can use them with the conditions Will load header if the .blade file exists.

Load the header file for the user whose role_id is 1

Try to load adminlte.header, if not, load default.header

Tip 59 Change the Timestamp field

If we want to change the created_at and updated_at fields to something we can change in our model

Tip 60 Fast sort by created_at

Instead of using User :: orderBy (‘created_at’, ‘desc’) -> get (); You can use User :: latest () -> get () ;. By default, latest () will sort by created_at . Conversely, we also have the oldest () to order by the descending created_at field.

If you use a certain field to sort, like updated_at, you can do it like this

Tip 61 Generate Images with Seed / Factories

When using faker (factories / seed) to add sample data, Faker supports our avatar style so that we can generate pictures with the attached size. For example, let’s take a 50×50 image

Tip 62 Eloquent: Update Parent in one line.

In belongsTo () relationship, we may use Eloquent to be able to update data in a single line. For example, if we have Project -> belongsTo (User :: class), we will update the user’s email with the following command

Tip 63 Eloquent: Laravel 7+ Foreign Keys

Since Laravel 7, in migrations, you do not need to write two lines to define a foreign key. Laravel already supports the foreignId () method to define a foreign key.

Tip 64 Using methods with Collection.

For queries that take all the data with all () or get () , instead of repeatedly querying, we should use methods with collection as the initial query result to increase performance / efficiency. For example.

Tip 65 Add events when registering a user (user)

If you want to add some action, after a new user registers, first go to app / Providers / EventServiceProvider.php and add the class (class) listener, then implement the method’s handle () function with the $ event-> user icon

Share the news now

Source : Viblo