Home > Enterprise >  How to add extra parameter in with condition in laravel
How to add extra parameter in with condition in laravel

Time:09-28

I want to add extra parameter type in with condition.

I have no type column in matches table.

$list = self::where('id', $user->id);
$list = $list->with(['matches'=>function($q) use($user) {
    $q = $q->where('user_id', $user->id);
    $q = $q->with(['touser'=>function($q1) {
        $q1 = $q1->select('id', 'name', 'email', 'user_image', 'is_active', 'is_profile_completed');
    }]);
    $q->select('id', 'uuid', 'user_id', 'to_user_id');
}]);
$list = $list->get();

Expeacted Out put is:

"matches": [
    {
        "id": 2,
        "uuid": "dedescvdf",
        "user_id": 128,
        "type": 1,
    },
    {
        "id": 3,
        "uuid": "dedescvdf",
        "user_id": 128,
        "type": 1,
    },
]

CodePudding user response:

If the value of "type" is static you can try something like this:

Add this line (import) at the begging of the file:

use Illuminate\Support\Facades\DB;

Then:

$q->select('id', 'uuid', 'user_id', 'to_user_id', DB::raw("1 as 'type'"));
  • Related