How to get the last query executed in Laravel 5.3? - php

I try like this :
public function displayList()
{
\DB::enableQueryLog();
$query = Self::orderBy('program_code')->orderBy('output_code')
->orderBy('account_code')->all();
dd(\DB::getQueryLog());
return $query;
}
The result is like this :
[]
It displays an empty array
Is there anyone can help me?

Most suitable way to go about this is listen to db queries. You can do
\DB::listen(function ($query) {
dump($query->sql);
dump($query->bindings);
dump($query->time);
})
in your route file. this will dump out executing db queries. But if you want a much cleaner approach you could wrap above listener inside laravel logger like this.
\Log::info(
\DB::listen(function ($query) {
dump($query->sql);
dump($query->bindings);
dump($query->time);
})
);
then the output will be dump into your-app/storage/logs/laravel.log.
NOTE: Keep in mind to remove or comment out above codes as they are for development purpose only.
Further, you could put it in AppServiceProvider like it is mentioned in database transactions

I suggest you use the package Laravel debugbar: https://github.com/barryvdh/laravel-debugbar. It shows you list of queries executed, and other useful data you want to see.

Related

Laravel DB::listen get controller & method from which the query was called

As suggested in https://laravel.com/docs/5.5/database
I have
DB::listen(function(query) {
....
});
to log a query from my Laravel application.
I'd also like to find out which controller or function that this query was called from. How could I achieve that? Is that possible?
As long as the query is being fired during a request you should be able to use this.
DB::listen(function($query) {
dd($this->app->request->route()->action['uses']);
});
output: "App\Http\Controllers\SomeController#someAction"
There is also $this->app->request->route()->action['controller']
There may be ways to use $this-app to reach the controller in none request situations.

View composer runs multiple times, how to reduce to 1

I made a view composer in Laravel 5. When i use a wildcard *, to add something to all my views, it get's called at least twice. It runs when my master template is loaded, and again when my content page is included. This will give problems in the future, because it executes the query it does multiple times. I was able to fix the multiple querying by storing it in a static variable :
class StoreComposer {
static $composed;
public function __construct(StoreRepository $repository)
{
$this->store = $repository;
}
public function compose(View $view)
{
if(static::$composed)
{
return $view->with('store', static::$composed);
}
static::$composed = $this->store->pushCriteria( new WithCategories() )
->pushCriteria( new WithSettings() )
->applyCriteria()
->all();
$view->with('store', static::$composed);
}
}
My question is, is there a way to make sure it only runs once, no matter how many views i load, or is there another solution to this? The way i fixed it now doesn't feel right to me. Thanks!
Unfortunately no, there is no way to make it run once, due to the way View Composers are implemented. The Illuminate\View\View::renderContents() method is responsible for calling the composer bound to a view, and since any form of view rendering (Blade template inheritance or simple #include statements) executes that method, it means that when any view is rendered any composer bound to it gets triggered.
Since in your case you use a * wildcard to bind all views, if your page renders ten views, the composer will get executed ten times. However your approach looks like a good solution to solve this shortcoming.
You can use config here to resolve multiple times query run issue for example show below code.
public function compose(View $view)
{
if(!Config::has('composeVars'))
{
Config::set('composeVars') = [
'data' => User::all();
];
}
$view->with('*', Config::get('composeVars'));
}
Try this singleton solution or use cache https://laracasts.com/discuss/channels/laravel/executing-a-view-composer-only-once
On Laravel 5.6.38 works fine

Laravel query screws my variables

Controller:
public function getSpecificPost($id)
{
$returnArray = with(new Posts)->getSpecificPost($id);
print_r($returnArray);
}
?>
Model:
public function getSpecificPost($post_id)
{
//exit($post_id);
return DB::table('posts')->where('id', $post_id)->toSql();
}
?>
If I uncomment the exit, it gives me a 1 as return.
When I comment the exit I of course get a query, the query is as follows:
select * frompostswhereid= ?
Its Laravel Framework, help me out please!
The ? is a placeholder for your variable. It will be replaced with (in this case) 1. See the colored block on this page:
Note: The Laravel query builder uses PDO parameter binding throughout
to protect your application against SQL injection attacks. There is no
need to clean strings being passed as bindings.
You are aware of the fact that your don't actually execute the query? If you want to get the specific post with $post_id, you can change the line in your model to this one:
return DB::table('posts')->where('id', $post_id)->first();
If you want to see all executed queries (with replaced variables!) with Eloquent, you can use Laravel PHP Debugbar.

Codeigniter does not execute delete query

I have a problem, when I try to run this function in my model it does nothing. The print statement prints out. 
DELETE FROM child_participantsWHERE Child_Name='test'
andParent_username='tester2'
Which when I run from command line works correctly(the record exists and is deleted). But when I try it from my web application it gives me no error but does not actually delete anything. I know i am passing data correctly because I receive it in my controller and model. What gives?
function remove_child($username, $participant_name)
{
$where = "`Child_Name`='$participant_name' and`Parent_username`='$username'";
$this->db->where($where, null, false);
$this->db->delete($this->child_table);
echo $this->db->last_query();
}
From the documentation:
If you use multiple function calls they will be chained together with AND between them:
Try changing:
$where = "`Child_Name`='$participant_name' and`Parent_username`='$username'";
to
$this->db->where('Child_Name', $participant_name);
$this->db->where('Parent_username', $username);
// translates to WHERE Child_Name='XXX' and Parent_username='XXX'
Hope this helps!
Do you get the same results when you break it out into two where method calls? I would do this over how you are using the where method.
$this->db->where('Child_Name',$participant_name);
$this->db->where('Parent_username',$username);
$this->db->delete($this->child_table);
also, turn on the profiler to see all the queries that are being run to make sure there are not other parts of code we cannot see that might be interfering or a transaction not being committed
$this->output->enable_profiler(TRUE);
Another suggestion is the practice of soft deletes so that way your data is not truly gone and also minimizes how much you need to rely on reconstructing your log file. Also to make simple CRUD operations faster you can use a very simple extension of the base model. One that I have used by recommendation is https://github.com/jamierumbelow/codeigniter-base-model
Check that does your user has delete privilege in the database. if it has than change your
code like this:
function remove_child($username, $participant_name)
{
$this->db->trans_start();
$this->db->where('Child_Name',$participant_name);
$this->db->where('Parent_username',$username);
$this->db->delete($this->child_table);
$this->db->trans_complete();
return TRUE;
}
i hope that this will solve your problem.

Get the Query Executed in Laravel 3/4

How can I retrieve the raw executed SQL query in Laravel 3/4 using Laravel Query Builder or Eloquent ORM?
For example, something like this:
DB::table('users')->where_status(1)->get();
Or:
(posts (id, user_id, ...))
User::find(1)->posts->get();
Otherwise, at the very least how can I save all queries executed to laravel.log?
Laravel 4+
Note for Laravel 5 users: You'll need to call DB::enableQueryLog() before executing the query. Either just above the line that runs the query or inside a middleware.
In Laravel 4 and later, you have to call DB::getQueryLog() to get all ran queries.
$queries = DB::getQueryLog();
$last_query = end($queries);
Or you can download a profiler package. I'd recommend barryvdh/laravel-debugbar, which is pretty neat. You can read for instructions on how to install in their repository.
Laravel 3
In Laravel 3, you can get the last executed query from an Eloquent model calling the static method last_query on the DB class.
DB::last_query();
This, however, requires that you enable the profiler option in application/config/database.php. Alternatively you could, as #dualed mentioned, enable the profiler option, in application/config/application.php or call DB::profile() to get all queries ran in the current request and their execution time.
You can enable the "Profiler" in Laravel 3 by setting
'profiler' => true,
In your application/config/application.php and application/config/database.php
This enables a bar at the bottom of each page. One of its features is listing the executed queries and how long each one took.
For Eloquent you can just do:
$result->getQuery()->toSql();
But you need to remove the "->get()" part from your query.
I would recommend using the Chrome extension Clockwork with the Laravel package https://github.com/itsgoingd/clockwork. It's easy to install and use.
Clockwork is a Chrome extension for PHP development, extending
Developer Tools with a new panel providing all kinds of information
useful for debugging and profiling your PHP scripts, including
information on request, headers, GET and POST data, cookies, session
data, database queries, routes, visualisation of application runtime
and more. Clockwork includes out of the box support for Laravel 4 and
Slim 2 based applications, you can add support for any other or custom
framework via an extensible API.
Since the profiler is not yet out in Laravel 4, I've created this helper function to see the SQL being generated:
public static function q($all = true)
{
$queries = DB::getQueryLog();
if($all == false) {
$last_query = end($queries);
return $last_query;
}
return $queries;
}
NOTE: Set the $all flag to false if you only want the last SQL query.
I keep this sort of functions in a class called DBH.php (short for Database Helper) so I can call it from anywhere like this:
dd(DBH::q());
Here is the output I get:
In case you are wondering, I use Kint for the dd() formatting.
http://raveren.github.io/kint/
For Laraver 4 it's
DB::getQueryLog()
Here is a quick Javascript snippet you can throw onto your master page template.
As long as it's included, all queries will be output to your browser's Javascript Console.
It prints them in an easily readable list, making it simple to browse around your site and see what queries are executing on each page.
When you're done debugging, just remove it from your template.
<script type="text/javascript">
var queries = {{ json_encode(DB::getQueryLog()) }};
console.log('/****************************** Database Queries ******************************/');
console.log(' ');
queries.forEach(function(query) {
console.log(' ' + query.time + ' | ' + query.query + ' | ' + query.bindings[0]);
});
console.log(' ');
console.log('/****************************** End Queries ***********************************/');
</script>
Laravel 5
Note that this is the procedural approach, which I use for quick debugging
DB::enableQueryLog();
// Run your queries
// ...
// Then to retrieve everything since you enabled the logging:
$queries = DB::getQueryLog();
foreach($queries as $i=>$query)
{
Log::debug("Query $i: " . json_encode($query));
}
in your header, use:
use DB;
use Illuminate\Support\Facades\Log;
The output will look something like this (default log file is laravel.log):
[2015-09-25 12:33:29] testing.DEBUG: Query 0: {"query":"select * from
'users' where ('user_id' = ?)","bindings":["9"],"time":0.23}
***I know this question specified Laravel 3/4 but this page comes up when searching for a general answer. Newbies to Laravel may not know there is a difference between versions. Since I never see DD::enableQueryLog() mentioned in any of the answers I normally find, it may be specific to Laravel 5 - perhaps someone can comment on that.
You can also listen for query events using this:
DB::listen(function($sql, $bindings, $time)
{
var_dump($sql);
});
See the information from the docs here under Listening For Query Events
Using the query log doesnt give you the actual RAW query being executed, especially if there are bound values.
This is the best approach to get the raw sql:
DB::table('tablename')->toSql();
or more involved:
$query = Article::whereIn('author_id', [1,2,3])->orderBy('published', 'desc')->toSql();
dd($query);
If you are using Laravel 5 you need to insert this before query or on middleware :
\DB::enableQueryLog();
Or as alternative to laravel 3 profiler you can use:
https://github.com/paulboco/profiler
or
https://github.com/barryvdh/laravel-debugbar
in Laravel 4 you can actually use an Event Listener for database queries.
Event::listen('illuminate.query', function($sql, $bindings)
{
foreach ($bindings as $val) {
$sql = preg_replace('/\?/', "'{$val}'", $sql, 1);
}
Log::info($sql);
});
Place this snippet anywhere, e.g. in start/global.php. It'll write the queries to the info log (storage/log/laravel.log).
Event::listen('illuminate.query', function($sql, $param)
{
\Log::info($sql . ", with[" . join(',', $param) ."]<br>\n");
});
put it in global.php it will log your sql query.
The Loic Sharma SQL profiler does support Laravel 4, I just installed it. The instructions are listed here. The steps are the following:
Add "loic-sharma/profiler": "1.1.*" in the require section
in composer.json
Perform self-update => php composer.phar self-update in the console.
Perform composer update => php composer.phar update loic-sharma/profiler in the console as well
`
Add 'Profiler\ProfilerServiceProvider', in the provider array in
app.php
Add 'Profiler' => 'Profiler\Facades\Profiler', in the
aliasses array in app.php as well
Run php artisan config:publish loic-sharma/profiler in the console
Last query print
$queries = \DB::getQueryLog();
$last_query = end($queries);
// Add binding to query
foreach ($last_query['bindings'] as $val) {
$last_query['query'] = preg_replace('/\?/', "'{$val}'", $last_query['query'], 1);
}
dd($last_query);
L4 one-liner
(which write query):
$q=\DB::getQueryLog();dd(end($q));
Laravel 3
Another way to do this is:
#config/database.php
'profiler' => true
For all Queries result:
print_r(DB::profiler());
For last Result:
print_r(DB::last_query());
To get the last executed query in laravel,We will use DB::getQueryLog() function of laravel it return all executed queries. To get last query we will use end() function which return last executed query.
$student = DB::table('student')->get();
$query = DB::getQueryLog();
$lastQuery = end($query);
print_r($lastQuery);
I have taken reference from http://www.tutsway.com/how-to-get-the-last-executed-query-in-laravel.php.
There is very easy way to do it, from your laravel query just rename any column name, it will show you an error with your query.. :)
In Laravel 8.x you can listen to the event by registering your query listener in a service provider as documented in laravel.com website.
//header
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
public function boot()
{
DB::listen(function ($query) {
Log::debug("SQL : " . $query->sql);
});
}
You can then see all the queries in the laravel.log file inside storage\logs\laravel.log

Categories