Farshad
Farshad

Reputation: 2000

merge 2 collection into another with one same value in both laravel

hi i have 2 objects in laravel like below :

{
user_id: 34993,
price: "20000.00"
},
{
user_id: 35153,
price: "20000.00"
},
{
user_id: 35168,
price: "20000.00"
},

and the secound one is the user object :

[
{
id: 34993,
uuid: "8f8f2ba3-0d51-4fdb-83cd-6e13a0f2cc13",
fullname: "َUsersName",
email: null,
created_at: "2020-01-15 14:42:45",
updated_at: "2020-01-15 14:43:58",
email_verified_at: null,
gender: 2,
title: null
},
{
id: 35153,
uuid: "8f8f2ba3-0d51-4fdb-83cd-6e13a0f2cc13",
fullname: "َUsersName",
email: null,
created_at: "2020-01-15 14:42:45",
updated_at: "2020-01-15 14:43:58",
email_verified_at: null,
gender: 2,
title: null
},
{
id: 35168,
uuid: "8f8f2ba3-0d51-4fdb-83cd-6e13a0f2cc13",
fullname: "َUsersName",
email: null,
created_at: "2020-01-15 14:42:45",
updated_at: "2020-01-15 14:43:58",
email_verified_at: null,
gender: 2,
title: null
},

so now how can i add the price into the user object in second object or some how merge them with the key of user_id in first object and id of secound object because they are the same ?? my code is like below :

    {

        $data = DB::table('users_tracker')->where('user_id','>','34676')->where('action','LIKE','%gatewayResponse%')
            ->leftJoin('gateway_transactions',function($join){
                $join->on('gateway_transactions.ip','=','users_tracker.ip');
            })
            ->where('gateway_transactions.status','=','SUCCEED')
            ->select('user_id','price')
            ->distinct()
            ->get();
//        return $data;

       $user_id = $data->pluck('user_id');
        $users = User::whereIn('id',$user_id)->get();
        return $users;
    }

Upvotes: 0

Views: 269

Answers (1)

TsaiKoga
TsaiKoga

Reputation: 13394

Use User to join the users_tracker like this:

    User::join('users_tracker', 'users_tracker.user_id', '=', 'users.id')
              ->where('users_tracker.user_id','>','34676')
              ->where('users_tracker.action','LIKE','%gatewayResponse%')
              ->leftJoin('gateway_transactions',function($join){
                $join->on('gateway_transactions.ip','=','users_tracker.ip');
               })
              ->where('gateway_transactions.status','=','SUCCEED')
              ->select('*', 'users_tracker.user_id', 'gateway_transactions.price')
              ->groupBy('users_tracker.user_id', 'gateway_transactions.price')
              ->get();

It seems you have only_full_group_by problem.

Set the sql_mode in your config/database.php

'mysql' => [
  ...
   'modes' => [
       'STRICT_ALL_TABLES',
       'ERROR_FOR_DIVISION_BY_ZERO',
       'NO_ZERO_DATE',
       'NO_ZERO_IN_DATE',
       'NO_AUTO_CREATE_USER',
   ],
 ];

Remember to clear your config cache:

php artisan config:clear
php artisan optimize

Upvotes: 1

Related Questions