Item Model:
public function item_makes(){
return $this->hasMany(ItemMake::class,'item_id','id');
}
In ItemMake Model :
public function make(){
return $this->belongsTo(Make::class,'make_id','id');
}
I need to get array of all make based on item_id. How to achieve this? Thanks in Advance.
This worked for me.
$item = Item::findOrFail(1)
->item_makes()
->with('make')
->get()
->pluck('make')
->flatten()
->toArray();
Try something like this:
Item::findOrFail(1)
->with('item_makes.make')
->get()
->pluck('make')
->flatten()
->toArray()
Try wherehas method something like this
$makes = Make::whereHas('item_makes', function ($query) use($item_id) {
$query->where('item_id', $item_id);
})->get();
Related
can someone help me on getting data from multi level relationships in laravel?
I want to do something like
BagageAnnouncement->Announcement->user->profile->"name of the field"
$data = $request->text;
$filter = BagageAnnouncement::whereHas('announcement',function ($query) {
})->whereHas('user', function ($query) {
})->whereHas('profile',function($query){
$query->where('level',$data)
})->get();
Thank you guys for you answers I finally figure out a solution
$data = $request->text;
$filter = BagageAnnouncement::whereHas(
'announcement.user.profile',
function ($q2) use ($data) {
$q2->where('level',$data);
}
)->get();
Not sure what you want as the result, but if you want a single result, you can do like this:
$bagage = BagageAnnouncement::where(specify_your_condition)->first();
$filter = $bagage->user->profile->name_of_the_field;
If you want an array of results, change the first() to get() and make use of foreach
Of course, you will need to set up the relationships in your models first.
This is my table.i need all post in descending order.I want advertised posts in top.
result example
Code
$data=Post::get();
Relationship in model
public function isAdvertised()
{
return $this->hasOne('App\models\PostAdvertise', 'post_id');
}
You can use leftJoin
$data = Post::leftJoin('post_advertise', 'post.id', '=', 'post_advertise.post_id')
->orderBy('post_advertise.created_at')
->select('post.id', 'post.title', 'post.description')
->get();
try
$posts = Post::with('isAdvertised')->all();
$sorted_posts = $posts->sortBy(function ($post, $key) {
return (is_null($post->isAdvertised) ? 1 : 0);
});
This will order the collection.
$posts = Post::orderBy('created_at','desc')->get();
You can then add this to your view:
#foreach($posts as $post)
$post->post_advertise->title
#endforeach
If it does not work I would advise you to post exactly what you did, like your migration, model(relationship) and controller.
I have a Seller object which has a related User. I need to fill a select from LaravelCollective so I need to make something like this:
{!! Form::selectGroup('seller_id', 'Seller', Seller::with('user')->pluck('user.first_name', 'id')->toArray(), null) !!}
The problem is that I cannot take fields from relationships (user.first_name).
How can I do it?
UPDATE
I want to avoid doing this...
<?php
$sellers = [];
Seller::with('user')->get()->each(function ($seller) use (&$sellers) {
$sellers[$seller->id] = $seller->user->first_name;
});
?>
You can use Laravel's pluck method as:
$sellers = Seller::with('user')->get()->pluck('user.first_name', 'id')
try with below solution it's working for me as i am using laravel 8
$sellers = Seller::with('user')->get()->pluck('user.*.first_name')
You can achieve it by using join() & pluck() like this:
$s = Seller::join('users', 'sellers.user_id', '=', 'users.id')
->pluck('sellers.id', 'users.id')
->all();
This would give an array like this:
[
'seller_id_1' => 'user_id_1',
'seller_id_2' => 'user_id_2',
'seller_id_3' => 'user_id_3',
'seller_id_4' => 'user_id_4',
'seller_id_n' => 'user_id_n',
];
Hope this helps!
Another way to do it is to define what columns you need inside the relationship. It's good if you always need just these columns on the given relationship. Example:
Class Seller extends Model {
...
public function user()
{
return $this->hasOne(user::class, 'id')
->select('id', 'first_name');
}
}
i want to sort the users through voornaam(firstname). but im getting the data via a relation.
How do i make my query so that, the relation users are sorted by firstname by alphabet
my function:
public function sortfirstname($id) {
$ingeschrevenspelers = UserToernooi::with('users')->where('toernooiid', '=', $id)->get()->all();
//This query ^^
$toernooi = Toernooi::findOrFail($id);
dd($ingeschrevenspelers);
return view('adminfeatures.generatespelerslijst', compact('ingeschrevenspelers', 'toernooi'));
}
What i want to sort
any help is appreciated
thanks in advance
Writing code in your own language doesn't make it very easy for other developers to understand your code.
That being said, you can try the orderBy() method on your relationship
In your model where you define the relationship:
public function relationship()
{
return $this->belongsTo(SomeClass::class)->orderBy('name', 'DESC');
}
Don't fire all() function at the end thus obtaining a Collection instance of result
//query without the all function
$ingeschrevenspelers = UserToernooi::with('users')->where('toernooiid', '=', $id)->get();
//
$ingeschrevenspelers = $ingeschrevenspelers->sortBy('users.firstname');
An alternative to Jordy Groote's answer if you do not want to modify the Model class itself, you can query it with a closure.
$ingeschrevenspelers = UserToernooi::with(['users' => function($q) {
$q->orderBy('voornaam', 'asc');
}])->where('toernooiid', '=', $id)->get()->all();
Reference: https://laravel.com/docs/5.3/eloquent-relationships#constraining-eager-loads
Sidenote: I don't think you need a ->all() when you already did a ->get()
$ingeschrevenspelers = UserToernooi::with(['users' => function($query){
$query->orderBy('voornaam', 'asc');
}])->where('toernooiid', '=', $id)->get()->all();
I have News model, and News has many comments, so I did this in News model:
public function comments(){
$this->hasMany('Comment', 'news_id');
}
But I also have field trashed in comments table, and I only want to select comments that are not trashed. So trashed <> 1. So I wonder is there a way to do something like this:
$news = News::find(123);
$news->comments->where('trashed', '<>', 1); //some sort of pseudo-code
Is there a way to use above method or should I just write something like this:
$comments = Comment::where('trashed', '<>', 1)
->where('news_id', '=', $news->id)
->get();
Any of these should work for you, pick the one you like the most:
Eager-loading.
$comments = News::find(123)->with(['comments' => function ($query) {
$query->where('trashed', '<>', 1);
}])->get();
You can inject the parameter to query function by use($param) method, that allows you to use dynemic query value at runtime.
Lazy-loading
$news = News::find(123);
$comments = $news->comments()->where('trashed', '<>', 1)->get();
I couldn't help but notice, though, that what you're probably trying to do is handle soft deleting, and that Laravel has built-in functionality to help you with that: http://laravel.com/docs/eloquent#soft-deleting
You can do simply in your eloquent model file.
do like this :
public function comments_with_deleted()
{
return $this->belongsTo('Comments', 'id')->where('deleted', 1);
}
public function comments()
{
return $this->belongsTo('Comments', 'id');
}
call like this :
// for show comments with deleted
$comments = News::find(123)->with('comments_with_deleted');
// for show comments without deleted
$comments = News::find(123)->with('comments');
rmobis's answer was what I needed, but it throws an error in current Laravel 5. You have to use it as an associatve array now:
$comments = News::find(123)->with(
['comments' => function ($query) {$query->where('trashed', '<>', 1);}]
);
Took me some time to figure it out, hope this will help others.
Read more in Laravel's Docs (5.6): https://laravel.com/docs/5.6/eloquent-relationships#querying-relations