how to write where condition in relation table in Laravel in query - laravel

I have 3 table that is Resort, Booking, Expense, these tables are join with relation. the code is given below,
$resorts = Resort::where('status',1)->with('bookings')->withSum('bookings', 'amount')
->with('expenses')->withSum('expenses', 'amount')->get();
I want to sort this table using the date field. how could I use the wherebetween in this query for bookings and expense?

you can pass array in with() like this
$resorts = Resort::where('status', 1)
->with(
['bookings' => function ($q) {
$q->wherebetween('colName', ['startDate','endDate']);
}]
)->withSum('bookings', 'amount')
->with(
['expenses' => function ($q) {
$q->wherebetween('colName', ['startDate','endDate']);
}]
)->withSum('expenses', 'amount')
->get();
ref link https://laravel.com/docs/8.x/eloquent-relationships#constraining-eager-loads

Related

Laravel query orderBy nested relationship

I have the following relationship:
Unit (HasMany)-> Users -> (BelongsTo) -> Position
I am trying to return an array of units with users, where users are sorted by their position. The property in the position model is 'order' that I would like to use as the sort field. I have attempted the following:
return Unit::query()->ordered()->with(['users' => function($query) {
$query->with(['position' => function($query) {
$query->orderBy('order');
}]);
}])->get();
You can not order by nested relationship just using with() method. You need to join the relation first. So the code should be:
return Unit::query()->ordered()->with([
'users' => function ($query) {
$query->join('positions', 'positions.id', '=', 'users.position_id');
$query->orderBy('positions.order');
}
])->get();
or another way is order using laravel collection sortBy
$ordered_units = Unit::query()->ordered()->with(['users' => function($query) {
$query->with(['position' => function($query) {
$query->orderBy('order');
}]);
}])->get();
return $ordered_units->sortBy('users.position.order');

Laravel Eloquent with() selecting specific column doesn't return results

Say I have 2 models, Category and POI where 1 Category can have many POIs.
$categoryDetails = Category::with([
'pois' => function ($query) {
$query->where('is_poi_enabled', true);
},
])->findOrFail($id);
The above query returns results from the specific Category as well as its POIs.
However, with the query below:
$query->select('id', 'name')->where('is_poi_enabled', true);
The POIs become empty in the collection.
Any idea why this is happening? When added a select clause to the Eloquent ORM?
While doing a select it's required to fetch the Relationship local or Primary key.
For an example POIs table contains category_id then it's required to select it
Try this:
$categoryDetails = Category::with([
'pois' => function ($query) {
$query->select(['id', 'category_id', 'is_poi_enabled'])
->where('is_poi_enabled', true);
},
])->findOrFail($id);
Good luck!

Laravel Get Result Ordred By For an eager Loaded Relation

I have:
'cards' table
-id
-name
'card_categories' Table
id
card_id
category_id
'categories' Table
id
name
index
I'am Loading The Card then Eager load the Relation, what i would like to do is when doing this :
Card::with('ctrCategories.category').......;
I would like that all loaded category from categories will be sorted by Index just the categories.
I spent the hole day doing everything , but no solution:
I tried this:
$card = Card::findOrFail($id);
return $card->with('cardCategories')
->with('ctrCategories.category')
->with('ctrCategories.arguments')
->orderBy('ctrCategories.category.index')->get();
I also tried this approach:
$data = $this->card
->with([
'roles' => function ($q) {
$q->with(['tabs' => function ($q) {
$q->with(['department' => function ($q) {
$q->with(['panel' => function ($q) {
$q->orderBy('position', 'asc');
}])->orderBy('position', 'asc');
}])->orderBy('position', 'asc');
}])->orderBy('position', 'asc');
}
])
->findOrFail($id);
=====EDIT=====
I writed the SQL query and i got the result i want Now i want to transform it to Laravel Eloquent or DB query:
select cards.id,
cards.name,categories.name,categories.id,categories.index
from cards
inner join card_categories on cards.id = card_categories.card_id inner join categories on categories.id = card_categories.category_id
where cards.id = 120
AND cards.support_id= categories.support_id
order by categories.index asc
any help ? I can't figure it out after couple of hours of testing

Order by sum column relationship in Laravel

I have this controller which grabs posts from a post table.
Every post in the posts table have the relation "hasMany" with another table likes.
Controller:
public function getDashboard(){
$posts = Post::orderBy('created_at', 'desc')->get();
return view('dashboard', ['posts' => $posts]);
}
I'd like to replace 'created at' with something like:
$post->likes->sum(like)
Don't know how to write the right syntax though.
EDIT:
Here are the tables.
Posts
--id
--body
Likes
--id
--post_id
--like
The column like can have the value 1 or -1.
I'd like to order on the summation of that column for each post.
So a post with one dislike(-1) and one like(1) will have the aggregated value of 0, hence will be placed after a post with one like(1).
You can use withCount() for this as:
Post::withCount('likes')->orderBy('likes_count')->get()
withCount() will place a {relation}_count column on your resulting
models
Update
Post::withCount(['likes' => function($q) {
$q->where('like', 1)
}])
->orderBy('likes_count')
->get()
Update2
You can use sortByDesc() to sort your collection as:
$posts = Post::get();
$posts = $posts->sortByDesc(function ($post) {
return $post->likes->sum('like');
});
If you want to sum column value from relationship and then sort records.
$users = User::addSelect(['likes' => Post::selectRaw('sum(likes) as total_likes')
->whereColumn('user_id', 'useres.id')
->groupBy('user_id')
])
->orderBy('likes', 'DESC')
->get()
->toArray();
Or
use below
$users = User::select("*",
\DB::raw('(SELECT SUM(likes) FROM likes_table WHERE likes_table.user_id = users.id) as tolal_likes'))
->orderBy('likes', 'DESC')
->get()
->toArray();

How to select field in another table join by eager loading in Laravel

I got category_to_news and news_main table
category_to_news
news_id int
name varchar
title timestamp
news_main
id int
title varchar
image varchar
created_at timestamp
how to return by news_main's field and category_news's field ? I've tried this method and it's not work
$posts = Categorytonews::with(array(
'Newsmain' => function($query)
{
$query->orderBy('id', 'DESC')->select(array('title', 'id', 'created_at', 'image'));
}
))
->get( );
You need join for this because with() runs another query, so ordering there won't do the job:
$posts = Categorytonews::with('Newsmain') // get all fields from Newsmain
->join('news_main', 'category_to_news.news_id', '=', 'news_main.id') // join for ordering
->orderBy('news_main.id','desc') // order by joined field
->get('category_to_news.*'); // select only main table, you don't need joined fields
// on Categorytonews model - they will be loaded by with()
$posts = Categorytonews::with(array('Newsmain' => function($query)
{
$query->select(array('title', 'id', 'created_at', 'image'))->orderBy('id', 'DESC');
}))->get();

Resources