- Forums
- laravel
- Laravel - Understanding The Model Php Files And The Objects You Can Add
these are notes about the model methods you can use in your model files. [5284], Last Updated: Mon Jun 24, 2024
edw
Sun Jan 21, 2024
0 Comments
193 Visits
You can use various methods in your model file, here are some of my notes>
Example Model: Pizza.php from video below
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Pizza extends Model
{
use HasFactory;
protected $guarded = [];
protected $appends = [
'chef',
'last_updated',
];
protected $casts = [
'toppings' => 'array',
];
protected $hidden = [
'user',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function getChefAttribute(): string
{
return $this->user->name;
}
public function getLastUpdatedAttribute(): string
{
return $this->updated_at->diffForHumans();
}
}
This video is helpful to explaining: The video starts with "array to string" error when doing a migration seed, and they it shows the solution to using various methods in the model.
hWFP9DeB7KA