I want to add the column registered_by of my migration books with the name of the user registered in the system (it will not be possible to update this field after the record is registered, but I find the following error when registering the book:
BookModel.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;
class BookModel extends Model
{
use HasFactory;
public $timestamps = false;
protected $table = 'books';
protected $primaryKey = 'id';
protected $fillable = ['title','author','subject','year_publication'];
public function userName()
{
$this->attributes['registered_by'] = Auth::user()->name;
}
}