2017-09-17 2 views
0

in Database Ich habe eine Tabelle von Produkt- und Komponenten
und ich habe eine Tabelle für viele zu viele Beziehung, die component_product es ein Attribute (product_id, component_id haben, ist, Menge)Laravel Illegal Offset-Typ auf vielen zu vielen mit extra Spalte

in Modell Artikel

class Product extends Model 
{ 
     protected $fillable = [ 
     'name','price','user_id','is_avilable','description' 
    ]; 

    public function components() 
    { 
     return $this->belongsToMany('App\Component') 
      ->withPivot('quantity'); 
    } 
} 

in Ansicht

{!! Form::select('component_id[]',$components,null !!} 
{!! Form::select('component_id[]',$components,null !!} 
{!! Form::number('quantity[]',null ]) !!} 
{!! Form::number('quantity[]',null ]) !!} 

in Controller

public function store(Request $request) 
{ 
     $product= Product::create($request->all()); 
     $product->components()->sync($request->component_id => ['quantity'=> $request->quantity ]); 

} 

Es gibt mir einen Fehler von Illegal versetzem

Hinweis: wenn sterben Dump $ request-> Menge oder Anfrage- $> component_id wird es das Array erhalten richtig

Antwort

0

dies ist, wie ich mein eigenes Problem
in Laravel Dokumentation

$user->roles()->sync([1 => ['expires' => true], 2, 3]); 

so passen diese

$manyToMany = array(); 
     for ($i=0 ; $i< count($request->component_id); $i++) 
     { 
      $manyToMany[ $request->component_id[$i] ] = ['quantity' =>$request->quantity[$i] ]; 
     } 
     $product->components()->sync($manyToMany); 
lösen

ist ihre bessere Lösung

Verwandte Themen