-
Notifications
You must be signed in to change notification settings - Fork 4
eloquent
Foxdb includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well.
⚠️ In this section, we do not have all the facilities that Laravel has, but we have tried to provide useful items for use
Models are placed in the app/Models folder. Let's examine a basic model class and discuss some key Eloquent conventions:
<?php
namespace App\Models;
use Foxdb\Model;
class Flight extends Model
{
//
}
After glancing at the example above, you may have noticed that we did not tell Eloquent which database table corresponds to our Flight model. So, in this case, Eloquent will assume the Flight model stores records in the flights table, while an AirTrafficController model would store records in an air_traffic_controllers table.
If your model's corresponding database table does not fit this convention, you may manually specify the model's table name by defining a table property on the model:
<?php
namespace App\Models;
use Foxdb\Model;
class Flight extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'my_flights';
}
By default, Eloquent expects created_at and updated_at columns to exist on your model's corresponding database table. Eloquent will automatically set these column's values when models are created or updated. If you do not want these columns to be automatically managed by Eloquent, you should define a $timestamps property on your model with a value of false:
<?php
namespace App\Models;
use Foxdb\Model;
class Flight extends Model
{
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
protected $timestamps = false;
}
You may want to personalize and limit the fields that you receive as an output, for this we use the visible
method
<?php
namespace App\Models;
use Foxdb\Model;
class Flight extends Model
{
protected $table = 'my_flights';
protected $visible = ['id', 'title', 'customer_id'];
}
Once you have created a model and its associated database table, you are ready to start retrieving data from your database. You can think of each Eloquent model as a powerful query builder allowing you to fluently query the database table associated with the model. The model's get
method will retrieve all of the records from the model's associated database table:
use App\Models\Flight;
foreach (Flight::get() as $flight) {
echo $flight->name;
}
The Eloquent get
method will return all of the results in the model's table. However, since each Eloquent model serves as a query builder, you may add additional constraints to queries and then invoke the get method to retrieve the results:
$flights = Flight::where('active', 1)
->orderBy('name')
->take(10)
->get();