“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

There is no superior or inferior martial arts in the world, only those who practice martial arts are strong or weak.

Eloquent Eloquent Model: Eloquent Eloquent Model: Eloquent Eloquent Model: Eloquent Eloquent Model: Eloquent Eloquent Model: Eloquent Eloquent Model

Correlation model

Return to default model

This is very important!! Avoid fatal errors when associated data does not exist.

For example, a belongsTo relationship can be set to return a default model to avoid fatal errors like {{$POST ->user->name}} when $POST ->user does not exist.

public function user()
{
    return $this->belongsTo('App\User')->withDefault();
}
Copy the code

Use OrderBy in a Eloquent relationship

You can specify orderBy () directly in the association relationship

For example, to set the make and sell of the association, specify sorting by the name field

public function products()
{
    return $this->hasMany(Product::class);
}

public function productsByName()
{
    return $this->hasMany(Product::class)->orderBy('name');
}
Copy the code

Add conditions to the Eloquent relationship

If we often add some of the same WHERE conditions to model relationships, we can extract them into a separate method.

When you specify the relationship with the comment, you set approved=1 so that the external world can call this method directly when it calls it.

Public function comments() {return $this->hasMany(Comment::class); } // Specify the relationship with the comment, and set approved=1, so that the external can call this method directly when called. public function approved_comments() { return $this->hasMany(Comment::class)->where('approved', 1); }Copy the code

DB native query: havingRaw ()

We can use raw database queries in many places, such as calling havingRaw() after groupBy()

Goods::groupBy('category_id')->havingRaw('COUNT(*) > 1')->get();
Copy the code

Eloquent uses HAS () to implement a multi-tier call query

We can use has() in relational queries to implement two-tier relational queries.

// Teacher -> hasMany(Class::class);
// Class -> hasMany(Student::class);
$teachers = Teacher::has('classes.students')->get();
Copy the code

Gets a specified amount of data in a one-to-many relationship

In a one-to-many relationship, we can get the data that matches through conditional filtering.

For example, we need to find which teachers are responsible for more than 5 classes.

// Teacher -> hasMany(Class::class)
$teachers = Teacher::has('classes', '>', 5)->get();
Copy the code

Create multiple associated data at a time in a one-to-many relationship

In a one-to-many relationship, we can use saveMany() to save multiple associated data in a single commit.

$article = Article::find(1);

$article->comments()->saveMany([
    new Comment(['message' => 'First comment']),
    new Comment(['message' => 'Second comment']),
]);
Copy the code

Multilevel craving loading

In Laravel, we can eagerly load multiple levels in a single statement.

In the following example, we load not only the author relationship, but also the country relationship on the author model.

$users = App\Book::with('author.country')->get();
Copy the code

Eagerly load specific fields

We can load wistfully in Laravel and specify specific fields in the association.

$users = App\Book::with('author:id,name')->get();
Copy the code

We can also do this at deep levels, such as second-level relationships:

$users = App\Book::with('author.country:id,name')->get();
Copy the code

Welcome to the interactive

Welcome everyone to like attention, if you have any good suggestions, welcome to the comment section