【问题标题】:Get avarage value from a Table with PHP and MySQL使用 PHP 和 MySQL 从表中获取平均值
【发布时间】:2018-05-06 21:24:18
【问题描述】:

我有 2 张表 tachetarificationtacheone to many 关系。在桌子上tarification 我有tache_idtariftechnicien_id。在桌子上tache 我有idlibelle_tachetarif

我想在页面show.blade.php 上显示来自tarificationtache tarif 的所有tarif 的平均值,其中tache_id = id 来自table tache

转速控制器

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tache;
use App\Metier;
use App\Technicien;
class TacheController extends Controller
{

protected function validator(array $data)
{
return Validator::make($data, [
    'Tarif' => 'required|floatval(6.3)',

]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{


$tache=tache::with(['metier'])->get();

$search = $request->get('search');

$field = $request->get('field') != '' ? $request->get('field') : 
'libelle_tache';
$sort = $request->get('sort') != '' ? $request->get('sort') : 'asc';
$tache = new tache();
if ($request)

$tache = $tache->where('libelle_tache', 'like', '%' . $search . '%')
    ->orderBy($field, $sort)
    ->paginate(5)
    ->withPath('?search=' . $search . '&libelle_tache=' . $tache . '&field=' 
. $field . '&sort=' . 
 $sort);
return view('tache.index',['tache'=>$tache]);

}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{

//$metiers = Costcenter::lists('libelle_metier', 'id');
$metiers = Metier::orderBy('libelle_metier', 'asc')->get();
return view('tache.create')->with('metiers', $metiers);
}

/**
* Store a newly created resource in storage.
*
* @param  \Illuminate\Http\Request  $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$tache = new Tache();
$tache ->libelle_tache =$request->input('libelle_tache');
$tache ->Tarif =$request->input('Tarif');
$tache ->metier_id = $request->input('metier_id');
$tache->save();
return redirect('tache');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{

$tache = tache::findOrFail($id);
$metier = $tache->metier;
return view('tache.show' , compact('tache'))->with('metier',$metier);

}

/**
* Show the form for editing the specified resource.
*
* @param  int  $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$tache=Tache::find($id);
$metiers = Metier::orderBy('libelle_metier', 'asc')->get();
return view('tache.edit',['libelle_tache'=>$tache],['Tarif'=>$tache], 
['metier_id'=>$tache])- 
>with('metiers', $metiers);
}

 public function update(Request $request, $id)
 {
 // do some request validation
 $tache=Tache::find($id);
 $tache->update($request->all());
 return redirect('tache');
 }

/**
* Remove the specified resource from storage.
*
* @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
$tache =Tache::find($id);
$tache->delete();

return redirect('tache');
}
public function getTachesByMetier($metier_id)
{
$t = Metier::find($metier_id);
return response()->json(['taches' => $t->taches]);
 }
}

show.blade.php

@extends('Layouts/app')
@extends('Layouts.master')
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10">
<h1 align="center">Detail Tache</h1>

   <div class="form-group">
            <label for="libelle_Tache">Libelle Tache</label>
            <label id="libelle_Tache" type="text" class="form-control" 
 name="tache[libelle_Tache]" >{{$tache->libelle_tache}}</label>
    </div>
    <div class="form-group">
            <label for="Tarif">Tarif moyenne </label>
            <label id="Tarif" type="text" class="form-control" 
 name="tache[Tarif]" >{{$tache->Tarif}}</label>
    </div>
    <div class="form-group">
            <label for="libelle_metier">Libelle Metier</label>
            <label id="prenom" type="text" class="form-control" 
 name="metier[libelle_metier]" >{{$tache->metier->libelle_metier}}</label>
    </div>
       <div class="form-group" align="right">

                        <form action="{{url ('tache/'.$tache->id)}}" 
  method="post">
                            {{csrf_field()}}
                            {{method_field('DELETE')}}

                            <a href="{{url('/tache')}}" class="btn btn- 
  default" class="btn btn-primary">Retour</a>
                            <a href="{{url('tache/'.$tache->id.'/edit')}}" 
  class="btn btn-default">Editer</a>
                            <button type="submit" class="btn btn- 
  danger">Supprimer</button>
                        </form>
        </div
 </div>
 @endsection

在 tarification tache 界面中,每个技术人员为其任务定价

这是我喜欢显示每个任务的平均收费的 show.blade.php

tache.php

  <?php

 namespace App;

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

  class tache extends Model
  {
  use SoftDeletes;
  protected $guarded = [];
 protected $dates = ['deleted_at'];

     public function metier()
     {
    return $this->belongsTo(Metier::class);
      }

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


    }

【问题讨论】:

  • 我修复了格式,但是,您可能希望更改问题和代码以更好地显示您想要实现的目标。现在它很混乱,很难看到你尝试了什么,什么失败了。这样我们可以更轻松地帮助您:)
  • 感谢@ThomasDarvik,我没什么尝试,我希望谁能帮助我影响这个功能,因为这是我第一个使用 laravel 的项目
  • @ThomasDarvik 现在更清楚了吗?
  • 我只是想修复格式,以便其他人可以更清楚地看到问题。我不知道如何解决您的问题。对不起。 :) 希望你能找到解决办法!

标签: javascript php ajax laravel


【解决方案1】:

您应该在 tache 模型上创建一个方法。 像

public function getAverage() {
    //here you would get all the relationships and return the avarage
    $total = 0;
    foreach ($this->tarificationtache as $tarification) { //if tarificationtache() is the name of your relationship to your other model...
        $total =+ $tarification->tarif;
    }
    //return average by dividing total by number of tarifications
    return ($total / (count($this->tarificationtache)));
}

这有点像伪代码,因为我不知道你把模型上的关系称为什么,但你明白了吗?

然后在你看来你可以调用

$tache->getAverage()

【讨论】:

  • 调用未定义的方法 Illuminate\Database\Query\Builder::getAverage() (查看: C:\xampp\htdocs\projet\resources\views\tache\show.blade.php)"
  • 你没有添加我的功能?我正在编辑代码以匹配关系的函数。
  • 别忘了更改$this-&gt;tarifications以匹配关系名称
  • 我已将方法从 cotrolelr 更改为模型我现在有这个错误“为 foreach() 提供的参数无效(视图:C:\xampp\htdocs\projet\resources\views\tache\show.刀片.php)"
  • @Ben Dubuisson,如果你能用代码解释更多,因为我是 laravel 的新手
猜你喜欢
  • 2016-07-29
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-23
  • 1970-01-01
相关资源
最近更新 更多