该集合实例上不存在属性[libelle_metier]

时间:2018-07-02 09:20:42

标签: json laravel

我有一个显示选择ID'technicien'的功能,他从表用户那里显示了他的名字,并且在那里'metier' techniciens_tables

Schema::create('techniciens', function (Blueprint $table) {
    $table->increments('id');
    $table->boolean('actif')->default(1);
    $table->float('moyenne_avis')->nullable();
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
    $table->datetime('deleted_at')->nullable();
    $table->timestamps();

});

metier_tables

Schema::create('metiers', function (Blueprint $table) {
        $table->increments('id');
        $table->string('libelle_metier');
        $table->datetime('deleted_at')->nullable();
        $table->timestamps();
    });

users_table

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('email');
        $table->string('password');
        $table->string('nom');
        $table->string('prenom');
        $table->string('tel');
        $table->string('mobil');
        $table->boolean('role')->default(0);
        $table->datetime('deleted_at')->nullable();
        $table->rememberToken();
        $table->timestamps();
    });

技术模型

public function  user()
{
    return $this->belongsTo(User::class);
}

public function metier()
{
    return $this->belongsToMany('App\metier','technicien_metier', 
    'technicien_id','metier_id');

}

分层模型

 public function techniciens()
{
    return $this->belongsToMany('App\technicien','technicien_metier', 
    'metier_id','technicien_id');

}

我的技术人员控制器中具有此功能

 public function GetTables($id)
{
    $technicien = Technicien::with('user','metier')->find($id);
    $metier = $technicien->metier;
    return [
            'id' => $technicien->id,
            'actif' => $technicien->actif,
            'nom' => $technicien->user->nom,
            'prenom' => $technicien->user->prenom,
            'metier' => $metier->libelle_metier,
    ];
}

3 个答案:

答案 0 :(得分:3)

$metier是一个集合实例,您可以使用foreach对其进行循环,或者使用Collection方法firstpluck这样来获取值

public function GetTables($id)
{
    $technicien = Technicien::with('user','metier')->find($id);
    $metier = $technicien->metier;
    return [
            'id' => $technicien->id,
            'actif' => $technicien->actif,
            'nom' => $technicien->user->nom,
            'prenom' => $technicien->user->prenom,
            'metier' => $metier->first()->libelle_metier,
    ];
}

OR

public function GetTables($id)
{
    $technicien = Technicien::with('user','metier')->find($id);
    $metier = $technicien->metier;
    return [
            'id' => $technicien->id,
            'actif' => $technicien->actif,
            'nom' => $technicien->user->nom,
            'prenom' => $technicien->user->prenom,
            'metier' => $metier->pluck('libelle_metier')->all(), //it will give you an array
    ];
}

收集详细信息https://laravel.com/docs/5.6/collections#method-pluck

答案 1 :(得分:1)

很明显,由于没有关系,您的关系返回null $metier = $technicien->metier;,所以您可以这样做:

 public function GetTables($id)
{
    $technicien = Technicien::with('user','metier')->find($id);
    $metier = $technicien->metier;
    return [
            'id' => $technicien->id,
            'actif' => $technicien->actif,
            'nom' => $technicien->user->nom,
            'prenom' => $technicien->user->prenom,
            'metier' => isset($metier->libelle_metier) ? $metier->get()->pluck('libelle_metier') : null,
    ];
}

或调整您的查询以仅获取具有类似以下条件的Technicien:

$technicien = Technicien::whereHas('user','metier')->find($id);

答案 2 :(得分:1)

高层关系是许多技术模型的基础。首先更正您的亲戚姓名

public function metiers()
{
    return $this->belongsToMany('App\metier','technicien_metier', 
    'technicien_id','metier_id');
}

然后

public function GetTables($id)
{
    $technicien = Technicien::with('user','metiers')->find($id);
    $metiers = $technicien->metiers;
    return [
        'id' => $technicien->id,
        'actif' => $technicien->actif,
        'nom' => $technicien->user->nom,
        'prenom' => $technicien->user->prenom,
        'metier' => $metiers->pluck('libelle_metier')->all(),
        // or 'metier' => !empty($metiers->first()) ? $metiers->first()->libelle_metier : 'no metier';
    ];
}