Friday, March 31, 2017

Laravel 5.2 Update with foreign key

hello i've been working on a laravel project lately, and i've wanted to be able to update my records that have foreign keys in them. i've succesfully been able to update records in tables that dont have a foreign key but for some reason my table with a foreign key doesn't want to update. when i var_dump($voorraad) i get the new value on my screen but it doesn't update into the database. I have the same code for my Product table and Location table and there it works completely fine.

My Controller:

 public function update(Request $request, $id)
{
    //voorraad = stock in dutch
    //Product_id = foreign key from the table products
    //locatie_Id = foreign key from the tabe locations
    //aantal = ammount of a certain product

    $voorraad = Voorraad::FindOrFail($id);


    $voorraad->fill($request->only('aantal', 'Product_id', 'locatie_Id'));

    $voorraad->save();

    return redirect(route('voorraad.index'));

}

My Model:

protected $fillable = ['aantal', 'Product_id', 'locatie_Id'];

My edit form:

    {!! Form::model($voorraad,['action' => ['VoorraadsController@update', $voorraad->Id], 'method' => 'PATCH'])!!}

{!! Form::label('aantal', 'aantal:') !!}
{!! Form::text('aantal')!!}<br>

{!! Form::label('Product_id', 'product:') !!} 
{!! Form::select('Product_id', $products)!!}<br>

{!! Form::label('locatie_Id', 'locatie:') !!} 
{!! Form::select('locatie_Id', $locaties)!!} <br>

{!! Form::submit('edit') !!}

Gyazo of Var_dump($voorraad): https://gyazo.com/109d54e2bb7a91bbb8b047611e66dbe0

gyazo of var_dump($request):https://gyazo.com/8437ee90881ba38a039b5b583f8296a5

If there is any information missing just let me know and i'll add it



via B. All

Advertisement