on your client's model make sure you have this :
public function user()
{
return $this->belongsTo('App\User','user_id');
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
i have 3 tables ('avis_intervention' = id , client_id , intervention_id, note ) ('client' = id , user_id ) ('user' =nom , prénom .....)
in my interface index.blade.php to affiche the list of avis_intervention when i shown client_id it work but when i shown the name of client whith this code i have a this error
"SQLSTATE[42S22]: Column not found: 1054 Unknown column
'clients.avis_intervention_id' in 'where clause' (SQL: select * from `clients` where
`clients`.`avis_intervention_id` in (1) and `clients`.`deleted_at` is null) ◀"
index.blade.php
@extends('Layouts.app')
@extends('Layouts.master')
@section('css')
<style>
a, a:hover {
color: white;
}
</style>
@endsection
@section('content')
<div class="container">
<div class="col-md-10">
<div class="float-right">
</div>
<h1> Liste Avis Intervention</h1>
<hr/>
<div class="row">
<div class="col-sm-5 form-group">
</div>
<div class="col-sm-7 form-group">
<div class="pull-right">
<a href="{{url('avis/create')}}" class="btn btn-primary">Nouvelle Avis</a>
</div>
</div>
</div>
<table class="table table-bordered bg-light">
<thead class="bg-dark" >
<tr >
<th class="light-table-filter" data-table="order-table" placeholder="Filter"
style=" color: #337ab7">Client</th>
<th class="light-table-filter" data-table="order-table" placeholder="Filter"
style=" color: #337ab7">qualité</th>
<th class="light-table-filter" data-table="order-table" placeholder="Filter"
style=" color: #337ab7">nbr_heure</th>
<th class="light-table-filter" data-table="order-table" placeholder="Filter"
style=" color: #337ab7">service</th>
<th class="light-table-filter" data-table="order-table" placeholder="Filter"
style=" color: #337ab7">note</th>
<th style="color: #337ab7" width="220px" style="vertical-align:
middle">action</th>
</tr>
</thead>
<tbody>
@foreach($avis as $avis)
<tr>
<td>{{$avis->client->user->nom}}</td>
<td>{{$avis->qualité}}</td>
<td>{{$avis->nbr_heure}}</td>
<td>{{$avis->service}}</td>
<td>{{$avis->note}}</td>
<td >
<form action="{{url ('avis/'.$avis->id)}}"
method="post" >
<a class="btn btn-default btn-sm" title="Edit"
href="{{url('avis/'.$avis->id.'/show')}}">
Details</a>
<a class="btn btn-primary btn-sm" title="Edit"
href="{{url('avis/'.$avis->id.'/edit')}}">
Editer</a>
<input type="hidden" name="_method" value="delete"/>
{{csrf_field()}}
<a class="btn btn-danger btn-sm" title="Delete"
href="javascript:if(confirm('Are you sure want to delete?')) $('#frm_{{$avis-
>id}}').submit()">
Supprimer
</a>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
<!-- /.table-responsive -->
</div>
<!-- /.panel-body -->
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
@endsection
avis_intervention_controller
<?php
namespace App\Http\Controllers;
use App\AvisIntervention;
use Illuminate\Http\Request;
use App\Intervention;
use App\Client;
class Avis_InterventionController extends Controller
{
public function index()
{
$Listavis=AvisIntervention::with(['client'])->get();
return view('avis.index',['avis'=>$Listavis]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$intervention = intervention::orderBy('id','desc')->get();
$client = client::orderBy('id', 'desc')->get();
return view('avis.create')->with('intervention', $intervention)->with('client', $client);
}
/**
*
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$avis = new AvisIntervention();
$avis ->qualité =$request->input('qualité');
$avis ->nbr_heure =$request->input('nbr_heure');
$avis ->service =$request->input('service');
$avis ->note =$request->input('note');
$avis ->client_id =$request->input('client_id');
$avis ->intervention_id = $request->input('intervention_id');
$avis->save();
return redirect('avis');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$avis=AvisIntervention::find($id);
return view('avis.show',['avis'=>$avis]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$avis=AvisIntervention::find($id);
return view('avis.edit',['avis'=>$avis]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$avis=AvisIntervention::find($id);
$client=client_id::find($id);
$intervention=intervention_id::find($id);
$avis ->qualité =$request->input('qualité');
$avis ->nbr_heure =$request->input('nbr_heure');
$avis ->service =$request->input('service');
$avis ->client_id =$request->input('client_id');
$avis ->intervention_id =$request->input('intervention_id');
$avis->save();
return redirect('intervention');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$avis =AvisIntervention::find($id);
$avis->delete();
return redirect('avis');
}
}
You can't have a hasOne relationship on both sides! One of them (the model whose table has the foreign key) will be a belongsTo relationship
Please or to participate in this conversation.