Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Basic Query Using Eloquent ORM in laravel 4.x

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 101
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: