As we know Every DB table has a corresponding "Model" that is helpful to interct with the table.
By using Eloquent we can implement query using MODEL NAME.
The basic query using Eloquent ORM in laravel 4.x are
All Query:
$users = User::all();
Count Query:
Question::find($karmaFeedId)->GiversHelp()->count();
that will count the total row.
Condition Query:
$model = User::where('votes', '>', 100)->firstOrFail();
WhereRaw Query:When you are not able to generate query using Model then for raw condition use whereRaw.
$users = User::whereRaw('age > ? and votes = 100', array(25))->get();
Chunk Result:When we have thoudsand of records and we dont want to use our whole memory then for this
we will use chunk command.
User::chunk(200, function($users)
{
foreach ($users as $user)
{
//
}
});
Update Query:
$affectedRows = User::where('votes', '>', 100)->update(array('status' => 2));
These all are the basic query of laravel 4.x using eloquent.
0 Comment(s)