@fsdolphin I suggest you subscribe for this, and soon you will know ins and outs of Eloquent https://leanpub.com/eloquence-emerging
Now, I will show you what Eloquent does for you and what it does not (by itself) - it is covered in the preface of the book:
// Query\Builder
$rows = DB::table('some_table')->where(...)->orderBy(...)->select(...)->join(...)->get();
// Eloquent
$models = SomeModel::where(...)->orderBy(...)->select(...)->join(...)->get();
basically all the methods above belong to the Query\Builder class, no Eloquent feature was used so far, but you already have handful of methods for abstracting your queries. That said, you don't need to leverage Eloquent, if you just want to write your queries easily.
Now, here's what Eloquent can do for you additionally (just a little bit of its powers):
$user = App\User::find($someId); // row is fetched and stored as PHP object with Eloquent features
$user->email = 'new@email.com'; // assign some values to the attributes that reflect table columns
$user->first_name = 'Newname';
$user->save(); // save the updated values in the table
$user->posts; // fetch and return a Collection of Post models, assuming we've setup a relation
$user->posts()->save( // save a new post and associate it to the user
new Post(['title' => 'Some long title of my new post', 'body' => ' .... '])
);
// assuming we have first_name and last_name columns in the table
// but we want to print full name easily:
$user->full_name; // using Eloquents accessor:
public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
// let's use query scopes:
$users = App\User::latest()->active()->adults()->take(10)->get();
// latest is generic method on the Builder class, but active and adults are not.
// we define them on the model in order to make the code easier to write and read.
// example:
public function active($query)
{
$query->where('active', true);
}
public function adults($query)
{
$today = date('Y-m-d');
// assuming MySQL and 18yrs is the age we're looking for
$query->whereRaw('timestampdiff (year, born_on, ?) >= 18', [$today]);
}
@pmall bon travail, gaillard! ;)