how to print SQL statement in Laravel 5 - php

As you know, we can use the method DB::getQueryLog() to print the sql statement in Laravel 4.
But it doesn't work in Laravel 5.

Query log is disabled by default in L5. Call
DB::connection()->enableQueryLog();
$queries = DB::getQueryLog();
before trying to log it. See the docs for more info
.

if you want to see the currently running SQL query you can use this
https://github.com/itsgoingd/clockwork
installation instructions are clearly mentioned in the README file

Related

How to get sql query in cakePHP

I am using cakephp framework. I need SQL for following statement:
$vehicle = $this->Vehicle->find('all');
How can I do that?
Please guide me.
Thank you,
Trupti
For Cakephp 1.x, you could use following code to get the last query
$dbo = $this->Vehicle->getDatasource();
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
echo $lastLog['query'];
Alternatively, To get all the Queries executed in the Current HTTP Request, you can use the following code
$db =& ConnectionManager::getDataSource('default');
$db->showLog();
You have to set the Debug Mode to 2 for this to work.

Laravel and stored procedures SQL

I have a stored procedure that client has made.
Now I have to access it and process data in my laravel app.
I have no problems with connecting to SQL but the problems starts when passing in parameters.
Here is my code:
$connections = DB::connection('sqlsrv')
->select(EXEC [stored].[procedure].[here] $param1, $param2);
The error I got back is:
The active result for the query contains no fields
I have searched for many hours in forums and here is what I have found:
$connections = DB::connection('sqlsrv')
->select(DB::raw('EXEC [stored].[procedure].[here] $param1, $param2'));
Error I got back is:
The active result for the query contains no fields.
I have installed SQL driver and OBDC driver.
Any help would be much appreciated
Don't know if this will help but this answer suggests you put your DB::raw around just the params instead of the EXEC stored.proc.name.
So instead of
$connections = DB::connection('sqlsrv')->select(DB::raw('EXEC [stored].[procedure].[here] $param1, $param2'));
Try
$connections = DB::connection('sqlsrv')->select('EXEC stored.proc.name' . DB::raw($param1, $param2));
OK. So I found out the issue.
It turns out the client sent me over a variable with xml.
As far as I figured out from Microsoft docs then you cant pass xml as a varable because if you call the SP from an application it is gonna pass it as an array and application cant parse xml into array directly. You have to do it on your own.
What can be done instead is to pass xml as a value of an array and then take it from there.

Laravel how to call a function in sql server

I am working with Laravel and I am using a database in Sql Server. I need to execute a function that was do in Sql Server but I do not how to execute it.
The function in Sql Server is something that it:
Select dbo.functionName (848,95,37)
I try to execute it with this:
DB::connection('database')->select('Select dbo.functionName ?,?,?',array(848,95,37))
but Laravel return it: Cannot access empty property
I hope you can help me.
I had this problem too, i solved that like this:
DB::connection('database')->select('Select dbo.functionName (?,?,?) as result',array(848,95,37))
May be this will work (Added parenthesis)
DB::connection('database')->select('Select dbo.functionName (?,?,?)',array(848,95,37))

Issue in update() function in sqlserver.php in CakePHP

We have developed our app in CakePHP running with MYSQL and this is working fine. PHP version is 5.4.16, Apache 2.4.4, latest XAMP. CakePHP version: 2.3.0
We already have similar Database in MSSQL also. Now we want to run our app with this MSSQL also.
We are using php_pdo_sqlsrv_54_ts.dll and php_sqlsrv_54_ts.dll for MSSQL driver for this. Its sql server 2005.
Whenever we are updating values in tables, we are using UpdateAll() of Model.Php which further calls update() of the corresponding datasource. Our code is like this.
if ($this->Configurationtable->updateAll(array('Configurationtable.configuration_optionsetting' => $limit ),
array('Configurationtable.configuration_optionname =' => $optionName))) {
$this->Session->setFlash('The Result Limit has been updated');
$this->redirect(array('action' => 'edit'));
} else
{
$this->Session->setFlash('The configuration could not be saved. Please, try again.');
}
Now this works fine in MySql and generated the following Update query
UPDATE ipa.configurationtable AS Configurationtable SET Configurationtable.configuration_optionsetting = 12 WHERE Configurationtable.configuration_optionname = 'searchPaginationLimit'
But in MS Sql Server, it fails and generates completely wrong query which is
SELECT [Configurationtable].[id] AS [Configurationtable__id] FROM [configurationtable] AS [Configurationtable] WHERE [Configurationtable].[configuration_optionname] = N'searchPaginationLimit'
Now, If I make Update() function in \lib\Cake\Model\Datasource\Database\Sqlserver.php similar to Update() in \lib\Cake\Model\Datasource\Database\Mysql.php then it works fine. It generates proper correct "Update" query
I could not figure out why is it going so? Is it something I am going wrong OR there is bug in cake?
Any would be of great help.
I would file a bug, but first make sure that upgrading to the latest version doesn't fix your problem. Also see the commit log of Sqlserver.php.

Codeigniter and Doctrine 500 internal error

I've tried to combine Doctrine with Codeigniter and I'm almost there, I think.
The version of Codeigniter is 2.0.2 and from Doctrine is 2.0.0.
The problem is now that I get an 500 internal server error using the flush() method of Doctrine.
I am following a tutorial (http://wildlyinaccurate.com/integrating-doctrine-2-with-codeigniter-2/) to install doctrine with codeigniter. But at the last step it just fails and I don't know why.
This is my code in my controller:
$app = new models\application;
$app->setName("Test applicatie");
$app->setGuid();
$this->doctrine->em->persist($app);
$this->doctrine->em->flush(); //If I comment this out, it loads the view...
$this->load->view('welcome_message');
When I comment out the flush method it loads the view.
Thanks in advance.
Well, since $this->doctrine->em->flush(); is what actually does the write to the DB, Make sure you have the required MySQL user rights to do the DB action.
Write the query directly in MySQL and make sure that the error isn't thrown there.
Make sure to put some content in fields that cannot be null.

Categories