laravel 5.5 - 未定义的变量:@foreach循环中的产品

时间:2018-06-14 10:31:16

标签: php laravel-5 foreach

我在使用Laravel 5.5进行项目构建时遇到了一些问题。

我想从数据库中的表单中保存数据并将其显示在数组中。我在数组的@foreach部分编了一个tbody循环代码:

<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 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>

第一个问题是我无法显示视图(使用数组和表格)因为我有这个:

  

ErrorException(E_ERROR)   未定义的变量:products(查看:C:\ xampp \ htdocs \ Laratest \ resources \ views \ template.blade.php)

我尝试了很多解决方案,最后一个是:

控制器

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

模型

<?php

namespace App\Models;

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

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

路线

Route::resource('products','ProductController');
Route::get('/monCommerce', 'ProductController@index')->name('products');

第二个问题是我不知道如何从select>options获取我的值,它只适用于输入字段。 = 已解决

请建议。

1 个答案:

答案 0 :(得分:0)

关于第一个问题,请改变您的路线

Route::get('/monCommerce', 'ProductController@index')->name('products');
Route::resource('products','ProductController');

如果情况并非如此,那么: 可能是因为你有一个没有任何行的空表或表,这肯定会返回一个空对象。所以检查一下这个错误。您可以在控制器或刀片本身上执行此操作,我建议您在控制器上执行此操作。

如果在刀片上检查:

尝试执行以下操作:

@if($products == null || $products == "")
  No data found
@else
  Your foreach loop
@endif

如果尝试控制器: 只需使用try和catch异常处理程序。

对于您的第二个问题,请尝试更改

<select name="selectCategory" id="selectCategory" class="form-control">

   <select name="category" id="selectCategory" class="form-control"> 

因为您有类别字段,并且您已在模型中将类别指定为可填写。如果您希望将您的选择名称保留为selectCategory,则必须在控制器中更改请求。

以防万一你想在选择表单中保留selectCategory名称

在您的ProductController @ store

public function store(Request $request)
{
   $request->category=$request->selectCategory;
}