douquanhui5735 2016-04-04 08:50
浏览 34
已采纳

too long

Right now I try this query with eloquent:

'MentorId'  => $employee->intern(true)->mentor(true)->MentorId,

And in my employee and intern model I've got this:

Intern

  /**
     * @return mixed
     */
    public function intern($withTrashed = false)
    {
        if($withTrashed == true)
        {
            return $this->belongsTo(internModel::class, 'InternId')->withTrashed();
        }

        return $this->belongsTo(internModel::class,'InternId');
    }

Mentor

 /**
     * @return mixed
     */
    public function mentor($withTrashed = false)
    {
        if($withTrashed == true)
        {
            return $this->belongsTo(mentorModel::class, 'MentorId')->withTrashed();
        }

        return $this->belongsTo(mentorModel::class,'MentorId');
    }

But it crashes:

BadMethodCallException in Builder.php line 2148:
Call to undefined method Illuminate\Database\Query\Builder::mentor()

How could I fix this?

--EDIT--

Employee

<?php

namespace App\src\employee;

use Illuminate\Foundation\Auth\User as Authenticatable;
use App\src\department\Department as departmentModel;
use App\src\employee\Employee as employeeModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\src\intern\Intern as internModel;
use App\src\mentor\Mentor as mentorModel;
use App\src\employee\Role as roleModel;

class Employee extends Authenticatable
{
    use SoftDeletes;
    use EmployeeServiceTrait;

    /**
     * table name
     */
    protected $table = 'employee';

    /**
     * Mass assignment fields
     */
    protected $fillable = ['RoleId', 'DepartmentId', 'InternId', 'FirstName', 'LastName', 'Bio','api_token', 'email', 'LinkedIn', 'password', 'Address', 'Zip', 'City', 'ProfilePicture', 'BirthDate', 'StartDate', 'EndDate', 'Suspended','LinkedIn'];

    /**
     * Primarykey
     */
    protected $primaryKey = 'EmployeeId';

    /**
     * Deleted_at
     */
    protected $dates = ['deleted_at'];

    /**
     * @return mixed
     */
    public function role()
    {
        return $this->belongsTo(roleModel::class,'RoleId');
    }

    /**
     * @return mixed
     */
    public function intern($withTrashed = false)
    {
        if($withTrashed == true)
        {
            return $this->belongsTo(internModel::class, 'InternId')->withTrashed();
        }

        return $this->belongsTo(internModel::class,'InternId');
    }

    /**
     * @return mixed
     */
    public function department()
    {
        return $this->belongsTo(departmentModel::class,'DepartmentId');
    }

    /**
     * @return mixed
     */
    public function mentor()
    {
        return $this->belongsTo(mentorModel::class,'MentorId');
    }

    /**
     * @return mixed
     */
    public function employees()
    {
        return $this->hasManyThrough(employeeModel::class,departmentModel::class,'CompanyId','DepartmentId');
    }

    /**
     * @param $role
     * @return bool
     */
    public function hasRole($role)
    {
        if(strtolower($this->role->RoleName) == strtolower($role))
        {
            return true;
        }
        return false;
    }
}
  • 写回答

2条回答 默认 最新

  • duanjiao6711 2016-04-05 08:21
    关注

    The problem you have is that any Eloquent relationship object is actually an instance of Relation. This means when you create relationships you actually return a collection (instance of Builder); Hense your error:

    BadMethodCallException in Builder.php line 2148: Call to undefined method Illuminate\Database\Query\Builder::mentor()

    The simple solution, without any modification to your code would be something like:

    'MentorId'  => $employee->intern(true)->first()->mentor(true)->first()->MentorId;
    

    However, you could use overloading like the following:

    'MentorId'  => $employee->intern->mentor->MentorId;
    

    Although this will NOT include your withTrashed. You can however tweak your relationship to something like:

    public function intern($withTrashed = false)
    {
        $relation = $this->belongsTo(internModel::class, 'InternId');
    
        if($withTrashed == true)
        {
            return $relation->withTrashed()->first();
        }
    
        return $relation->first();
    }
    

    But I wouldn't advise this because later on if you try using things like WhereHas you will get errors. That said, another way would be to do something along the following lines:

    public function intern()
    {
        return $this->belongsTo(internModel::class, 'InternId');
    }
    

    Then get trashed like:

    'MentorId' => $employee->intern()->withTrashed()->first()->mentor()->withTrashed()->first()->MentorId;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器