json

Published 3 months ago by chagouani

I have 2 table technicien(id, user_id,actif) and user (nom , prenom , adresse )

I would like to perform a web service function to display the technician list with their name and firstname of the user table

i have this function but it displays all techiciens i like to shown information of selected id

public function GetTables() {
    $techniciens = Technicien::with('user')->get();

    return $techniciens->map(function ($technicien) {
    return [
        'id' => $technicien->id,
        'actif' => $technicien->actif,
        'moyenne_avis' => $technicien->moyenne_avis,
        'nom' => $technicien->user->nom,
        'prenom' => $technicien->user->prenom,
        'adresse' => $technicien->user->adresse,
    ];
  });
  }
Best Answer (As Selected By chagouani)
tykus

You will need to pass in the id for the Technicien you want (usually as a URI segment):

public function GetTables($id)
{
    $technicien = Technicien::with('user')->findOrFail($id);
    
    return [
            'id' => $technicien->id,
            'actif' => $technicien->actif,
            'moyenne_avis' => $technicien->moyenne_avis,
            'nom' => $technicien->user->nom,
            'prenom' => $technicien->user->prenom,
            'adresse' => $technicien->user->adresse,
    ];
}
tykus
tykus
3 months ago (657,630 XP)

You will need to pass in the id for the Technicien you want (usually as a URI segment):

public function GetTables($id)
{
    $technicien = Technicien::with('user')->findOrFail($id);
    
    return [
            'id' => $technicien->id,
            'actif' => $technicien->actif,
            'moyenne_avis' => $technicien->moyenne_avis,
            'nom' => $technicien->user->nom,
            'prenom' => $technicien->user->prenom,
            'adresse' => $technicien->user->adresse,
    ];
}
Sergiu17
$techniciens = Technicien::with('user')->find($id); // $id
chagouani

Missing argument 1 for App\Http\Controllers\TechnicienController::GetTables()"

tykus
tykus
3 months ago (657,630 XP)

You must give the id for the Technicien you want, e.g. /techniciens/123

Your route definition would need to change as follows:

Route::get('techniciens/{id}', 'TechnicienController::GetTables');

The route wildcard {id} becomes the $id argument to the GetTables($id) method.

chagouani

Please sign in or create an account to participate in this conversation.