Laravel将值传递给模型

时间:2016-04-04 08:50:05

标签: php laravel

现在我用eloquent尝试这个查询:

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

在我的employeeintern模型中,我得到了这个:

实习生

  /**
     * @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 mentor($withTrashed = false)
    {
        if($withTrashed == true)
        {
            return $this->belongsTo(mentorModel::class, 'MentorId')->withTrashed();
        }

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

但它崩溃了:

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

我该如何解决这个问题?

- 编辑 -

员工

<?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 个答案:

答案 0 :(得分:1)

根据laravel指南尝试以下操作。请注意,父模型必须具有hasOne/hasMany方法,子模型必须具有belongsTo方法。

实习生

/**
 * @return mixed
 */
public function intern($withTrashed = false)
{
    if($withTrashed == true)
    {
        return $this->hasOne('App\Intern', 'InternId')->withTrashed();
    }

    return $this->hasOne('App\Intern','InternId');
}  

员工

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

    return $this->belongsTo('App\Intern','InternId');
}

注意:所有其他型号都相同。

答案 1 :(得分:1)

您遇到的问题是任何Eloquent关系对象实际上都是Relation的实例。这意味着当您创建关系时,实际上会返回一个集合(Builder的实例);纠正你的错误:

  

Builder.php第2148行中的BadMethodCallException:   调用未定义的方法Illuminate \ Database \ Query \ Builder :: mentor()

简单的解决方案,无需对代码进行任何修改即可:

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

但是,您可以像以下一样使用重载:

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

虽然这将 NOT 包含您的withTrashed。但是,您可以将您的关系调整为:

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

    if($withTrashed == true)
    {
        return $relation->withTrashed()->first();
    }

    return $relation->first();
}

但我不建议这样做,因为如果你尝试使用像WhereHas这样的东西,你会得到错误。也就是说,另一种方法是按照以下方式做点什么:

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

然后像以下一样遭受破坏:

'MentorId' => $employee->intern()->withTrashed()->first()->mentor()->withTrashed()->first()->MentorId;
相关问题