[
29
]
We can also add the database name to our Homestead configuration file, so that the
database is created automatically for us. Open the file from the command line, using
the following command:
$ homestead edit
Under the
databases
section, add a new line:
databases:
- homestead
- furbook
Save the file, then run the
homestead provision
command to create the database.
Creating Eloquent models
The first and easiest step is to define the models with which our application is going
to interact. At the beginning of this chapter, we identified two main entities: cats
and breeds. Laravel ships with Eloquent, a powerful ORM that lets you define these
entities, map them to their corresponding database tables, and interact with them
using PHP methods, rather than raw SQL. By convention, they are written in the
singular form; a model named
Cat
will map to the
cats
table in the database, and a
hypothetical
Mouse
model will map to the
mice
table. You can also manually define
the name of the database table using the aptly-named
$table
property, in case your
table name doesn't follow the convention expected by Laravel:
protected $table = 'custom_table_name';
The
Cat
model, saved at
app/Cat.php
, will have a
belongsTo
relationship with the
Breed
model, which is defined in the following code snippet:
use Illuminate\Database\Eloquent\Model;
class Cat extends Model {
protected $fillable = ['name','date_of_birth','breed_id'];
public function breed() {
return $this->belongsTo('Furbook\Breed');
}
}
Your First Application
Do'stlaringiz bilan baham: |