Laravel Lumen - Eloquent Query Log - laravel

I'm using Laravel Lumen to build an API.
I've come to a point where I need to find out what SQL query is being generated by Eloquent. I know how to do this in Laravel 4 and Laravel 5 but i've tried the same code in Lumen and the query is blank?
$queries = DB::getQueryLog();
$last_query = end($queries);
echo 'Query<pre>';
print_r($last_query);
exit;
The above code, when run in Laravel works fine - in Lumen the query is blank?

To get the query log in Laravel Lumen working you need to enable it:
DB::connection()->enableQueryLog();
You can add that code in to your controller, middleware, etc then use:
$queries = DB::getQueryLog();
$lastQuery = end($queries);
dd($lastQuery)
To print your query.
You can also use the following with eloquent:
$myModel = Users::where('active', true);
dd($myModel->getSql(), $myModel->getBindings());
You must run the getSql() and getBindings() before you call ->first() or ->get(), etc

Just call this after the query to keep it quick and simple:
echo $query->toSql();

Related

Get query string parameters are lost when using pagination links in laravel

I have tried to use pagination with laravel and everything works fine
until I navigate to another page, then the request doesn't preserve the previous query string and, as a result, the query doesn't work anymore.
This is the query in the controller:
$inputDate =$request->input('date') ;
$endDate = date('Y-m-d', strtotime($inputDate. ' + 7 days'));
$shifts = DB::table('driver_shifts')
->join('drivers','drivers.PermitNumber','=','driver_shifts.driver_badge_id')
->where ('driver_shifts.shift_start_time','>',$inputDate)
->where ('driver_shifts.shift_start_time','<',$endDate)
->orderby("vehicle_id")
->orderby("shift_start_time")
->paginate(20);
return view('reports.WeeklyTaxiShifts.WeeklyTaxiShifts', compact('shifts','inputDate'));
You can try to use:
$shifts = DB::table('driver_shifts')
->join('drivers','drivers.PermitNumber','=','driver_shifts.driver_badge_id')
->where ('driver_shifts.shift_start_time','>',$inputDate)
->where ('driver_shifts.shift_start_time','<',$endDate)
->orderby("vehicle_id")
->orderby("shift_start_time")
->paginate(20)
// this line will put back the query string parameters on the generated links
->appends(request()->query());
That is also explained with a blade usage example in the documentation

laravel api handler how to order by a child column

I'm using laravel API handler
and I was wondering on how to send a request to order by a column from 'with' table
Here is my backend code
$query = Product::query();
$query->company();
$query->with('category'); $result=ApiHandler::parseMultiple($query);
return $result->getResponse();
and the request-url
http://localhost/public/api/v1/product/view/0?&_offset=0&_limit=10&_config=meta-total-count,meta-filter-count&_sort=-category.category_name
$query = Product::getModel()
->newQuery()
->select('products.*','categories.*')
->join('categories','product.category_id,'=','categories.id')
->orderBy('category.sort_order');
Something like that should do.

How to paginate in Laravel when using Modell::find(array())

I'm grabbing some data in Laravel using the find method and only grabbing the id's I want:
$my_ids = array(1,4,5,10);
$results = Model::find($my_ids);
However, if I try to paginate via Model::find($my_ids)->paginate(10), it throws the error: Call to undefined method Illuminate\Database\Eloquent\Collection::paginate().
How can I query to only get specific model Id's back from the database while also using pagination?
Turns out I can use this syntax:
$results = Model::whereIn('id', $my_ids)->paginate(10);
I suggest use the eloquent query builder:
$results = Model::whereIn('id', $my_ids)->paginate(10);

How to do this query with Laravel query builder by more mysql and php

'm trying to run this query in Laravel, and would like to do it with the query and php builder, so I can use the paginate() method. But I can't seem to figure out how to get this subquery going.
foreach ($array AS &$value)
{
if (strlen($value)==3)
$l3[]="'".$value."'";
else
$l4[]="'".$value."'";
$valuex[]=$value;
}
if ($l4 && $l3)
$where.="AND (left(field,3) IN (".join(", ",$l3).") || left(field,4) IN (".join(", ",$l4)."))";
else if ($l3)
$where.="AND (left(field,3) IN (".join(", ",$l3)."))";
else
$where.="AND (left(field,4) IN (".join(", ",$l4)."))";
$sql=query("SELECT * FROM simso where field $where");
I want to convert to laravel .
Simplemodel::where(..........
Thanks in advance!
You can use Simplemodel::whereRaw($where)->get();

Laravel Pagination with appends() Error

I am new to Laravel and am using version 4.1.
I am attempting to query database using pagination and then run the results through the appends() function to add additional parameters to my URL.
Here is the code I am using
$query = DB::table('tableName');
$query->paginate(50);
$results = $query->get();
And that runs as desired. Now when I attempt to create the pagination list (Bootstrap default) and run the following code I get an error.
$pagination = $results->appends(array('key' => 'value'))->links();
This is the error I receive.
Call to a member function appends() on a non-object
I know I'm doing something wrong, I just can't figure out what...
Thanks in advance,
SC
I'm not familiar with the appends function, but you do have an error I can see. Try changing
$query = DB::table('tableName');
$query->paginate(50);
$results = $query->get();
To...
$query = DB::table('tableName');
if(Input::has('someinput')) {
$query->where('someinput', Input::get('someinput'));
}
if(Input::has('otherinput')) {
$query->where('otherinput', Input::get('otherinput'));
}
$results = $query->paginate(50);
Once you run paginate(), an instance of Paginator is returned. I don't see a get() method for Paginator though so I'm not sure how you weren't getting an error there.

Resources