phplaravel-5lumen-5.4

Select Only False Data from Database using lumen


I am new to coding, I'm trying to build an API using lumen. Now I have a problem, I cannot find out the solution. here is my code.

$data = Speech::select('select * from speeches where is_requested = 0');
return response()->json(['status'=> 'Success', 'data' => $data], 200);

I went to find out when "is_requested" is false. Now it returns me a blank array. please help me if possible.{of course, it's so easy for you guys... :) }


Solution

  • If you are using eloquent ($app->withEloquent();) in your bootstrap/app.php file, you can do it like this :

    <?php
    $data = Speech::where('is_requested', 0)->get();
    return response()->json(['status'=> 'Success', 'data' => $data], 200);
    

    Without eloquent, just use the database connection like this :

    $data = app('db')->select("SELECT * FROM speeches WHERE is_requested = ?", [0]);
    

    Or if the facades are enabled :

    $data = DB::select("SELECT * FROM speeches WHERE is_requested = ?", [0]);