How to multiply and group using laravel eloquent with relation - mysql

I'd like to export data from my database but have problems with multiplying and sum using laravel eloquent with relation
So i have 2 tables there (budgets, items)
Budget's:
// Table
+----+---------------+-----------------+------+-----+--------------------+
| id | delivery_plan | item_code | curr | qty | price |
+----+---------------+-----------------+------+-----+--------------------+
| 1 | 2022-08 | 201.0001 | IDR | 1 | 2000.0000000000 |
| 2 | 2022-08 | 201.0001 | IDR | 3 | 2000.0000000000 |
| 3 | 2022-07 | 201.9999 | IDR | 2 | 2000.0000000000 |
+----+---------------+-----------------+------+-----+--------------------+
// Relation
public function item()
{
return $this->belongsTo(Item::class, 'item_code', 'item_code');
}
Items :
// Table
+----+----------------+-----------+
| id | subgroup | item_code |
+----+----------------+-----------+
| 1 | KOMPONEN MESIN | 201.0001 |
| 2 | EQUIPMENT LAIN | 201.9999 |
+----+----------------+-----------+
// Relation
public function budgets()
{
return $this->hasMany(Budget::class, 'item_code', 'item_code');
}
So, the scenario is :
Multiply the "qty" * "price" columns and name them as "total" like so
Group them by "subgroup" column, which came from item() relationship
Group them by "delivery_plan"
I prefer using eloquent because to minimize the complexity because i need that "whereHas" method
This is what i've tried so far and isn't working :
$budgets = Budget::with('item', 'rate')->whereHas('period.term', function (Builder $builder) {
$builder->where('name', '=', Session::get('term-budget'));
})->where('section', Session::get('section-budget'))->getQuery();
$result = $budgets->sum('price * qty')->get();
How can i achieve this ?

This can be solved by a join with SUM(), something like below (untested):
Budget::leftJoin('items', 'budgets.item_code', '=', 'items.item_code')
->addSelect('subgroup')
->addSelect('delivery_plan')
->addselect(\DB::raw('SUM(qty * price) as total'))
->groupBy('subgroup', 'delivery_plan')
->get();

Related

Laravel 7 select from table with two columns having ids from a Single Table

My requirement?
If i am the logged in user with Id = 1, then through the Messages Table i want to select users from Users Table to whom i sent the message or from whome i received the message.
Table 1: Users
+----+------+-------+
| id | name | email |
+----+------+-------+
| 1 | a | ??? |
| 2 | b | ??? |
| 3 | c | ??? |
| 4 | d | ??? |
| 5 | e | ??? |
| 6 | f | ??? |
| 7 | g | ??? |
| 8 | h | ??? |
| 9 | i | ??? |
| 10 | j | ??? |
+----+------+-------+
Table 2: Messages
+----+---------+-------------+
| id | user_id | receiver_id |
+----+---------+-------------+
| 1 | 1 | 2 |
| 2 | 3 | 1 |
| 3 | 4 | 1 |
| 4 | 1 | 5 |
| 5 | 1 | 3 |
+----+---------+-------------+
User Model
public function messages()
{
return $this->belongsToMany(User::class, 'messages', 'user_id', 'receiver_id');
}
Message Model
public function user()
{
return $this->belongsTo(User::class);
}
So what i have tried so far?
$id = Auth::id();
$users = User::with(['messages' => function($query) use($id){
$query->where('user_id', $id)
->orWhere('received_id', $id)
->orderBy('created_at', 'DESC');
}])->get();
dd($users);
What is the expected result?
Using this query, i am getting all of my 10 users. Although i should only get 4 users(those with id's 2,3,4,5).
If the above query is wrong, or i should follow another method or i should created some sort of relationships Please help.
Hopefully you have understood the question, i am new to Laravel but i am learning.
Probably what you need is three relations(one to many) in the User model. One for sent messages, one for received messages and one for both, like this:
public function messagesSent()
{
return $this->hasMany(Message::class, 'user_id');
}
public function messagesReceived()
{
return $this->hasMany(Message::class, 'receiver_id');
}
public function messages()
{
return $this->messagesSent()->union($this->messagesReceived()->toBase());
}
Then you should be able to get user messages like this: User::with('messages')->get();
I think you should use a join statement or "whereHas" to select users who have any messages.
$id = Auth::id();
$users = User::whereHas('messages', function ($query) use($id){
$query->where('user_id', $id)
->orWhere('received_id', $id);
})
->get();
To have access to "messages" you should add "with" statement too.
Adding my own solution(i.e working) to this question.
User Model
public function sent()
{
return $this->hasMany(Message::class, 'user_id');
}
public function received()
{
return $this->hasMany(Message::class, 'receiver_id');
}
Query
$users = User::whereHas('sent', function ($query) use($id) {
$query->where('receiver_id', $id);
})->orWhereHas('received', function ($query) use($id) {
$query->where('user_id', $id);
})->get();
dd($users);

Laravel sub-query

I have a database that looks like this:πŸ‘‡πŸ‘‡
images πŸŒ…
| id | name | src | status |
| ------------- |---------------| ------------| ----------|
| 1 | nice sun set | 1020288.jpg | published |
| 2 | poor sun set | 1120288.jpg | published |
| 3 | best sun set | 3120288.jpg | deleted |
| ------------- |---------------| ------------| --------- |
image_views πŸ‘€
| id | image_id | browser_id🌎 | created_at |
| ------------- |---------------| ------------ | ------------------ |
| 1 | 2 | 1020288e3221 |2020-02-23 13:55:11 |
| 2 | 1 | 1120288221ww |2020-02-27 13:50:51 |
| ------------- |---------------| ------------ | ------------------ |
Now in my laravel App,
I want to get the most viewed image in the PAST last 7 days.
( i want to have a column of image_views and those views πŸ‘€ should be grouped by browser id ).
so here is what i have tried:πŸ‘‡πŸ‘‡
$image_views = DB::table('image_views')
->selectRaw('count(*) as view_count')
->where(function($query){
$query->where('image_views.image_id', 'images.id');
$query->whereDate('image_views.created_at', '>=', Carbon::now()->subDays(7)->toDateTimeString() );
});
$image = Image::select(['images.*', DB::raw('(' . $image_views->toSql() . ') as views ')])
->limit(1)
->orderBy('views', 'desc')
->where('images.status','published')
->mergeBindings($image_views)
->get();
return $image;
So unfortunately the posted above☝☝ code does not work😩
It only return blank results.
By the way i have lot of views in image_views table starting from 2⃣0⃣1⃣9⃣ to now, just that i couldn't post all here..
THE FUNNY THING IS THAT IF I CONVERT IT TO SQL AND PASTE IT IN PHPMYADMIN IT WORKS LIKE A CHARM
return $image->toSql();
//->mergeBindings($image_views)
//->get();
PLEASE SOMEONE TELL ME WHAT I AM DOING WRONG IN LARAVEL!!πŸ™Œ
Given images & image_views tables
$mostViewdImage = DB::table('image_views')
->join('images', 'image_views.image_id', '=', 'images.id')
->select('browser_id', DB::raw('count(image_id) as occurrence'), 'images.*')
->where('image_views.created_at', '>=', Carbon::now()->subDays(7)->toDateTimeString())
->groupBy('image_id', 'browser_id')
->orderByRaw('occurrence DESC')->first();
dump($mostViewdImage);
//Output
"select `browser_id`, count(image_id) as occurrence, `images`.* from `image_views` inner join `images` on `image_views`.`image_id` = `images`.`id` where `image_views`.`created_at` >= ? group by `image_id`, `browser_id` order by occurrence DESC limit 1" (2.02 s)
{#261 β–Ό
+"browser_id": "1020288e3221"
+"occurrence": 2
+"id": 2
+"name": "poor sun set"
+"src": "1120288.jpg"
+"status": "published"
}

Using whereRaw condition on laravel eager loading with query builder

We want's to need those complain, which lifetime(created_at - now()) is grater then a complain lifetime(the lifetime amount stored on complain_type table) by eloquent relationship.
01.complain table:
+---+------------+-----------------+
|id | complain_preset_id | created_at |
+---+------------+-----------------+
| 1 | 48 | 3/16/2018 10:30 |
| 2 | 13 | 3/16/2018 10:43 |
| 3 | 12 | 3/16/2018 10:57 |
+---+------------+-----------------+
02. Complain Preset Table:
+---+------------+-----------------+
|id | type_id | created_at |
+---+------------+-----------------+
| 1 | 1 | 3/16/2018 6:29 |
| 2 | 2 | 3/16/2018 6:29 |
| 3 | 3 | 3/16/2018 6:29 |
+---+------------+-----------------+
03. complain type table
+---+------------+
|id | lifetime |
+---+------------+
| 1 | 10 |
| 2 | 36 |
| 3 | 360 |
| 4 | 500 |
+---+------------+
the relation between complain->preset is:
public function preset()
{
return $this->belongsTo(ComplainPreset::class, 'complain_preset_id');
}
the relation between preset->complain is:
public function complains()
{
return $this->hasMany(Complain::class, 'complain_id');
}
AND preset->complain_type:
public function complainType()
{
return $this->belongsTo(ComplainType::class, 'type_id');
}
complain_type->preset:
public function presets()
{
return $this->hasMany(ComplainPreset::class);
}
Their is no direct relationship between complain to complain_type.
Here is our solution eloquent query. but that query doesn't work.
The relation is complain->preset->complain_type
Complain::with(['preset' => function ($q) {
$q->with(['complainType' => function($q2) {
$q2->whereRaw('SUBTIME(NOW(), lifetime) > complains.created_at');
}]);
}])->whereDate('created_at', '=' , Carbon::today());
In line 3, this query didn't get complains.created_at, because this line refer to complain_type table.
On line 3 we need to access complains.created_at.
Is their any eloquent way ?
You can use whereHas():
Complain::whereHas('preset.complainType', function($query) {
$query->whereRaw('SUBTIME(NOW(), lifetime) > complains.created_at');
})->whereDate('complains.created_at', '=', Carbon::today());
We want's to need those complain
You could use join to apply filter using column of main table complains with your indirectly (via complain_preset) related table complain_type
Complain::with('preset')
->join('complain_preset as cs','complains.complain_preset_id','=', 'cs.id')
->join('complain_type as ct','cs.type_id','=', 'ct.id')
->whereRaw('SUBTIME(NOW(), ct.lifetime) > complains.created_at')
->whereDate('complains.created_at', '=' , Carbon::today());

Mysql add column from different table

I have two tables:
| USER |
| _id | - int id PK
| id | - String id
| name |
| SUBSCRIBE |
| user |
| subscribe |
and I want to extract this data:
| _id | id | name | subscribe
Simple example
| USER |
| 1 | 10212551 | Mike
| 2 | 21022145 | Nick
| SUBSCRIBE |
| 1 | 2
get_info($user, $sub );
| EXPECTED RESULT |
| $user | id | name | 0 or 1 (false or true, if $sub has subscribed to $user)
My best try was with count():
SELECT u._id,u.id,u.name,u.email,u.country,count(s.subscribe) AS subscribe
FROM user u,subscribe s
WHERE u._id='$user' AND s.subscribe='$user' AND s.user='$sub'
But this works only if there's any record in subscribe table. So for
get_info(2,1)
it will give right result but for:
get_info(1,2)
there's nothing :/
Can somebody please help me with this ?
Use left join in order to return user even if he has not subscribed and use case to check for subscribed
select u._id,u.id,u.name,u.email,u.country,
case when s.subscribe is not null then 1 else 0 end as subscribe
from user u
left join subscribe s on (u._id = s.user and s.subscribe ='$sub')
where u._id='$user'
Demo

Ebean ManyToMany: Model A has ManyToMany<Model B> . select count(*) B where B in A's List<B>

The application allows users to select certain keywords (which we monitor the TwitterStream for)
Each Keyword contains a list of the tweet IDs that contain its keyword.
public class Keyword extends Model {
#Id
int id;
String keyword;
#ManyToMany
public List<Tweet> tweets = new ArrayList();
}
public class Tweet extends Model {
#Id
int id;
TimeStamp datetime;
}
I'd like to get some data on how each keyword performs each day by select count(*), datetime grouping them by day. The following SQL query will accomplish what I need.
select datetime, count(*) from tweet t left outer join keyword_tweet on t.id=keyword_tweet.tweet_id group by cast(t.datetime as date) having t.datetime > '2014-02-02';
+---------------------+----------+
| datetime | count(*) |
+---------------------+----------+
| 2014-02-02 13:27:45 | 1 |
| 2014-02-08 05:14:04 | 2 |
| 2014-02-09 08:34:31 | 1 |
| 2014-02-12 12:42:02 | 1 |
| 2014-02-13 06:00:09 | 2 |
| 2014-02-14 00:47:04 | 2 |
| 2014-02-15 07:26:30 | 6 |
| 2014-02-16 01:00:00 | 21 |
| 2014-02-17 00:06:50 | 916 |
| 2014-02-18 18:08:56 | 1 |
| 2014-02-19 01:28:40 | 1 |
| 2014-02-24 16:45:11 | 1 |
| 2014-02-26 14:43:54 | 4 |
| 2014-02-27 08:24:09 | 9 |
| 2014-02-28 05:08:16 | 411 |
+---------------------+----------+
How do I select * from Tweets where Tweet.id is IN Keyword.tweets ?
Also how in Ebean would I get a List that only contains Dates, and Count(*)?
Thanks guys!
You can use something along these lines:
Keyword targetKeyword = Keyword.find.byId(1L);
List<Tweets> foundTweets = new ArrayList<Tweets>();
for(Tweet tw : Tweets.find.all()) {
if(targetKeyword.tweets.contains(tw))
foundTweets.add(tw);
}
return foundTweets;
This code will return all tweets contained in keyword id number 1. You can place this in a function passing the keyword id you want instead of 1L.