This query constructor is quite a powerful tool for building and executing SQL queries. You can also direct it to filter, sort and merge tables. For example:
DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
Nevertheless, models are more convenient to work with. You can find the description of Eloquent ORM models and the syntax for querying them at .
This query will return the first 20 customers sorted alphabetically:
$customers = App\Customer::select()
->orderBy('name')
->take(20)
When a model is more complex, its relationships or relationship collections can be retrieved via dynamic attributes. The following query, for example, returns the items of the invoice that has the identifier 1:
$flight = new Flight;
$flight->name = $request->name;
$flight->save();
Updating a record involves finding it, accepting changes to the appropriate attributes and saving it with the save
method:
The method allows a record to be deleted more rapidly by its key value, without needing to retrieve its instance:
App\Flight::destroy(1);