Reputation: 13
I tried to write custom query in model as local scope:
public function scopeLastSync($query)
{
return DB::table('clients')
->select(DB::raw(
"select COUNT(*) as total, (SELECT created_at FROM clients ORDER BY created_at DESC LIMIT 1)"
))->get();
}
But it does not work. How to use easy: DB::query("native SQL query")? I tried:
return DB::raw("select COUNT(*) as total, (SELECT created_at AS date_modify FROM clients ORDER BY created_at DESC LIMIT 1)");
Upvotes: 0
Views: 18251
Reputation: 1068
May be you are searching for this, Write Custom query in laravel 5
For Select --> DB::select('your query here.....');
For Insert --> DB::insert('your query here.....');
For Update --> DB::update('your query here.....');
For Delete --> DB::delete('your query here.....');
For General --> DB::statement( 'your query here....' );
You don't have to write select
inside select(DB::raw(...))
. Try:
return DB::table('clients')
->select(DB::raw(
"COUNT(*) as total, (SELECT created_at FROM clients ORDER BY created_at DESC LIMIT 1)"
))->get();
}
But why to do you want to write query of your own if there is powerful Eloquent Model.
Upvotes: 4