Skip to content
Advertisement

How to add extra parameter in with condition in laravel

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,
    },
]

Advertisement

Answer

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

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

use IlluminateSupportFacadesDB;

Then:

$q->select('id', 'uuid', 'user_id', 'to_user_id', DB::raw("1 as 'type'"));
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement