chagouani's avatar

json

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,
    ];
  });
  }
0 likes
5 replies
tykus's avatar
tykus
Best Answer
Level 104

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's avatar
$techniciens = Technicien::with('user')->find($id); // $id
1 like
chagouani's avatar

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

tykus's avatar

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.

1 like

Please or to participate in this conversation.