Level 75
Use pathinfo to get an extension.
public function getExtensionAttribute()
{
return pathinfo($this->file_path, PATHINFO_EXTENSION);
}
By the way it's accesor and not mutator.
2 likes
Hello,
I have a file model like this:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class File extends Model
{
use HasFactory;
protected $fillable = [
'name',
'file_path'
];
protected $appends = ['extension'];
public function getExtensionAttribute()
{
return \File::extension($this->filename);
}
}
I tried to get the extensions. But the value is always empty, what I am doing wrong? This is how I store the file, maybe this is important:
$filePath = $request->file('file')->storeAs('assets', $fileName, 'local');
Please or to participate in this conversation.