Requête éloquente laravel avec relation

Exemples de code

2
0

associé laravel

When updating a belongsTo relationship, you may use the associate method. This 
method will set the foreign key on the child model:

	$account = App\Account::find(10);
	$user->account()->associate($account);
	$user->save();

When removing a belongsTo relationship, you may use the dissociate method. This
method will set the relationship foreign key to null:

	$user->account()->dissociate();
	$user->save();
1
0

relations éloquentes de laravel

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
    /**
     * Get all of the deployments for the project.
     */
    public function deployments()
    {
        return $this->hasManyThrough(Deployment::class, Environment::class);
    }
}
0
0

où la requête de la table de relations dans laravel

UserModel::whereHas('attachments', function ($attachmentQuery) {
    $attachmentQuery->where('level', 'profile_level');
})->get();
If you want from already queried model get specific attachments then write

$userModel->attachments()->where('level', 'profile_level')->get();

it is impossible to query both UserModel and AttachementModel in single query,
it must be at least two queries. Even fancy 
  UserModel::with('attachments')->get();, 
which returns user with all attachments, do internally two queries.

OR:

I noticed that you can define relation constraints within with method

UserModel::with(['attachments' => function ($attachmentQuery) {
    $attachmentQuery->where('level', 'profile_level');
}])->get();

Pages connexes

Pages d'exemples similaires

Dans d'autres langues

Cette page est dans d'autres langues

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................