I would like to check a SQL query of Laravel builder - laravel

Method Illuminate\Database\Eloquent\Collection::toSql does not exist.
occured when I tried this
My laravel version 6.20
$query = Car::where($criteria)
->get(['id'])->shuffle();
$sql = $query->toSql();
Log::info($sql);

When you execute the query using get() it will return an instance of Illuminate\Database\Eloquent\Collection class. And there's no method named toSql() on the Collection class.
If you want to inspect the sql you should
$query = Car::where($criteria)->select('id');
//Here $query is an instance of Illuminate\Database\Eloquent\Builder
//And it has a method named toSql
$sql = $query->toSql();
Log::info($sql);
$query->get()->shuffle();

You could use dd or dump methods for debugging purposes.
Source: https://laravel.com/docs/6.x/queries#debugging

Related

How to echo the last query after eloquent model in laravel 7?

I am deleting an item like this. The delete method is excuting well but I can't echo the last executed query.
public function deleteItems(Request $request){
$query=null;
switch($request->category){
case('categorylist'):
$category = category::find($request->id);
$query=$category->delete()->toSql();
break;
default:
}
echo json_encode(array('status'=>2, 'msg'=>'Successfully deleted', 'query'=>$query->toSql()));
}
The toSql() method works only on instances of Illuminate\Database\Query\Builder ( query builder as it is usually called ). Methods like where(), orderBy() etc return an instance of query builder, hence toSql() works on them. The delete() method does not return an instance of query builder ( I think it returns Boolean or the id it deleted. Can't remember ). So the toSql() method won't work.
If you just want to get the last executed query, then you could use DB::getQueryLog(). For example
DB::enableQueryLog();
$category->delete();
$queryLog = DB::getQueryLog();
// Now $queryLog will contain all the SQL queries which were executed after the DB::enableQueryLog()
$last = end($queryLog);
$query = $last['query'];
$bindings = $last['bindings'];

How to access the database in Helper Functions in CI4

CI4: I have created database object in Controller, i want to execute helper function which has some queries,
Question: How to access the database in Helper Functions in CI4?
you can use by adding these lines in your helper function
$db = db_connect();
$result = $db->query("SELECT * FROM table")->getResult();
You can access the builder from the model.
$model = new SomeModel();
$builder = $model->builder();
$builder->db //returns the db conn used in model

My relationship is not working?

When i write this code my relationship is not working. How can i write this part of code so that i can access to $article->translations(). Any suggestion?
$date_number = strval(date('m', strtotime($month)));
$articles = DB::table('articles')->whereRaw('MONTH(created_at) ='.$date_number)->where('approved',1)->get();
foreach($articles as $article){
$article->trans = $article->translations()->whereHas('language',function($query) use($current_language_id){
$query->where('id','=',$current_language_id);
})->first();
}
I got it. When i use $articles = Articles::whereRaw('MONTH(created_at) ='.$date_number)->where('approved',1)->get();
When you use query builder's get method it returns an Illuminate\Support\Collection containing the results where each result is an instance of the PHP StdClass object not an Eloquent Model object.
So you can't use the relations on StdClass object, you need an Eloquent object. And for that you should use Eloquent query builder as:
$articles = Article::whereRaw('MONTH(created_at) ='.$date_number)
->where('approved',1)
->get();

Laravel Query Builder in Lumen

When I use Laravel's query builder for my Lumen application with MySQL database, it does not work as expected. I used:
$itemid = DB::table('table1')->where('UserID','=',1)->pluck('ID');
This only returns one value. What is the mistake here?
Try
$itemid = DB::table('table1')->where('UserID','=',1)->get()->pluck('ID');
Here you can read more about why this happen when you use pluck on query
Pluck together with first using Query builder
Update:
I forget that DB::table returns array, so:
$items = DB::table('table1')->where('UserID','=',1)->get();
$itemsById = array_pluck($items, 'ID');
Use first instead of get as get return array.
$itemid = DB::table('table1')->where('UserID','=',1)->first()->pluck('ID');

Magento query __toString() like Zend

i can return this like a string?
Similar Zend Framework ->__toString()
here i select my categories (just name) when is active...
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('is_active', 1);
i want see that like string sql query "SELECT * ..."
(string)$categories->getSelect();
You have to use the getSelect() function and cast the results of that to a string. It will make use of the aforementioned __toString() function and return the results of the query.

Resources