找不到列:1054未知列

时间:2018-06-26 10:16:10

标签: php json laravel

我有一个函数可以显示“ tarificationtaches”,其中与干预有关的表avis干预中的moyenne_avis平均平均值,我有此错误..................... ........................

"SQLSTATE[42S22]: Column not found: 1054 Unknown column      
    'interventions.tarificationtache_id' in 'where clause' (SQL: select * 
    from `interventions` 
where `interventions`.`tarificationtache_id` in (1, 2, 3, 4))

tarificationtaches_tables

Schema::create('tarificationtaches', function (Blueprint $table) {
        $table->increments('id');
        $table->float('tarif', 8,2);
        $table->integer('tache_id')->unsigned();
        $table->foreign('tache_id')->references('id')->on('taches');
        $table->integer('technicien_id')->unsigned();
        $table->foreign('technicien_id')->references('id')- 
        >on('techniciens');
        $table->datetime('deleted_at')->nullable();
        $table->timestamps();
    });

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();

    });

avis_interventions_tables

Schema::create('avis_interventions', function (Blueprint $table) {
        $table->increments('id');
        $table->string('qualité');
        $table->integer('nbr_heure');
        $table->string('service');
        $table->float('note', 1,1);
        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('clients');
        $table->integer('intervention_id')->unsigned();
        $table->foreign('intervention_id')->references('id')- 
        >on('interventions');
        $table->timestamps();
    });

interventions_tables

 Schema::create('interventions', function (Blueprint $table) {
        $table->increments('id');
        $table->date('date_intervention')->nullable();
        $table->string('description');
        $table->dateTime('duree_prevu');
        $table->boolean('statut');
        $table->integer('technicien_id')->unsigned();
        $table->foreign('technicien_id')->references('id')- 
        >on('techniciens');
        $table->integer('tarification_id')->unsigned();
        $table->foreign('tarification_id')->references('id')- 
        >on('tarificationtaches');
        $table->integer('client_id')->unsigned();
        $table->foreign('client_id')->references('id')->on('Clients');


        $table->timestamps();
    });

这是我的功能

 public function getTar(){
     $tarifications = tarificationtache::with('technicien','intervention')- 
    >get();
        $notes = $tarifications->intervention->avisinterventions- 
   >pluck('note');
        $moyenne = $tarifications->intervention()->avisinterventions()- 
   >avg('note');
        return $tarifications->map(function ($tarification) {
            return [
                'nom' => $tarification->technicien->user->nom,
                'moyenne_avis' => $tarification->intervention- 
     >avisinterventions->avg('note'),
                'tache' => $tarification->tache->libelle_tache,
                'tarif' => $tarification->tarif,
            ];
        });    



}

tarificationtache模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class tarificationtache extends Model
{
use SoftDeletes;
protected $guarded = [];
 protected $dates = ['deleted_at'];
public function tache()
{
    return $this->belongsTo(Tache::class);
}


public function technicien()
{
    return $this->belongsTo(Technicien::class);

}
public function intervention() {
    return $this->hasMany(intervention::class);
}


}

1 个答案:

答案 0 :(得分:1)

只需将外键tarification_id添加到关系方法中即可。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class tarificationtache extends Model
{
   use SoftDeletes;
   protected $guarded = [];
   protected $dates = ['deleted_at'];
   public function tache()
   { 
       return $this->belongsTo(Tache::class);
   }    

   public function technicien()
   {
       return $this->belongsTo(Technicien::class);
   }

    public function intervention() 
    {
        return $this->hasMany(intervention::class, 'tarification_id');
    }
}