试图获取非对象的属性“员工”(查看:/Applications/MAMP/htdocs

一直试图从我的 vew 中获取这些代码的输出,但它给我带来了问题,请我真的很乐意寻求帮助。


在我的控制器中


<?php

namespace App\Http\Controllers;


use Illuminate\Http\Request;

use Illuminate\Support\Facades\Input;

use App\leaveType;

use App\allLeave;

use App\leaveDepartment;


class LeavesController extends Controller

{

    public function getAllLeave()

    {

        $data = App\allLeave::find(1)->full_name;

        return view('leave/allLeave',["data"=>$data]);

    }

}

在我的员工模型中


<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


/**

 * Class Personnel

 * @package App

 */

class Employee extends Audit

{

    public function leave()

    {

        return $this->belongsTo('App\allLeave');

    }

}

在所有离开模型


<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class allLeave extends Model

{

    public function empolyee()

    {

        return $this->hasMany('App\Employee');

    }

}

在刀片


{{$data->employee->full_name}}


FFIVE
浏览 104回答 3
3回答

潇潇雨雨

您已经为控制器中的数据分配了全名。你只需要{{ $data }}在刀片

千万里不及你

如果我必须做同样的事情,我会这样做&nbsp;<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use Illuminate\Support\Facades\Input;use App\leaveType;use App\allLeave;use App\leaveDepartment;class LeavesController extends Controller{&nbsp; &nbsp; public function getAllLeave()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* No need to use App\allLeave because you already have that used in&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; top of the project */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$data=allLeave::findorFail(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return view('leave.allLeave')->with('data', $data);&nbsp; &nbsp; }}在前端只使用{{$data->first_name}} //same column as in database table注意:确保使用 laravel eloquent 模型关系

DIEA

在你的控制器中应该是这样的:$data&nbsp;=&nbsp;App\allLeave::find(1)->empolyee();还有你的刀片:{{$data->full_name}}
打开App,查看更多内容
随时随地看视频慕课网APP