Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

323
Visualizações
Laravel: How do I get data from this pivot table?

SOLVED: answer posted below

How can I get values from this pivot and specifications table?

I want to display this in a template like:

-Model (name from specifications table)

--Brand (attribute form pivot table): example1 (value from pivot table)

--Model (attribute form pivot table): example123 (value from pivot table) ...


In ProductController I tried returning something like this $product = Product::with('specifications')->first();, but then I can get only data from specifications table and if I try $product = Product::with('product_specification')->first(); I get error Call to undefined relationship [product_specification] on model [App\Product].


Pivot table:

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');
    });
}

Specifications table:

public function up()
{
    Schema::create('specifications', function (Blueprint $table) {
        $table->engine = 'InnoDB';

        $table->increments('id');
        $table->string('name')->unique();
        $table->timestamps();
    });
}

Product model:

public function specifications() 
{
    return $this->belongsToMany(Specification::class, 'product_specification');
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I had to add withPivot() to my Product model

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

And then in template:

foreach($product->specifications as $specification) {
    echo 'name: ' . $specification->name . ' attribute: ' . $specification->pivot->attribute . ' value ' . $specification->pivot->value . '</br>';
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda