jeudi 14 juin 2018

laravel 5.5 - Undefined variable: products in @foreach loop

I'm facing some issues with a project build with Laravel 5.5. Please note that I'm a complete beginner with Laravel and coding in general ^^

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 just like this:

<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></td>
      <td></td>
      <td></td>
      <td></td>
      <td><a href="" 
         class="btn btn-warning">Edit</a></td>
      <td>
      <form action="" 
         method="post">
         
         <input name="_method" type="hidden" value="DELETE">
         <button class="btn btn-danger" type="submit">Delete</button>
      </form>
      </td>
    </tr>
    @endforeach
  </tbody>
</table>

And this is my form:

<form id ="AddFormProduct" method="post" action="">
    
    <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=""></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\Laratest\resources\views\template.blade.php)

I've tried many solutions, the last one is:

In my 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é !');
}
}

My model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Input;

class Product extends Model
{
    protected $fillable = [
        'name', 
        'price', 
        'category'
    ];
}

And the routes that I'va made:

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.

I'm sure that it's only a small detail that block me but I could not find out where...

Someone has an idea ?

Thanks (sorry for my English)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire