1// Retrieve a model by its primary key...
2$flight = App\Models\Flight::find(1);
3
4// Retrieve the first model matching the query constraints...
5$flight = App\Models\Flight::where('active', 1)->first();
6
7// Shorthand for retrieving the first model matching the query constraints...
8$flight = App\Models\Flight::firstWhere('active', 1);
1return Destination::orderByDesc(
2 Flight::select('arrived_at')
3 ->whereColumn('destination_id', 'destinations.id')
4 ->orderBy('arrived_at', 'desc')
5 ->limit(1)
6)->get();
1use App\Models\Destination;
2use App\Models\Flight;
3
4return Destination::addSelect(['last_flight' => Flight::select('name')
5 ->whereColumn('destination_id', 'destinations.id')
6 ->orderBy('arrived_at', 'desc')
7 ->limit(1)
8])->get();
1$model = App\Models\Flight::where('legs', '>', 100)->firstOr(function () {
2 // ...
3});
1$model = App\Models\Flight::where('legs', '>', 100)
2 ->firstOr(['id', 'legs'], function () {
3 // ...
4 });
1<?php
2
3namespace App\Models;
4
5use Illuminate\Database\Eloquent\Model;
6
7class Flight extends Model
8{
9 /**
10 * The table associated with the model.
11 *
12 * @var string
13 */
14 protected $table = 'my_flights';
15}
1php artisan make:model Flight --migration
2
3php artisan make:model Flight -m
1<?php
2
3$flights = App\Models\Flight::all();
4
5foreach ($flights as $flight) {
6 echo $flight->name;
7}
1<?php
2
3namespace App\Models;
4
5use Illuminate\Database\Eloquent\Model;
6
7class Flight extends Model
8{
9 /**
10 * The connection name for the model.
11 *
12 * @var string
13 */
14 protected $connection = 'connection-name';
15}