I'm facing some issues with a project build with Laravel 5.5.
I would like to save datas from a form in a database and display them in a array. I have code a @foreach
loop in the tbody
of the array's section:
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Nom</th>
<th>Prix</th>
<th>Categorie</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
@foreach($products as $product)
<tr>
<td>{{$product->id}}</td>
<td>{{$product->name}}</td>
<td>{{$product->price}}</td>
<td>{{$product->category}}</td>
<td><a href="{{action('ProductController@edit', $product->id)}}"
class="btn btn-warning">Edit</a></td>
<td>
<form action="{{action('ProductController@destroy', $product->id)}}"
method="post">
{{csrf_field()}}
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
Form
<form id ="AddFormProduct" method="post" action="{{url('products')}}">
{{csrf_field()}}
<div class="row">
<div class="col-md-4"></div>
<div class="form-group">
<label for="name">Nom:</label>
<input type="text" class="form-control" name="name">
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="form-group">
<label for="price">Prix:</label>
<input type="text" class="form-control" name="price">
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="form-group">
<label for="category">Categorie:</label>
<select name="selectCategory" id="selectCategory" class="form-control">
@foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
@endforeach
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="form-group">
<button type="submit" class="btn btn-success" style="margin-left:38px">Enregistrer</button>
</div>
</div>
</form>
First problem is that I cannot display the view (with the array & the form) because I have this:
ErrorException (E_ERROR) Undefined variable: products (View: C:\xampp\htdocs\Laratestesources\views\template.blade.php)
I've tried many solutions, the last one is:
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
use View;
class ProductController extends Controller
{
public function index()
{
$products = Product::orderBy('id','name')->get();
return view('template', compact('products','categories'));
// $products = Product::all();
// return view('template', ['products' => $products]);
}
public function create()
{
//
}
public function store(Request $request)
{
$product = $this->validate(request(), [
'name' => 'required',
'price' => 'required|numeric',
'category' => 'required'
]);
Product::create($product);
return back()->with('success', 'Le produit a bien été ajouté !');
}
public function edit($id)
{
$product = Product::find($id);
return view('products.edit',compact('product','id'));
}
public function update(Request $request, $id)
{
$product = Product::find($id);
$this->validate(request(), [
'name' => 'required',
'price' => 'required|numeric',
'category' => 'required'
]);
$product->name = $request->get('name');
$product->price = $request->get('price');
$product->category = $request->get('category');
$product->save();
return redirect('products')->with('success','Le produit a bien été
modifié !');
}
public function destroy($id)
{
$product = Product::find($id);
$product->delete();
return redirect('products')->with('success','Le produit a bien été
supprimé !');
}
}
Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Input;
class Product extends Model
{
protected $fillable = [
'name',
'price',
'category'
];
}
Routes
Route::resource('products','ProductController');
Route::get('/monCommerce', 'ProductController@index')->name('products');
The second problem is that I don't know how to get my values from a select>options
, it's only works with input fields. = RESOLVED
Please suggest.
For your first question change your route to
Route::get('/monCommerce', 'ProductController@index')->name('products');
Route::resource('products','ProductController');
If that is not the case then : It may be because you have an empty table or table without any rows this will definitely return a null object. So check for that error. You can either do this on your controller or on blade itself, I recommend doing it on your controller.
If checking it on blade :
try doing the follow:
@if($products == null || $products == "")
No data found
@else
Your foreach loop
@endif
If trying out on controller: Just use try and catch exception handler.
For your second question try changing
<select name="selectCategory" id="selectCategory" class="form-control">
to
<select name="category" id="selectCategory" class="form-control">
Because you have category field and you have specified category as fillable in your model. If you would like to keep your select name as selectCategory then you will have to change request in your controller.
Just in case if you want to keep selectCategory name in your select form
In your ProductController@store
public function store(Request $request)
{
$request->category=$request->selectCategory;
}