How to do query with Laravel Eloquent

In laravel you can do query to the database using the eloquent ORM (Object Relational Mapping), using eloquent you can create a nice looking and easy to understand database query.

To use eloquent you must have the model class of the database table that you want to use. Every database table should have a model, so that you can use eloquent to play with the database table, such as create record, update record, delete record, etc.

Here's an example getting record with eloquent (select query) from table user
$users = Users::get();

The get() method will return more than one record, if you just need one record use first() instead. You can also search for specific value using 'where' clause like this:
$users = Users::where('user_id', '=', 1)->get();

You can add more than one 'where' clause which makes it equal to 'and' in mysql query
$users = Users::where('user_id', '=', 1)
                         ->where('status', '=', 'active')

Here's an example creating new record with eloquent (insert query) to table user
$new_user = new User();
$new_user->user_id = 2;
$new_user->status = 'active';

Creating new record for table user, first you need to create instance of the user model then fill the records, and finally run the save() method to stored the record. 

Here's an example updating record with eloquent (update query) to table user
$update_user = Users::where('user_id', '=', 1)->first();
$update_user->status = 'inactive';

Updating a record is pretty much the same as create a record, except first you need find the single record that you want to update, update the record and then call the save() method.

These are just some basic eloquent that i share with you guys, there are more advanced features of eloquent that you can learn after understand the basic.