Laravel Relationship

[Solved] Laravel Relationship | Php - Code Explorer | yomemimo.com
Question : laravel where has

Answered by : alberto-peripolli

use Illuminate\Database\Eloquent\Builder;
// Retrieve posts with at least one comment containing words like foo%...
$posts = App\Post::whereHas('comments', function (Builder $query) { $query->where('content', 'like', 'foo%');
})->get();
// Retrieve posts with at least ten comments containing words like foo%...
$posts = App\Post::whereHas('comments', function (Builder $query) { $query->where('content', 'like', 'foo%');
}, '>=', 10)->get();

Source : https://laravel.com/docs/7.x/eloquent-relationships | Last Update : Thu, 14 May 20

Question : how to use where relationship laravel

Answered by : alive-angelfish-8hkpto2y9706

Event::with(["owner", "participants" => function($q) use($someId){ $q->where('participants.IdUser', '=', 1); //$q->where('some other field', $someId);
}])

Source : https://stackoverflow.com/questions/29989908/laravel-where-on-relationship-object | Last Update : Wed, 05 Aug 20

Question : whereHas site:https://laravel.com/docs/

Answered by : tiago-frana

use Illuminate\Database\Eloquent\Builder;
// Retrieve posts with at least one comment containing words like code%...
$posts = Post::whereHas('comments', function (Builder $query) { $query->where('content', 'like', 'code%');
})->get();
// Retrieve posts with at least ten comments containing words like code%...
$posts = Post::whereHas('comments', function (Builder $query) { $query->where('content', 'like', 'code%');
}, '>=', 10)->get();

Source : https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence | Last Update : Thu, 24 Dec 20

Question : associate laravel

Answered by : lokesh-ramchandani

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();

Source : | Last Update : Mon, 14 Dec 20

Question : laravel how to query belongsTo relationship

Answered by : fragile-flatworm-0c0no1b5zwnz

$movies = Movie::whereHas('director', function($q) { $q->where('name', 'great');
})->get();

Source : https://stackoverflow.com/questions/23970762/eloquent-where-condition-based-on-a-belongs-to-relationship | Last Update : Mon, 19 Oct 20

Question : laravel.com relationship

Answered by : dinesh-mohurle-z3qrudukbslp

1-1
public function phone() { return $this->hasOne('App\Phone'); } public function user() { return $this->belongsTo('App\User'); }
1-many
public function phone() { return $this->hasMany('App\Phone'); } public function user() { return $this->belongsTo('App\User'); }
many-many // migrations/****_**_**_******_create_category_post_table.php
Schema::create('category_post', function(Blueprint $table) { $table->integer('category_id')->unsigned(); $table->integer('post_id')->unsigned(); $table->primary(['category_id', 'post_id']); $table->foreign('category_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade'); $table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade');
});
// Post.php
public function categories()
{ return $this->belongsToMany(Category::class);
}
// Category.php
public function posts()
{ return $this->belongsToMany(Post::class);
}
In post.php, use this relation: public function categories(){ return $this->belongsToMany(Category::class, 'category_post', 'post_id', 'category_id'); }
//controller $post = new Post(); $post->title = $request->title; $post->body = $request->body; $post->categories()->attach($request->categories_id);
https://laracasts.com/discuss/channels/laravel/how-to-seed-db-in-pivot-table-laravel
https://laraveldaily.com/pivot-tables-and-many-to-many-relationships/
https://stackoverflow.com/questions/38746613/how-to-insert-a-post-with-multi-category-and-with-multi-column-deferent-category
query builder https://stackoverflow.com/questions/33449387/laravel-creating-different-views-from-query/33449507#33449507

Source : | Last Update : Wed, 24 Aug 22

Question : relationship in laravel

Answered by : elegant-echidna-hujgm9zlnx9z

N + 1
return new SongsCollection(Song::with('album')->get());
'songs' => SongResource::collection($this->whenLoaded($this->songs))

Source : | Last Update : Thu, 21 Oct 21

Question : eloquent relationships

Answered by : energetic-emu-dmvyfcbirsbd

$roles = App\User::find(1)->roles()->orderBy('name')->get();

Source : https://laravel.com/docs/7.x/eloquent-relationships | Last Update : Mon, 22 Jun 20

Question : laravel relationship

Answered by : tough-tapir-5290s8m5b1ys

$model->relation; // result of the relation, ie. null/model for x-1 relations or collection for x-m
$model->relation(); // relation object

Source : https://laracasts.com/discuss/channels/eloquent/update-hasmany-relation | Last Update : Tue, 20 Jul 21

Question : laravel defining relationship

Answered by : cooperative-crab-qq95wr7pe6oz

$user->posts()->where('active', 1)->get();

Source : https://laravel.com/docs/8.x/eloquent-relationships | Last Update : Sat, 31 Jul 21

Answers related to laravel relationship

Code Explorer Popular Question For Php