Last active
August 11, 2020 18:43
-
-
Save rydurham/d4e6b426fc3e581700b19d82fdbd0cab to your computer and use it in GitHub Desktop.
Eloquent Model Stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Flight extends Model | |
{ | |
/** | |
* The table associated with the model. | |
* | |
* @var string | |
*/ | |
protected $table = 'my_flights'; | |
/** | |
* The primary key for the model. | |
* | |
* @var string | |
*/ | |
protected $primaryKey = 'id'; | |
/** | |
* The "type" of the auto-incrementing ID. | |
* | |
* @var string | |
*/ | |
protected $keyType = 'int'; | |
/** | |
* Indicates if the model should be timestamped. | |
* | |
* @var bool | |
*/ | |
public $timestamps = false; | |
/** | |
* The storage format of the model's date columns. | |
* | |
* @var string | |
*/ | |
protected $dateFormat = 'U'; | |
/** | |
* The attributes that should be mutated to dates. | |
* | |
* @var array | |
*/ | |
protected $dates = [ | |
'seen_at', | |
]; | |
/** | |
* The attributes that should be cast to native types. | |
* | |
* @var array | |
*/ | |
protected $casts = [ | |
'is_admin' => 'boolean', | |
]; | |
/** | |
* Prepare a date for array / JSON serialization. | |
* | |
* @param \DateTimeInterface $date | |
* @return string | |
*/ | |
protected function serializeDate(DateTimeInterface $date) | |
{ | |
return $date->format('Y-m-d H:i:s'); | |
} | |
/** | |
* The connection name for the model. | |
* | |
* @var string | |
*/ | |
protected $connection = 'connection-name'; | |
/** | |
* The model's default values for attributes. | |
* | |
* @var array | |
*/ | |
protected $attributes = [ | |
'delayed' => false, | |
]; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = ['name']; | |
/** | |
* The attributes that aren't mass assignable. | |
* | |
* @var array | |
*/ | |
protected $guarded = ['price']; | |
/** | |
* The relations to eager load on every query. | |
* | |
* @var array | |
*/ | |
protected $with = []; | |
/** | |
* The relationship counts that should be eager loaded on every query. | |
* | |
* @var array | |
*/ | |
protected $withCount = []; | |
/** | |
* The number of models to return for pagination. | |
* | |
* @var int | |
*/ | |
protected $perPage = 15; | |
/** | |
* Scope a query to only include popular users. | |
* | |
* @param \Illuminate\Database\Eloquent\Builder $query | |
* @return \Illuminate\Database\Eloquent\Builder | |
*/ | |
public function scopePopular($query) | |
{ | |
return $query->where('votes', '>', 100); | |
} | |
/** | |
* Scope a query to only include users of a given type. | |
* | |
* @param \Illuminate\Database\Eloquent\Builder $query | |
* @param mixed $type | |
* @return \Illuminate\Database\Eloquent\Builder | |
*/ | |
public function scopeOfType($query, $type) | |
{ | |
return $query->where('type', $type); | |
} | |
/** | |
* The event map for the model. | |
* | |
* @var array | |
*/ | |
protected $dispatchesEvents = [ | |
'saved' => UserSaved::class, | |
'deleted' => UserDeleted::class, | |
]; | |
/** | |
* Handle the User "created" event. | |
* | |
* @param \App\User $user | |
* @return void | |
*/ | |
public function created(User $user) | |
{ | |
// | |
} | |
/** | |
* Handle the User "updated" event. | |
* | |
* @param \App\User $user | |
* @return void | |
*/ | |
public function updated(User $user) | |
{ | |
// | |
} | |
/** | |
* Handle the User "deleted" event. | |
* | |
* @param \App\User $user | |
* @return void | |
*/ | |
public function deleted(User $user) | |
{ | |
// | |
} | |
/** | |
* Create a new Eloquent Collection instance. | |
* | |
* @param array $models | |
* @return \Illuminate\Database\Eloquent\Collection | |
*/ | |
public function newCollection(array $models = []) | |
{ | |
return new CustomCollection($models); | |
} | |
/** | |
* The attributes that should be hidden for arrays. | |
* | |
* @var array | |
*/ | |
protected $hidden = ['password']; | |
/** | |
* The attributes that should be visible in arrays. | |
* | |
* @var array | |
*/ | |
protected $visible = ['first_name', 'last_name']; | |
/** | |
* The accessors to append to the model's array form. | |
* | |
* @var array | |
*/ | |
protected $appends = ['is_admin']; | |
/** | |
* The "booted" method of the model. | |
* | |
* @return void | |
*/ | |
protected static function booted() | |
{ | |
static::created(function ($user) { | |
// | |
}); | |
} | |
} | |
// form requests | |
/** | |
* Get the error messages for the defined validation rules. | |
* | |
* @return array | |
*/ | |
public function messages() | |
{ | |
return [ | |
'title.required' => 'A title is required', | |
'body.required' => 'A message is required', | |
]; | |
} | |
/** | |
* Configure the validator instance. | |
* | |
* @param \Illuminate\Validation\Validator $validator | |
* @return void | |
*/ | |
public function withValidator($validator) | |
{ | |
$validator->after(function ($validator) { | |
if ($this->somethingElseIsInvalid()) { | |
$validator->errors()->add('field', 'Something is wrong with this field!'); | |
} | |
}); | |
} | |
/** | |
* Get custom attributes for validator errors. | |
* | |
* @return array | |
*/ | |
public function attributes() | |
{ | |
return [ | |
'email' => 'email address', | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment