Randeep Singh Randeep Singh - 1 year ago 65
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']);
$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 Source

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])
            ->selectRaw('SUM(fee), courses.name')