I have this code in a controller (Laravel 5.6):
\DB::enableQueryLog();
$foo->update($data);
dd(\DB::getQueryLog());
The problem is that in the dump there is no update query. I know the update command is running (I can see the updated data in the database). What am I missing?
You might have better luck with:
\DB::connection()->enableQueryLog();
$foo->update($data);
dd(\DB::getQueryLog());
Personal choice, but I would even modify it a bit more to:
\DB::connection()->enableQueryLog();
$foo->update($data);
print_r(\DB::getQueryLog());
die();
You might like the output better.
Try Query Builder:
DB::connection()->enableQueryLog();
DB::connection('your-connection')->update($data);
$queries = DB::getQueryLog();
dd($queries);
If that doesn't work out, you might want to check this out.
Related
I don't know why I started getting the following error while working on Laravel application.
No query results for model [App\Hotspot].
Here is my Model Function to get user's hotspots.
public function hotspots()
{
return $this->hasManyThrough(Hotspot::class, Operator::class, 'id', 'operator_id');
}
and here is how I am executing the query to get data.
$hotspotId = $id;
$hotspot = Auth::user()->hotspots()->findOrFail($hotspotId);
I started getting this error suddenly. I don't know what went wrong! I tried to find solution on internet but they are totally different cases.
As shown in https://laravel.com/docs/5.4/eloquent-relationships, section Has Many Through.
return $this->hasManyThrough(Hotspot::class, Operator::class, 'user_id', 'operator_id', 'id');
This is necesary due to you are first connecting the Hotspots to he User, after that you are connecting he operations to the Hotspots. therefor his order is correct.
Reason for it working certain times, is because of id clashes, if you used Uuids this code would never work. You can also debug this solution with he following.
DB::enableQueryLog();
// General i feel like Auth::user()->hotspots->where('id', $hotspotId)->first() is more Laravel'sh.
// Like this you will query a lot, if used in wrong cases.
$hotspot = Auth::user()->hotspots()->findOrFail($hotspotId);
dd(DB::getQueryLog());
Or you can get the raw sql, sometimes provides you with a different view.
dd(Auth::user()->hotspots()->toSql());
I have a Laravel 5.2.45 application, also, I have a complex query to do, so I tried to consult it using:
DB::select("the query");
I read that this should work, but it's not the case, so just testing I simplified the query to: "Select * from aTable" but it also doesn't give any result, it takes a long long timeloading the webpage and then just doesnt show anything. I'm using this exactly: dd(DB::select("SELECT * FROM myTable AS mt"))
So, I'm wondering what is exaclty happening, it's still a valid function in Laravel 5.2? it's a really simple query and am not sure in what is failing. Thanks in advance!
I think you are trying to execute raw query. If you execute raw query in laravel please try this way:
$tableData = DB::select( DB::raw("SELECT * FROM table WHERE id = 100 ") );
dd($tableData);
You can set custom function also for printing data in your helper function like
function pr($var){
echo "<pre>";
print_r($var);
echo "</pre>";
}
than you can call pr($tableData);
I think this should work for you. Thank You :)
It's strange but you can check your Query Log to find problem
$users = DB::select('SELECT * FROM myTable AS mt');
print_r(DB::enableQueryLog());
How to enable query log
https://github.com/laravel/framework/commit/e0abfe5c49d225567cb4dfd56df9ef05cc297448
Try this:
$users = DB::table('myTable')->get();
I wrote the following eloquent query for a table in my application and now the application won't stop loading. what i need is to understand what is wrong with it and how to fix it
$customer_purchased_1_time=DB::table("data as t1")->select('vin','last_service')
->where('type',1)->distinct('vin')
->whereRaw("(select count(*) from data where t1.vin=data.vin and data.type=1)=1")
->count();
It is the only code in my controller method along with the return statement. the incrementing of x was for debugging
I solved it already. and this code worked fine for me
$customer_purchased_1_time=count(DB::table("data as t1")
->select('vin','last_service',DB::raw("count(*) as count"))
->where('type',1)
->distinct('vin')
->having("count","=",1)
->get());
when I write this $thread = Thread::find($id); then I write {{$thread->title}} it gives me the title of the thread, but when I write $thread = Thread::where('id','=',$id); then I write {{$thread->title}} it gives me an error.why is that happening?
You should write:
$thread = Thread::where('id','=',$id)->first();
to get one column, else laravel will understand it as array.
You need to call the get() (or any of its variants) method to execute the actual query when using where.
Thread::where('id','=',$id)->get();
Otherwise Thread::where('id','=',$id) just gets you an instance of eloquent's query builder.
find() on the other hand will automatically run a query for whatever it is you want to find by you can't do all sorts of useful stuff (e.g. orderBy, paginate, etc.) that you can very easily pull of using the query builder.
In my app/config/app.php file, I have turned on 'debug'=>true.
I have tried
//run eloquent functions
$allMessages = Messages::with('User')->whereIn('conv_id',$conv_id)->orderBy('created_at','aadesc')->take(10);
$q= DB::getQueryLog();
dd($q);
but it returns an empty array. So I guess it is useless to do end($q) as suggested.
I have also tried the accepted answer to a question and added it to the end of my routes file but nothing happened. I'm still new to Laravel and need some guidance. Thank you!
A popular method is to monitor the Event for Eloquent, and output any queries run on the database as you are going along:
Event::listen('illuminate.query', function($query, $params, $time, $conn)
{
dd(array($query, $params, $time, $conn));
});
$allMessages = Messages::with('User')->whereIn('conv_id',$conv_id)->orderBy('created_at','aadesc')->take(10);
That will output the query that is run.
Another option is to use a Laravel4 Debugger package, which automatically shows you the queries run: https://github.com/barryvdh/laravel-debugbar