Reputation: 15
i'm trying to create a product which belongs only to one order. That order id i'm taking with me to create form, but don't know how to pull it to store function. Updated files: ProductController:
use Illuminate\Http\Request;
use App\Order;
use App\Product;
use Illuminate\Support\Facades\Route;
class ProductController extends Controller
{
public function create(Order $order){
return view('order.product-create', compact($order));
}
public function store(Order $order, Request $request){
$this->validate($request, [
'name'=>'required',
'drawing'=>'nullable|file',
'3d_file'=>'nullable|file',
'quantity'=>'integer',
'unit_price'=>'required',
'discount'=>'nullable'
]);
$order->products()->create($request->all());
session()->flash('product-created', 'New product was added successfully');
return redirect()->route('order.view');
}
}
Route:
Route::get('/order/{order}/product/create', 'ProductController@create')->name('product.create');
Route::post('/order/{order}/product', 'ProductController@store')->name('product.store');
view.blade.php ---> link to product create form
<a href="{{route('product.create', $order)}}" class="btn btn-primary">Add new product</a>
product.create.blade.php ---> fragment to method action
action="{{route('product.store', ['order' => $order])}}"
so with this at the moment, i can't get in to product-create.blade.php with error $order is undefined
Upvotes: 0
Views: 1554
Reputation: 1507
Dynamic parameters are defined as curly braces in your route file in routes/web.php or routes/api.php
// routes/web.php
Route::get('/order/{order}/product/create', 'ProductController@create')->name('product.create');
Route::post('/order/{order}/product', 'ProductController@store')->name('product.store');
Use Route model binding in ProductController.php to have Laravel resolve the order out of the container.
// ProductController.php
use App\Order;
use App\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function create(Order $order)
{
// Only get here if laravel resolves the $order model
return view('order.product-create', compact('order'));
}
public function store(Order $order, Request $request)
{
// Only get here if laravel resolves the $order model
//validation
$this->validate($request, [
'name' => 'required',
'drawing' => 'nullable|file',
'3d_file' => 'nullable|file',
'quantity' => 'integer',
'unit_price' => 'required',
'discount' => 'nullable'
]);
// Create related Product model and associate it with the Order model.
// https://laravel.com/docs/7.x/eloquent-relationships#the-create-method
//
// Need to allow for mass assignment in the Product.php model
// https://laravel.com/docs/7.x/eloquent#mass-assignment
$order->products()->create($request->all());
// Flash to session and redirect after creating
session()->flash('product-created', 'New product was added successfully');
return redirect()->route('order.view');
}
}
view.blade.php
<a href="{{route('product.create', ['order' => $order]))}}" class="btn btn-primary">Add new product</a>
product.create.blade.php
action="{{route('product.store', ['order' => $order])}}"
Upvotes: 1
Reputation: 662
The way i understood, you want to pass the Order ID
from your create form to your controller function, and based on that you want to save your form data.
You can pass your Order ID to your POST route
by
{{ route('route_name', ['order_id' => $order->id]) }}
and your Route in web.php
would be
Route::post('/order/{order_id}/product/create', 'ProductController@store')->name('route_name)
Create Method with Relationship Relationship Documentation
You can accept your parameters passed with in your controller function by
public function store($order_id)
{
//validation
//save function
$order = App\Order::findOrFail($order_id); //Fails if the passed id is not present in DB
// Assuming you have one to one relationship
// Product() is the relationship you declare in your model
$product = $order->Product()->create([
'your_columns' => 'values',
]);
// redirect after save
}
Edit : Updated Question
use Illuminate\Http\Request;
use App\Order;
use App\Product;
use Illuminate\Support\Facades\Route;
class ProductController extends Controller
{
public function create(Order $order){
// you are receiving the Order instance by the Order ID (primary key defined)
return view('order.product-create', compact($order));
}
public function store(Order $order, Request $request){
$this->validate($request, [
'name'=>'required',
'drawing'=>'nullable|file',
'3d_file'=>'nullable|file',
'quantity'=>'integer',
'unit_price'=>'required',
'discount'=>'nullable'
]);
$order->products()->create($request->all());
session()->flash('product-created', 'New product was added successfully');
return redirect()->route('order.view');
}
}
and your Routes
// order = order id which is passed to the controller Order $order variable instance
Route::get('/order/{order}/product/create', 'ProductController@create')->name('product.create');
Route::post('/order/{order}/product/create', 'ProductController@store')->name('product.store');
Your form POST
// You can pass the ID of the order result you receive from your `create` function
<form action = "{{ route('product.store',['order' => $order->id]) }}" ... >
Upvotes: 1