Reputation: 371
Default model:
{!! Form::model($stock, ['route' => ['stock.update_variant', $stock->id],'method'=>'put']) !!}
{!! Form::close() !!}
I want to do the following;
{!! Form::model($stock, $new_variant, ['route' => ['stock.update_variant', $stock->id],'method'=>'put']) !!}
{!! Form::close() !!}
Is this possible?
Upvotes: 1
Views: 475
Reputation: 1033
I can't say for certain that you can or cannot do this as the documentation for Laravel Blade Forms has been slowly being abandoned and in fact isn't even managed by Taylor Otwell anymore (last updated was for Laravel 5.4). However! You do have another option!
If you generate a Stock instance and use a join in your database query, you can load up your $stock object with values from both tables and thus would only need to use the $stock object in your form method -> change it's name to $formData as not to confuse yourself.
For example:
You are currently doing this I imagine:
$stock = Stock::where('x', 'y')->findOrFail('1');
$new_variant = NewVariant::where('z', 'a')->findOrFail($stock->id);
return $view('/')->with([compact('stock'), compact('new_variant')]);
Instead, use a join query:
$formData = Stock::where('x', 'y')
->join('new_variants', 'stocks.id', '=', 'new_variants.stock_id')
->select('stocks.*', 'new_variants.*')
->findOrFail('1');
return $view('/')->with(compact('formData'));
This is basically saying get the stock where x = y and join it with a corresponding new variant in the new_variants table by matching the corresponding stock ID. I believe the select method is redundant if you wish to return all of the fields. Now, you can simply access the $formData
object in your from model binding and it should work.
I will caution you with saying this is a custom solution and does come with it's own drawbacks. The big one being the form request will no longer be associated with a model and so you will have to de-conflict the data when the form is submitted to ensure the appropriate models are updated with the appropriate information. Additionally, if the two tables contain a field of the same name, you will have to deconflict this in you select method on the query. If this is the case please see the Laravel documentation on raw statements/selects.
I hope this at least points you in the right direction :)
Upvotes: 1