试图获取非对象的财产“雇员”(查看:/Applications/MAMP/htdocs/al-halal/resources/views/leave/allLeave.blade.php)

时间:2019-12-05 12:29:21

标签: php laravel

一直在尝试从我的vew中获取这些代码的输出,但是它给我带来了问题,请让我真心b地寻求帮助。

在我的控制器中

<?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');
    }
}

在allLeaveModel中

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class allLeave extends Model
{
    public function empolyee()
    {
        return $this->hasMany('App\Employee');
    }
}

刀片中

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

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

如果我必须做同样的事情,我会像

 <?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()
    {
           /* No need to use App\allLeave because you already have that used in 
              top of the project */
           $data=allLeave::findorFail(1);
           return view('leave.allLeave')->with('data', $data);
    }
}

在前端只需使用

{{$data->first_name}} //same column as in database table

注意:确保正在使用laravel雄辩的模型关系

答案 2 :(得分:0)

我认为您必须更改此行

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

收件人

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

答案 3 :(得分:0)

在您的控制器中应如下所示:

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

还有你的刀片:

{{$data->full_name}}