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

322
Views
Laravel: ¿Cómo obtengo datos de esta tabla dinámica?

SOLUCIONADO : respuesta publicada a continuación

¿Cómo puedo obtener valores de esta tabla dinámica y de especificaciones?

Quiero mostrar esto en una plantilla como:

-Modelo (nombre de la tabla de especificaciones)

--Marca (tabla dinámica de formulario de atributos): ejemplo 1 (valor de la tabla dinámica)

--Modelo (tabla dinámica de formulario de atributos): ejemplo 123 (valor de la tabla dinámica) ...


En ProductController intenté devolver algo como esto $product = Product::with('specifications')->first(); , pero solo puedo obtener datos de la tabla de especificaciones y si pruebo $product = Product::with('product_specification')->first(); Recibo el error Call to undefined relationship [product_specification] on model [App\Product].


Tabla dinámica:

 public function up() { Schema::create('product_specification', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->integer('product_id')->unsigned()->index(); $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade'); $table->integer('specification_id')->unsigned()->index(); $table->foreign('specification_id')->references('id')->on('specifications')->onDelete('cascade'); $table->string('attribute'); $table->string('value'); }); }

Tabla de especificaciones:

 public function up() { Schema::create('specifications', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->increments('id'); $table->string('name')->unique(); $table->timestamps(); }); }

Modelo del Producto:

 public function specifications() { return $this->belongsToMany(Specification::class, 'product_specification'); }
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Tuve que agregar withPivot() a mi modelo de Producto

 public function specifications() { return $this->belongsToMany(Specification::class, 'product_specification')->withPivot('attribute', 'value'); }

Y luego en la plantilla:

 foreach($product->specifications as $specification) { echo 'name: ' . $specification->name . ' attribute: ' . $specification->pivot->attribute . ' value ' . $specification->pivot->value . '</br>'; }
about 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!