jeudi 31 décembre 2015

Eloquent where clause with multiple condtion filter

In my database table i have A TABLE named student add i want to fetch the specific student who has all the following condition stored in filters array with some other option ("like name can be middlename or lastname")

$filters = [
                ['type' => 'name', 'value' => 'Abhishek'],
                ['type' => 'roll', 'value' => '0013']
            ];

i am trying to fetch that data with following code

 $student = Student::where(function ($query) use ($filters) {

            foreach($filters as $filter) {
                switch($filter['type']){
                    case 'name' :
                        $query->where('firstname', $filter['value'])
                        ->orWhere('middlename', $filter['value'])
                        ->orWhere('lastname', $filter['value']);
                        break;
                    case 'roll' :
                        $query->where('rollnumber', $filter['value']);
                        break;

                }
            }
        })
        ->get();

i am using advance where clause here but it is running for first parameter(like name) only means i am getting value where name = filters['value'] why it is so



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire