Birmingham mumbai


Download 1.3 Mb.
Pdf ko'rish
bet60/68
Sana05.01.2022
Hajmi1.3 Mb.
#227543
1   ...   56   57   58   59   60   61   62   63   ...   68
Bog'liq
Laravel 5 Essentials

[

 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


Download 1.3 Mb.

Do'stlaringiz bilan baham:
1   ...   56   57   58   59   60   61   62   63   ...   68




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling