I have 2 simple views: home
and create
. Here is the controller for them
public function index()
{
$products = Product::all();
return view('home', compact('products'));
}
public function createProduct()
{
return view('productCreate');
}
public function createProductSubmit( Request $request ) {
$product = new Product;
$product->productName = $request->name;
$product->productPrice = $request->price;
$product->quantity = strip_tags($request->quantity);
$product->category_id = $request->category;
$product->save();
return view('home');
}
and the routes
Route::get('/home', 'HomeController@index');
Route::get('/home/product/create', 'HomeController@createProduct');
Route::post('/home/product/create', 'HomeController@createProductSubmit');
The problem is that when I go in /home/product/create
to create new product and after I add everything and submit the form I've get the error
Undefined variable: products in home.blade.php
Why I get this error? In my index()
function I have passing it?
Update
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Product Name</th>
<th>Product Price</th>
<th>Product Quantity</th>
<th>Category</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach( $products as $product )
<tr>
<th>{{ $product->id }}</th>
<td>{{ $product->productName }}</td>
<td>{{ $product->productPrice }}</td>
<td>{{ $product->quantity }}</td>
<td>{{ $product->category_id }}</td>
<td><button class='btn btn-success'>Edit</button> <button class='btn btn-danger'>Delete</button></td>
</tr>
@endforeach
</tbody>
</table>