Php 试图获取Laravel 5.4中非对象的属性时返回的雄辩关系错误

Php 试图获取Laravel 5.4中非对象的属性时返回的雄辩关系错误,php,laravel-5.4,Php,Laravel 5.4,我也遇到过类似的问题,但一直看不出我做错了什么 所以我在员工和任务之间建立了这样的关系 任务模型 class Task extends Model { public function employee() { return $this->belongsTo(Employee::class, 'employee_id'); } } Empoyee模型 public function tasks() { return $this->has

我也遇到过类似的问题,但一直看不出我做错了什么

所以我在员工和任务之间建立了这样的关系

任务模型

class Task extends Model
{
    public function employee()
    {
        return $this->belongsTo(Employee::class, 'employee_id');
    }
}
Empoyee模型

public function tasks()
{
    return $this->hasMany(Task::class);
}
“我的任务”表结构使用employee_id作为外键

public function up()
{
    Schema::create('tasks', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('employee_id')->unsigned();
$table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
        $table->string('title');
        $table->string('description');
        $table->string('priority');
        $table->date('begin');
        $table->date('end');
        $table->timestamps();
    });
}
在我看来,我试图返回任务列表以及员工的第一个任务列表,但我得到了错误“尝试获取非对象的属性”

我能看到这个

`#original: array:9 [▼
    "id" => 1
    "employee_id" => 2
    "title" => "Roaster"
    "description" => "Submission of work roaster has commenced and you are expected to submit before the due date"
    "priority" => "high"
    "begin" => "2017-06-26"
    "end" => "2017-06-30"
    "created_at" => "2017-06-26 22:32:39"
    "updated_at" => "2017-06-26 22:32:39"
  ]`

我哪里弄错了?

可能是您返回的
任务之一没有您试图显示的所有字段,
按如下方式更改模板:

@foreach ($allTask as $task)
    <tr>
        <td>{{ isset($task->priority) ? $task->priority : 'no priority !'  }}</td>
        <td>{{  isset($task->title) ? $task->title : 'no title!'  }}</td>
        <td>{{  isset($task->employee->firstname) ?  ucfirst($task->employee->firstname) : 'no first name!' }} {{" "}} {{ 
 isset($task->employee->lastname) ? ucfirst($task->employee->lastname) : 'no last name!' }}</td>
        <td>{{  isset($task->end) ? $task->end : 'no end!'  }}</td>
    </tr>
@endforeach
2-


我只为测试目的添加了一个任务,它包含所有字段。这样做,您的名字和姓氏字段将始终变为“无名字/姓氏”@Mena好的,我明白了!您是否为此任务创建了任何
empolyee
?我认为问题在于您没有员工或有多个员工负责此
任务
,我将更新答案。我已添加了更多员工并成功为他们创建了任务,但这并没有解决问题。@Mena当您使用此选项时:
任务::with('employee')->limit(5)->get()
,该关系将作为数组重新调整,您应该像这样使用它:
$task->employee[0]->firstname
$task->employee[1]->firstname
…我的代码正在运行。唯一不同的是,我删除了我创建的第一个任务,然后又添加了两个任务,并将代码保持在原始状态,效果很好。我觉得很奇怪
`#original: array:9 [▼
    "id" => 1
    "employee_id" => 2
    "title" => "Roaster"
    "description" => "Submission of work roaster has commenced and you are expected to submit before the due date"
    "priority" => "high"
    "begin" => "2017-06-26"
    "end" => "2017-06-30"
    "created_at" => "2017-06-26 22:32:39"
    "updated_at" => "2017-06-26 22:32:39"
  ]`
@foreach ($allTask as $task)
    <tr>
        <td>{{ isset($task->priority) ? $task->priority : 'no priority !'  }}</td>
        <td>{{  isset($task->title) ? $task->title : 'no title!'  }}</td>
        <td>{{  isset($task->employee->firstname) ?  ucfirst($task->employee->firstname) : 'no first name!' }} {{" "}} {{ 
 isset($task->employee->lastname) ? ucfirst($task->employee->lastname) : 'no last name!' }}</td>
        <td>{{  isset($task->end) ? $task->end : 'no end!'  }}</td>
    </tr>
@endforeach
$allTask = Task::with('employee')->limit(5)->get();

isset($task->employee[0]) ?  ucfirst($task->employee[0]->firstname) : 'empty'
$allTask = Task::with(['employee' => function($q){
   $q->first();
}])->limit(5)->get();

isset($task->employee) ?  ucfirst($task->employee->firstname) : 'empty'