Randeep Singh Randeep Singh - 7 months ago 18
PHP Question

laravel : How to run query for user input in sql?

I have to make a report of some certain data where user will give two date range. so I have to show data in between that range.
how to run query for that user input date?

Here is my controller:

public function reportByDate(Request $request){
$now = $request->Input(['sdate']);
$then=$request->Input(['edate']);
$input = DB::select('SELECT sum(fee), courses.name FROM `fees` JOIN
courses on fees.course_id=courses.id join
users on courses.id =users.course_id
where users.completed_status=1 AND users.date between 'now' AND 'then' group by fees.course_id');

return view('revenueDetails','input'=>$input]);
}


How to pass this two input data (now and then in that sQL Query?

Answer

Try this:

$input = DB::table('fees')->join('courses', 'fees.course_id', '=','courses.id')
            ->join('users', 'users.course_id', '=', 'courses.id')
            ->where('users.completed_status', 1)
            ->whereBetween('users.date', [$now, $then])
             ->groupBy('fees.course_id')
            ->selectRaw('SUM(fee), courses.name')
             ->get();
Comments