Birmingham mumbai


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

[

 30 

]

The 


$fillable

 array defines the list of fields that Laravel can fill by mass assignment

which is a convenient way to assign attributes to a model. By convention, the column 

that Laravel will use to find the related model has to be called 

breed_id

 in the 


database. The 

Breed


 model, 

app/Breed.php

, is defined with the inverse 

hasMany


 

relationship as follows:



use Illuminate\Database\Eloquent\Model;

class Breed extends Model {

  public $timestamps = false;

  public function cats(){

    return $this->hasMany('Furbook\Cat');

  }

}

By default, Laravel expects a 



created_at

 and 


updated_at

 timestamp field in the 

database table. Since we are not interested in storing these timestamps with the 

breeds, we disable them in the model by setting the 

$timestamps

 property to 

false

:

protected $timestamps = false;



This is the entire code that is required in our models for now. We will discover 

various other features of Eloquent as we progress in this book; however, in this 

chapter, we will primarily use two methods: 

all()


 and 

find()


. To illustrate their 

purpose, here are the SQL queries that they generate:

Furbook\Breed::all()     => SELECT * FROM breeds;

Furbook\Cat::find(1)     => SELECT * FROM cats WHERE id = 1;

The properties of an Eloquent model can be retrieved with the 

->

 operator:  



$cat->name

. The same goes for the properties of the related models, which are 

accessible with: 

$cat->breed->name

. Behind the scenes, Eloquent will perform the 

necessary SQL joins.




Download 1.3 Mb.

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




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