Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theblack68's avatar

Input text with Pivot Table

Hy ...I'm new to Laracast. I have a problem and I try to explain.

    I have three tables
  • pellets
  • specie
  • specie_pellet (The specie are the trees)
    The tree_pellet is a pivot table with fields
  • pellet_id
  • specie_id
  • percentage

I have setting my models for the relations so:

Pellet

public function specie()
    {
        return $this->belongsToMany(Specie::class)->withPivot('percentuale');
    }

Specie

public function pellets()
    {
        return $this->belongsToMany(Pellet::class)->withPivot('percentuale');
    }

I have also create a Form for Update the tree_pellet so:

{!! Form::model($pellet, ['method' => 'PUT', 'route' => ['composizione.update', $pellet->id]]) !!}


Specie
Percentuale
Azione


@foreach($pellet->specie as $specie)

{!! Form::select('specie[]', $listaSpecie, $specie->id, ['class' => 'form-control']) !!}

Elimina 

@endforeach


{!! Form::submit('Aggiorna Composizione', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
    A pellet can have multiple configurations:
  • 5 - 1 - 50 (5 is the pellet_id - 1 is the specie_id - 50 is the percentage)
  • 5 - 7 - 50 (5 is the pellet_id - 7 is the specie_id - 50 is the percentage)
  • (example: a bio pellet is make with 50% of silver fir and 50% red fir)

But when I try to update i receive this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into `pellet_specie` (`0`, `pellet_id`, `specie_id`, `1`) values (1, 5, 0, 7))

Someone can try to help me please?

0 likes
3 replies
lmxdev's avatar

you should have:

in Pellet model:

 public function species() { 
    return $this->belongsToMany(Specie::class)->withPivot('percentuale'); 
}

in Specie model:

public function pellets(){ 
    return $this->belongsToMany(Pellet::class)->withPivot('percentuale'); 
}

also show the code you use to insert.

theblack68's avatar

in Pellet

public function specie()
    {
        return $this->belongsToMany(Specie::class)->withPivot('percentuale');
    }
In Specie
public function pellets()
    {
        return $this->belongsToMany(Pellet::class)->withPivot('percentuale');
    }

Please or to participate in this conversation.