I have a code:
$update= DB::table('Appraiser_commands')->where('cycle_id', $cycleid)->where('user_id',$userId)->update(['mode' => $mode,'Appraiser_commnd'=>$json])->toSql();
echo $update;exit;
I am trying to convert laravel query to mysql using toSql()
But i got an error like
Call to a member function toSql() on integer
Then i tried
DB::table('Appraiser_commands')->where('cycle_id', $cycleid)->where('user_id',$userId)->update(['mode' => $mode,'Appraiser_commnd'=>$json])
DB::enableQueryLog();
$queries = DB::getQueryLog();
dd(end($queries));
But it returns output as 'false' i dint get the expected output.I don't know why this happened.Any help would be appreciated.
Expcted output:
UPDATE table_name
SET Appraiser_commnd=value, mode=value2,...
WHERE cycle_id=some_value
When you call update(), it executes the query and returns the number of affected rows (an integer). You're then calling toSql() on the integer. This is causing the error you are seeing.
If you're trying to see the SQL without running the query, you'll need to do this:
// start the query but don't execute it
$query = DB::table('Appraiser_commands')
->where('cycle_id', $cycleid)
->where('user_id', $userId);
// use the grammar object to get the update sql
$sql = $query
->getGrammar()
->compileUpdate($query, ['mode' => $mode, 'Appraiser_commnd' => $json]);
If you do want to execute the query, but also get the SQL, you can do this:
// start the query but don't execute it
$query = DB::table('Appraiser_commands')
->where('cycle_id', $cycleid)
->where('user_id', $userId);
// use the grammar object to get the update sql
$sql = $query
->getGrammar()
->compileUpdate($query, ['mode' => $mode, 'Appraiser_commnd' => $json]);
// actually run the update statement
$updated = $query
->update(['mode' => $mode, 'Appraiser_commnd' => $json]);
Additionally, you could view the sql in the query log, but you must enable the query log before executing the statement:
// enable the query log
DB::enableQueryLog();
// execute the update
DB::table('Appraiser_commands')
->where('cycle_id', $cycleid)
->where('user_id', $userId)
->update(['mode' => $mode, 'Appraiser_commnd' => $json]);
// view the query log
dd(DB::getQueryLog());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With