Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chagouani's avatar

Get avarage value from a Table with PHP and MySQL

I have 2 tables tache and tarificationtache with one to many relation. On the table tarification I have tache_id , tarif and technicien_id. On table tache I have id , libelle_tache and tarif

I would like on page show.blade.php to shown the average of all tarif from tarificationtache tarif where the tache_id = id from table tache .

Tachecontroller

<?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)',

]);
}

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

}

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');
}


public function show($id)
{

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

}
}

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
0 likes
10 replies
Vilfago's avatar

You can try this (but really not sure it works) :

$tache = $tache->where('libelle_tache', 'like', '%' . $search . '%')
    ->orderBy($field, $sort)
    ->with(['tarificationtache' => function ($query) {
        $query->avg('tarif');
    }])
    ->paginate(5)
    ->withPath('?search=' . $search . '
        &libelle_tache=' . $tache . '
        &field='. $field . '&sort=' .  $sort);

If it works you can probably use $tache->tarificationtache in your view, or maybe $tache->tarificationtache->tarif... In any case, you can dump($tache) to see what you get.

chagouani's avatar

@Vilfago whith " $tache->tarificationtache" he shown me all tarificationtache whith tache_id

Vilfago's avatar
Vilfago
Best Answer
Level 20

you don't have any extra attribute with the average value ?

If not, do this

$tache = $tache->where('libelle_tache', 'like', '%' . $search . '%')
    ->orderBy($field, $sort)
    ->with('tarificationtache')
    ->paginate(5)
    ->withPath('?search=' . $search . '
        &libelle_tache=' . $tache . '
        &field='. $field . '&sort=' .  $sort);

and in your view, to get the average value of tarif

{{ $tache->tarificationtache->avg('tarif') }}
1 like
chagouani's avatar

@Vilfago thanks its work just fi i like shown just 2 nulber after the ',' how can i do ?

chagouani's avatar

@Vilfago yes i want only 2dicimal

I put this line in the migration or in the index function ?

Vilfago's avatar

it's formatting, so obviously in your view when you want to show it.

and in your view, to get the average value of tarif

{{ number_format($tache->tarificationtache->avg('tarif'), 2) }}
1 like
Vilfago's avatar

Close this thread, we almost solved everything ;)

1 like

Please or to participate in this conversation.