How to Make Laravel Eloquent "In" Query

How do I get the query builder to output its raw SQL query as a string?

To output to the screen the last queries ran you can use this:

\DB::enableQueryLog(); // Enable query log

// Your Eloquent query executed by using get()

dd(\DB::getQueryLog()); // Show results of log

I believe the most recent queries will be at the bottom of the array.

You will have something like that:

array(1) {
[0]=>
array(3) {
["query"]=>
string(21) "select * from "users""
["bindings"]=>
array(0) {
}
["time"]=>
string(4) "0.92"
}
}

(Thanks to Joshua's comment below.)

How to Make Laravel Eloquent IN Query?

Here is how you do in Eloquent

$users = User::whereIn('id', array(1, 2, 3))->get();

And if you are using Query builder then :

$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();

Laravel eloquent build query

If I correctly understood your question, you can try do it this way:

$products->where(DB::raw('price - price * discount'), '<=', $max)
->where(DB::raw('price - price * discount'), '>=', $min)
->orderByDesc('id')
->paginate(9);

https://laravel.com/docs/5.8/queries#raw-expressions

Creating and Update Laravel Eloquent

Here's a full example of what "lu cip" was talking about:

$user = User::firstOrNew(array('name' => Input::get('name')));
$user->foo = Input::get('foo');
$user->save();

Below is the updated link of the docs which is on the latest version of Laravel

Docs here: Updated link

Laravel Eloquent Query Where IN statement

You can use the whereIn() like:

  1. Using DB::table()

$data = \DB::table("service_package")
->whereIn("price", ['110010', 'Test 02', '11009'])
->get();

  1. Using Eloquent

$data = App\ServicePackage::whereIn("price", ['110010', 'Test 02', '11009'])
->get();

Here, make sure you have created model ServicePackage for the table service_package.

How to log every query from multiple connections in Eloquent (outside laravel)

To log queries of multiple db connections in sequential order, we'll need to enableQueryLog first, have separate Logger configured.

$capsule->setAsGlobal();
$capsule->bootEloquent();
$container->set('connection', $capsule);

$capsule->connection('<MySqlConnectionName>')->enableQueryLog();
$capsule->connection('<SqlServerConnectionName>')->enableQueryLog();
//$capsule->connection('<MongoConnectionName>')->enableQueryLog();

// Listen
\Illuminate\Database\Capsule\Manager::listen(function($query) {
if($query->connectionName == 'mysql') {
$mysqlLogger->debug('mysql', [
'query' => $query->sql,
'bindings' => $query->bindings
]);
} elseif($query->connectionName == 'sqlserver') {
$sqlServerLogger->debug('mongodb', [
'query' => $query->sql,
'bindings' => $query->bindings
]);
} /*elseif($query->connectionName == 'mongodb') {
$mongoDbLogger->debug('mongodb', [
'query' => $query->sql,
'bindings' => $query->bindings
]);
}*/
});

Laravel Subquery with Query Builder

You should create a relationship between your User model and the model for the covis_transactions table. (I'm gonna call it CovisTransaction)

# User.php
public function covis_transactions()
{
return $this->hasMany(CovisTransaction::class);
}

Then, you can use withCount to get the aggregate count.

User::query()
->select('id', 'nip', 'name')
->withCount('covis_transactions as total_survey')
->where('user_role_id', 7)
->groupBy('id')
->get();



  • https://laravel.com/docs/9.x/eloquent-relationships#one-to-many
  • https://laravel.com/docs/9.x/eloquent-relationships#aggregating-related-models


Related Topics



Leave a reply



Submit