Include comments in Laravel Eloquent queries

Viewed 1111

I would like to know whether or not is possible to perform a query including a comment so that, when auditing, in my SQL server log I see something like:

/*This is for getting John's id*/
SELECT id FROM users WHERE name = 'John';

I know how to query users (in this case) by using the model and something like

User::where(name, 'John');

But, is it possible to send a comment in the same query or before? I have investigated and read the Laravel documentation but I can't find anything similar. The only way I can think of is creating a new query that sends only the comment, but I don't think that's the best way and even if it'll work.

Thanks in advance.

2 Answers

you can add custom log for that situation.

Example:

$query = User::where(name, 'John');
$query->toSql()

// in query you can add your query

and then add your comment

$data = [
    'comment' => 'This is for getting John's id',
    'query' => $query->toSql()
]; 

finally store it in your log file

Log::info($data);

Don't know this will help you or not. I just shared my thinking with you.

Just to close this. @simonecosci commented that:

Since the Eloquent Query Builder is designed to assemble various parts of the query (wheres, orderBy, joins etc) you can't place comments inside the generated SQL, hence you should use raw queries to place your SQL but you loose all the benefits of using an ORM.

Thanks for your answer, that's what I thought :(

Related