PHP Laravel 5.1 - 使用“with()”不能正常工作的Eloquent选择

时间:2015-09-15 16:24:35

标签: php laravel laravel-5.1

我正在尝试使用数据库中的特定表进行选择。

这里是“Funcionario”模型:

class Funcionario extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'funcionarios';


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['nome', 'matricula', 'pis_pasep', 'data_admissao', 'data_demissao', 'data_nascimento', 'apelido', 'sexo_id', 'setor_id', 'cargo_id', 'turno_id'];

     /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function cargo()
    {
        return $this->belongsTo('App\Cargo');
    }

}

这是“货物”模型:

class Cargo extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'cargos';


    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['nome'];

    /**
     * Belongs to 'funcionarios' relationship
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function funcionario()
    {
        return $this->hasMany('App\Funcionario');
    }
}

这就是我现在正在尝试的事情:

public function showMyEmployees(){

        $data = Funcionario::where('supervisor_id', $user->id)
            ->with([
                'cargo' => function($query){
                    $query->select('nome'); // $query->pluck('nome'); // $query->get(['nome']);
                }
            ])
            ->orderBy('nome')
            ->get();

        return response()->json($data, 200);
    }

我得到了什么:

{
    "id": 1648,
    "nome": "ADOLFO ARAUJO DOS SANTOS JUNIOR",
    "matricula": 14311,
    "pis_pasep": 0,
    "data_admissao": "1970-01-01",
    "data_demissao": "1970-01-01",
    "data_nascimento": null,
    "apelido": null,
    "supervisor_id": 1105,
    "coordenador_id": null,
    "gerente_id": null,
    "diretor_id": null,
    "sexo_id": null,
    "setor_id": 36,
    "cargo_id": 56,
    "turno_id": null,
    "created_at": "2015-09-15 14:49:32",
    "updated_at": "2015-09-15 15:58:36",
    "cargo": null
  }

“货物”的值应该为空。

如果我没有在select eloquent中使用闭包,它将返回“货物”中的“nome”。

像这样:

{
    "id": 1648,
    "nome": "ADOLFO ARAUJO DOS SANTOS JUNIOR",
    "matricula": 14311,
    "pis_pasep": 0,
    "data_admissao": "1970-01-01",
    "data_demissao": "1970-01-01",
    "data_nascimento": null,
    "apelido": null,
    "supervisor_id": 1105,
    "coordenador_id": null,
    "gerente_id": null,
    "diretor_id": null,
    "sexo_id": null,
    "setor_id": 36,
    "cargo_id": 56,
    "turno_id": null,
    "created_at": "2015-09-15 14:49:32",
    "updated_at": "2015-09-15 15:58:36",
    "cargo": {
      "id": 56,
      "nome": "AUXILIAR DE PRODUCAO",
      "created_at": "2015-09-15 14:47:18",
      "updated_at": "2015-09-15 14:47:18"
    }

..这就是我想要的:

{
    "id": 1648,
    "nome": "ADOLFO ARAUJO DOS SANTOS JUNIOR",
    "matricula": 14311,
    "pis_pasep": 0,
    "data_admissao": "1970-01-01",
    "data_demissao": "1970-01-01",
    "data_nascimento": null,
    "apelido": null,
    "supervisor_id": 1105,
    "coordenador_id": null,
    "gerente_id": null,
    "diretor_id": null,
    "sexo_id": null,
    "setor_id": 36,
    "cargo_id": 56,
    "turno_id": null,
    "created_at": "2015-09-15 14:49:32",
    "updated_at": "2015-09-15 15:58:36",
    "cargo": "AUXILIAR DE PRODUCAO"
}

谢谢!

2 个答案:

答案 0 :(得分:2)

您需要在select()中添加“id”,因为雄辩需要它进行比较。我认为你最好不要在这种情况下使用eloquent,因为你只想为货物钥匙返回一个字符串。

DB::table('funcionarios')
    ->join('cargos', 'cargos.id', '=', 'funcionarios.cargo_id')
    ->get(['funcionarios.*', 'cargos.nome AS cargo']);

答案 1 :(得分:0)

由于我想避免DB类外观,我决定使用 foreach循环在“货物”中添加新值。

就像:

public function showMyEmployees(){

    $data = Funcionario::where('supervisor_id', $user->id)
        ->orderBy('nome')
        ->get();

    $funcionarios = array();
    foreach($data as $funcionario){
        $funcionario->cargo = $funcionario->cargo()->pluck('nome');
        $funcionario->setor = $funcionario->setor()->pluck('nome');

        array_push($funcionarios, $funcionario);
    }

    return response()->json($funcionarios, 200);
}