“This is the 9th 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.

In this post, we introduce you to the new version of Laravel, as well as some of its elegant and simple new features.

The new version uses tricks

WhereHas is a cleaner implementation

Published in Laravel 8.57: Use the whereRelation() method to abbreviate whereHas().

before

User::whereHas('articles', function ($query) {
    $query->where('published_at', '>', now());
})->get();
Copy the code

now

User::whereRelation('articles', 'published_at', '>', now())->get();
Copy the code

Laravel 7+ foreign key

Starting with Laravel 7, we don’t need to write two lines of code for some relational fields in a migration: one for the field and one for the foreign key.

We can use the foreignId() method.

Laravel before 7

Schema::table('articles', function (Blueprint $table)) {
    $table->unsignedBigInteger('user_id');
    $table->foreign('user_id')->references('id')->on('users');
}
Copy the code

Start with Laravel 7

Schema::table('articles', function (Blueprint $table)) {
    $table->foreignId('user_id')->constrained();
}
Copy the code

When our field is different from the reference in the table, we can do this:

Schema::table('articles', Function (Blueprint $table) {// created_by_id = created_by_id $table->foreignId('created_by_id')->constrained('users', 'column_name'); }Copy the code

Here are the common version tips

The two “whereHas” are used in combination

Eloquent We can use whereHas() and orDoesntHave() in the same statement, Eloquent.

Here’s an example:

User::whereHas('jobs', function($query) {
    $query->where('id', 1);
})
->orDoesntHave('jobs')
->get();
Copy the code

Check whether the relational method already exists

Eloquent relationship name is dynamic

We can use the PHP method method_exists($object, $methodName) to check if a relationship with the same name exists in the project;

Note: Be sure to do this check, otherwise it can lead to unpredictable, difficult-to-locate problems.

$user = User::first(); If (method_exists ($user, 'Mr. Jobs')) {/ / use the $user - > jobs () - > doing other things... }Copy the code

Gets the relational data in the intermediate table

In a many-to-many relationship, we define an intermediate table that may contain extended fields or even other relationships.

Generate an intermediate table model:

php artisan make:model JobUser --pivot
Copy the code

Then, specify the ->using() method to belongsToMany().

  • app/Models/User.php
public function jobs()
{
    return $this->belongsToMany(Job::class)
        ->using(JobUser::class)
        ->withPivot(['company_id']);
}
Copy the code
  • app/Models/RoleUser.php

Note that you inherit from Pivot, not Model

use Illuminate\Database\Eloquent\Relations\Pivot; class JobUser extends Pivot { public function company() { return $this->belongsTo(Conpany::class); }}Copy the code

JobUser = Company; JobUser = Company;

$firstCompany = auth()->user()->jobs()->first()->pivot->company->name;
Copy the code

Random ordering of relational model data

We can use inRandomOrder() to randomly sort the results of our Eloquent query

It can also be used to implement random sorting of associated data.

$users = User::inRandomOrder()->get(); / / 2. For random users random work: $users = User: : with ([' jobs' = > function ($q) {$q - > inRandomOrder ();}]) - > inRandomOrder () - > get ();Copy the code

Last but not least

Technical group please come here. Or add my wechat account wangzhongyang0601 to learn together.

Thank you for your likes, comments and followings. Thank you for your support. Thank you.