ErrorException试图获取Laravel中非对象的属性“名称”

时间:2019-12-14 21:12:44

标签: relational-database laravel-6.2

我的日历控制器出现此错误,她只为克林特名字工作,尽管下面的方法与我的控制器使用的方法相同,这是我的控制器和3个模型,在与DB一起加入克林特任命后出现了这个问题

namespace App\ Http\ Controllers\ Admin;

use App\ Appointment;
use App\ Http\ Controllers\ Controller;

class SystemCalendarController extends Controller

{

    public
    function index() {
        $events = [];

        $appointments = Appointment::with(['client', 'employee']) - > get();

        foreach($appointments as $appointment) {
            if (!$appointment - > start_time) {
                continue;
            }

            $events[] = [
                'title' => $appointment - > Clint - > name.
                ' ('.$appointment - > employee - > name.
                ')',
                'start' => $appointment - > start_time,
                'url' => route('admin.appointments.edit', $appointment - > id),
            ];
        }

        return view('admin.calendar.calendar', compact('events'));
    }
}

具有克林特与员工关系的约会模型

 <?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Yajra\Auditable\AuditableWithDeletesTrait;
class Appointment extends Model
{
    use SoftDeletes, AuditableWithDeletesTrait;

    public $table = 'appointments';

    protected $dates = [
        'start_time',
        'created_at',
        'updated_at',
        'deleted_at',
        'finish_time',
    ];

    protected $fillable = [
        'price',
        'comments',
        'client_id',
        'start_time',
        'weigh',
        'circum',
        'checked',
        'created_at',
        'updated_at',
        'deleted_at',
        'employee_id',
        'finish_time',
    ];

    public function client()
    {
        return $this->belongsTo(Client::class, 'client_id');
    }

    public function employee()
    {
        return $this->belongsTo(Employee::class, 'employee_id');
    }

    public function getStartTimeAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('panel.date_format') . ' ' . config('panel.time_format')) : null;
    }

    public function setStartTimeAttribute($value)
    {
        $this->attributes['start_time'] = $value ? Carbon::createFromFormat(config('panel.date_format') . ' ' . config('panel.time_format'), $value)->format('Y-m-d H:i:s') : null;
    }

    public function getFinishTimeAttribute($value)
    {
        return $value ? Carbon::createFromFormat('Y-m-d H:i:s', $value)->format(config('panel.date_format') . ' ' . config('panel.time_format')) : null;
    }

    public function setFinishTimeAttribute($value)
    {
        $this->attributes['finish_time'] = $value ? Carbon::createFromFormat(config('panel.date_format') . ' ' . config('panel.time_format'), $value)->format('Y-m-d H:i:s') : null;
    }

    public function services()
    {
        return $this->belongsToMany(Service::class);
    }
}

与约会模型相关的Clint模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Yajra\Auditable\AuditableWithDeletesTrait;



class Client extends Model
{
    use SoftDeletes, AuditableWithDeletesTrait;

    public $table = 'clients';

    protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected $fillable = [
        'name',
        'phone',
        'email',
        'sex',
        'occupation',
        'residente',
        'habits',
        'co',
        'history',
        'ex',
        'diagnosis',
        'notes',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    public function appointments()
    {
        return $this->hasMany(Appointment::class, 'client_id', 'id');
    }
}

employee model 

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\Models\Media;

class Employee extends Model implements HasMedia
{
    use SoftDeletes, HasMediaTrait;

    public $table = 'employees';

    protected $appends = [
        'photo',
    ];

    protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected $fillable = [
        'name',
        'email',
        'phone',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    public function registerMediaConversions(Media $media = null)
    {
        $this->addMediaConversion('thumb')->width(50)->height(50);
    }

    public function appointments()
    {
        return $this->hasMany(Appointment::class, 'employee_id', 'id');
    }

    public function getPhotoAttribute()
    {
        $file = $this->getMedia('photo')->last();

        if ($file) {
            $file->url       = $file->getUrl();
            $file->thumbnail = $file->getUrl('thumb');
        }

        return $file;
    }

    public function services()
    {
        return $this->belongsToMany(Service::class);
    }
}

0 个答案:

没有答案