编辑:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\User;
class technicien extends Model
{
protected $table = "techniciens";
public function user()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
}
TarificationTache模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\technicien;
use App\tache;
use App\interventions;
class tarificationtache extends Model
{
protected $table = "tarificationtaches";
public function technicien()
{
return $this->belongsTo('App\technicien', 'technicien_id', 'id');
}
public function tache()
{
return $this->belongsTo('App\tache', 'tache_id', 'id');
}
public function interventions()
{
return $this->hasMany('App\interventions', 'technicien_id', 'id');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tache extends Model
{
protected $table = "taches";
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\avis_interventions;
class interventions extends Model
{
protected $table = "interventions";
public function avis_interventions()
{
return $this->hasMany('App\avis_interventions', 'intervention_id', 'id');
}
}
Avis_干预模式:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class avis_interventions extends Model
{
protected $table = "avis_interventions";
}
$tarifications = tarificationtache::with('technicien')->get();
$results = $tarifications->map(function ($tarification) {
return [
'nom' => $tarification->technicien->user->name,
'moyenne_avis' => $tarification->technicien->moyenne_avis,
'tache' => $tarification->tache->libelle_tache,
'tarif' => $tarification->tarif,
'avg_avis_interventions' => $tarification -> interventions -> count()
];
});
print_r($results -> toJson());
exit;
返回以下输出给我:
[{
"nom": "Ravi-carpenter",
"moyenne_avis": 2,
"tache": "Task #1",
"tarif": 5.22,
"avg_avis_interventions": 2
}, {
"nom": "Ravi-carpenter",
"moyenne_avis": 3.5,
"tache": "Task #2",
"tarif": 6.52,
"avg_avis_interventions": 3
}]