mu92's avatar
Level 1

eloquent model with condition where,

how i make a query with conditional where, suppose we have a table like this tableName: 'example' columns are: id, name, title, type, status

id | name | title | type | status 1 | a | t1 | 1 | 3 2 | b | t2 | 2 | 1 3 | c | t3 | 1 | 1 4 | c | t4 | 2 | 5

now i want to fetch the record if type ==1 then ignore the status if type ==2 then status must be 5

thanks in adv

0 likes
6 replies
zainudinnoori's avatar

I didn't understand what you wanna do, but in your case you can use

switch($type)
{
    case(1): ..............;
    case(2): ..............;
    case(3): ..............;
    default: ..............;
}

to check the condition and then proceed based on each case.

Bercove's avatar

I assume u already has models so let's say you have a model called Type then do this $status = Type::all(); foreach($tatus as $s){ if($s->type == 1){ #code } else if($s->type == 2 && $s->status == 5){ #code } else{ #code }

}

mdeorue's avatar

I think the better way to do that is an accessor from the model, for example

class Example{
    
    public function getStatusAttribute()
    {
        switch($this->attributes['type'])
        {
            case(1):
                return null;
                break;
            case(2):
                return 5;
                break;
            case(3):
                ...;
            default:
                ...;
        }
    }
    
}

So when you want the final status, your can use

$example->status;
jlrdw's avatar

I do something like

        $query = Dog::where('dogname', 'like', $dogsch);
        if ($aval == "n") {
            $query->where('adopted', '=', 1);
        } else if ($aval == "y") {
            $query->where('adopted', '=', 0);
        }
        $dogs = $query->orderBy('lastedit', 'DESC')->paginate(5);
mu92's avatar
Level 1

bro actually, problem is the, let suppose I wrtoe this

ABC::where('a_id', $a->id)
                        ->where('b_id', $b_id)
                        ->where(function ($query) {
                            $query->where('type', '1'); 
                        })->orWhere(function($query) {
                                $query->where('type', '2') 
                                        ->where('status_id', '=', 5);
                        })
                        ->with(['logs' => function ($q) {
                            $q->where('status_id', 3)->latest(); 
                        }])
                        ->get();

I am unable to mange where and orwhere , i want when type is 1 its ok, if type is 2 then status must be 5,

mu92's avatar
mu92
OP
Best Answer
Level 1

finally i got it how to make,

ABC::where('a_id', $a->id)
                        ->where('b_id', $b_id)
                        ->where(function ($query) {
                            $query->where('type', '1')
                    ->orWhere('type', '2') 
                                        ->where('status_id', '=', 5);
                        ->with(['logs' => function ($q) {
                            $q->where('status_id', 3)->latest(); 
                        }])
                        ->get();

now it works fine

Please or to participate in this conversation.