Call to undefined method App\Models\Appointment::name()
Hi all,
I am trying to create a new appointment but I am getting the error that the method
AppointmentResource
name()
is undefined. I don't know where this is comming from.
This is my source code:
Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Appointment extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'phone',
'status',
'date',
'time',
'message',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'date' => 'date',
'time' => 'time',
'created_at' => 'datetime',
'updated_at' => 'datetime'
];
/**
* Get the services for the appointment.
*
* @return BelongsToMany
*/
public function services(): BelongsToMany
{
return $this->belongsToMany(Service::class, 'appointment_service');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Appointment extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'phone',
'status',
'date',
'time',
'message',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'date' => 'date',
'time' => 'time',
'created_at' => 'datetime',
'updated_at' => 'datetime'
];
/**
* Get the services for the appointment.
*
* @return BelongsToMany
*/
public function services(): BelongsToMany
{
return $this->belongsToMany(Service::class, 'appointment_service');
}
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Fieldset::make()
->label(label: __(key: 'Client Details'))
->schema([
Forms\Components\TextInput::make(name: 'name')
->autofocus()
->required()
->columnSpan(span: 1)
->placeholder(placeholder: __(key: 'Name')),
Forms\Components\TextInput::make(name: 'email')
->required()
->type(type: 'email')
->columnSpan(span: 1)
->placeholder(placeholder: __(key: 'Email')),
Forms\Components\TextInput::make(name: 'phone')
->required()
->type(type: 'tel')
->columnSpan(span: 2)
->placeholder(placeholder: __(key: 'Phone')),
])->columns(),
Forms\Components\Fieldset::make()
->label(label: __(key: 'Appointment Details'))
->schema([
Forms\Components\Datepicker::make(name: 'date')
->required()
->columnSpan(span: 1)
->placeholder(placeholder: __(key: 'Date')),
Forms\Components\Timepicker::make(name: 'time')
->required()
->columnSpan(span: 1)
->placeholder(placeholder: __(key: 'Time')),
Forms\Components\CheckboxList::make(name: 'services')
->required()
->relationship(name: 'name', titleAttribute: 'name')
->options(Service::all()->pluck('name', 'id')->toArray())
->columnSpan(span: 2)
])->columns(),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Fieldset::make()
->label(label: __(key: 'Client Details'))
->schema([
Forms\Components\TextInput::make(name: 'name')
->autofocus()
->required()
->columnSpan(span: 1)
->placeholder(placeholder: __(key: 'Name')),
Forms\Components\TextInput::make(name: 'email')
->required()
->type(type: 'email')
->columnSpan(span: 1)
->placeholder(placeholder: __(key: 'Email')),
Forms\Components\TextInput::make(name: 'phone')
->required()
->type(type: 'tel')
->columnSpan(span: 2)
->placeholder(placeholder: __(key: 'Phone')),
])->columns(),
Forms\Components\Fieldset::make()
->label(label: __(key: 'Appointment Details'))
->schema([
Forms\Components\Datepicker::make(name: 'date')
->required()
->columnSpan(span: 1)
->placeholder(placeholder: __(key: 'Date')),
Forms\Components\Timepicker::make(name: 'time')
->required()
->columnSpan(span: 1)
->placeholder(placeholder: __(key: 'Time')),
Forms\Components\CheckboxList::make(name: 'services')
->required()
->relationship(name: 'name', titleAttribute: 'name')
->options(Service::all()->pluck('name', 'id')->toArray())
->columnSpan(span: 2)
])->columns(),
]);
}
Solution:Jump to solution
also it should be
->relationship(name: 'services', titleAttribute: 'name')
...5 Replies
provide only one of relationship or options dont need both
not sure if this is the cause but ... try it 🙂
Forms\Components\CheckboxList::make(name: 'services')
->required()
->relationship(name: 'name', titleAttribute: 'name')
->options(Service::all()->pluck('name', 'id')->toArray())
->columnSpan(span: 2)
Forms\Components\CheckboxList::make(name: 'services')
->required()
->relationship(name: 'name', titleAttribute: 'name')
->options(Service::all()->pluck('name', 'id')->toArray())
->columnSpan(span: 2)
Removed the
titleAttribute
and I am facing the same issue, if I remove name it is throwing a error that name is requiredSolution
also it should be
->relationship(name: 'services', titleAttribute: 'name')
and remove
->options(Service::all()->pluck('name', 'id')->toArray())
Thanks that solves my issue!