Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

338
Views
Cómo mostrar el nombre de usuario de una tabla de relaciones en laravel

Tengo dos mesas de usuarios y torneos. He definido las relaciones en el modelo para ambos como muchos a muchos. Tengo una tercera tabla dinámica donde se almacenan los usuarios registrados en el torneo. En esta tabla, también quiero almacenar el nombre de usuario del usuario. Aquí está mi función de almacenamiento.

 $tournament->users()->syncWithoutDetaching([auth()->id()]); $request->session()->flash('success', 'You have joined the tournament'); return back()->with('tournament', $tournament);

¿Qué puedo agregar para poder almacenar el nombre de usuario también? Gracias por adelantado

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Para almacenar el nombre de usuario en la tabla dinámica, simplemente cree una columna adicional llamada user_name en la tabla dinámica y puede guardarla como se muestra a continuación

 $userId = auth()->id(); $userName =auth()->user()->name; $tournament->users() ->syncWithoutDetaching([$userId=>['user_name'=>$userName]]); $request->session()->flash('success', 'You have joined the tournament'); return back()->with('tournament', $tournament);

Para obtener el nombre de usuario también al llamar a la relación, agregue withPivot() a su modelo

 public function relationName(){ return $this->belongsToMany(Model::class)->withPivot('user_name'); }

Actualizar

 $userName = auth()->user()->name ; // Instead name get the value you want from table column name

revisa la documentación

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!