How do I execute a whereIN query in Laravel?

I am trying to execute a request whereIN

in Laravel. The value I want to match is the value from the database table, for example: "a, b, c, d"

I want to select all users who have a name that is in this line above. So I take a string and explode it to create an array and then add that array to the Laravel command. The problem is that it only returns one user that only matches the letter "a" and it stops.

Controller code:

public function returnEorti(){

    $month = Input::get('month');
    $day = Input::get('day');

    $eorti = DB::select("select text from days where day='$day' and month='$month'");
    $eortiFinal = explode(",", $eorti[0]->text);
    $pelatis = Pelatis::whereIn('Onoma', $eortiFinal)->get();

    return json_encode($pelatis);
}

      

ajax:

 $.ajax({
    type:"GET",
    url:"getEorti",
    data:{"day":d[2],"month":d[1]},
    success:function(data){
        var b = jQuery.parseJSON(data)
        for(var i=0;i<Object.keys(b).length;i++){
            $( "#test" ).append( b[i].Onoma+'</br>' );
        }      
        console.log(data);      
    }            
 });

      

And my route:

Route::get('getEorti','EortologioController@returnEorti');

      

Table "Pelates": P_ID ONOMA Epitheto Onoma_Miteras Onoma_Patera Hmerominia_Gennisis Tilefono_Oikias Kinito_Tilefono Diefthinsi Dimos Periferia Periferiaki_Enotita Epaggelma Email

+3


source to share


1 answer


There DB

is a syntax error in the request

$eorti = DB::select("select text from days where day='$day' and month='$month'");



Should be fixed like

eorti = DB::table('days')->whereDay($day)->whereMonth($month)->get();

      

0


source







All Articles