【问题标题】:Laravel undefined variable issueLaravel 未定义变量问题
【发布时间】:2020-06-12 16:53:33
【问题描述】:

我正在尝试从数据库中获取数据并使用 foreach 循环进行打印,但出现未定义变量错误。

在我的 web.php 中有

Route::get('/ShowProducts' , 'ProductController@ShowProducts');

这是我在控制器中的代码

function ShowProducts(){
    $show = ProductModel::where('ID')->get();

    $show->Product_Name;
    $show->Count;
    $show->Price;

    return view('profile');
}

【问题讨论】:

  • ` $show = ProductModel::where('ID')->get();`.哪里有什么?您需要在 where 子句中提供一个值进行比较
  • 我怀疑您没有将数据传递给您的视图,您能否也发布您的视图代码?
  • 请发布您的完整错误消息以及您收到此错误的文件和行号?
  • 你的代码对我没有任何意义
  • 你能告诉你刀片文件代码吗?

标签: laravel


【解决方案1】:

您的函数中没有 $id 参数,如果您想列出所有产品,请使用:

function ShowProducts() {
   $products = ProductModel::all();
   return view('profile', ['products' => $products]);
}

在你的刀片上:

@foreach($products as $prod)
    <h3 class="product-title">
        <a href="product.html">{{$prod->Product_Name}}</a>
    </h3>
@endforeach

如果要显示单个产品,请使用{id}添加新路线:

Route::get('/ShowProducts/{id}' , 'ProductController@ShowProduct');

在你的控制器上:

function ShowProduct($id) {
   $product = ProductModel::findOrFail($id);
   return view('profile', ['product' => $product]);
}

【讨论】:

    【解决方案2】:

    您应该将变量传递到视图中。你没有这样做,这就是你得到undefined variable错误的原因。

    有很多方法可以将变量传递到视图中。

    Using array directly in view method
    return view('welcome', ['variable_name' => $variable_value]);
    
    Using with method
    return view('welcome')->with('variable_name', $variable_value);
    
    Using with method shortcut
    return view('welcome')->withVar($variable_value);
    // laravel parses 'withVariable' method name then extracts variable name and you will get variable called 'var' in your blade file
    
    Using php's compact function
    // It is more clear and readable to use compact if you are not doing conditionals and etc
    $var1 = 'val1';
    $var2 = 'val2';
    return view('welcome', compact('var1', 'var2'));
    // Compact methods gets variable name by string and retuns associative array with 'name' => 'value' signature
    

    我通常使用 fis 和 last 方法

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      让你的代码如下。

      控制器代码

      function ShowProducts(){
          $products = ProductModel::all();
          return view('profile', compact('products'));
      }
      

      profile.blade.php 文件。

      @if(!empty($products))
      @foreach($products as $product)
       {{ $product->Product_Name }}
      @endforeach
      @endif
      

      【讨论】:

      • 我的表不是空的但是没有打印产品名称
      • 你能在刀片文件中 print_r($products) 看看 products 变量中的实际内容吗?
      【解决方案4】:

      这段代码中有不少奇怪的地方:

      $show = ProductModel::where('ID')->get();
      

      此查询不比较任何值。您需要一个适当的 where 子句,例如:

      ProductModel::where('ID','=',$id)->get(); //ID is the AI column in your database, make sure you used capital letters for that.
      

      接下来是你的变量赋值。在 laravel 中,get 用于返回记录的集合。在您的情况下,您需要像 first() 这样的东西,因为您过滤了 id。

       ProductModel::where('ID','=',$id)->first();
      

      然后您将能够像您尝试过的那样分配值,但在您的代码中:

      $show->Product_Name;
      $show->Count;
      $show->Price;
      

      这些字段当前是undefined,因为那里有一个带有键 [0] 的嵌套数组,因为您当然只返回 1 条记录。这就是get() 的工作原理。因此,正如我上面提到的,使用first() 来完成这项任务。

      【讨论】:

        【解决方案5】:

        首先,您要查找的实际ID 在哪里?

        其次,$show-&gt;Product_Name; 是干什么用的?

        第三,访问 laracasts.com 并学习一些教程。您正在寻找的功能是:

        Route::get('/ShowProducts/{id}' , 'ProductController@ShowProducts');
        
        function ShowProducts($id){
            $show = ProductModel::find($id);
            return view('profile', compact('show'));
        }
        

        【讨论】:

          【解决方案6】:

          您好,这是正常的,因为您没有将变量传递给您的视图。

           function ShowProducts(){
             $show = ProductModel::where('ID')->get();
             return view('profile', compact('show'));
          }
          

          现在只需在刀片中使用 $show。

          【讨论】:

          • compact 函数有什么作用?
          • 对不起,我有同样的错误。在我的刀片中,我有 @foreach($show as $prod) 3 class="product-title">{{$prod->Product_Name}} @endforeach
          • 类 ProductModel 扩展模型 { public $table = 'products';公共 $timestamps = 假; public function photos(){ return $this->hasMany( 'App\PhotoModel', 'Product_id' , 'ID' ); } public function saller(){ return $this->belongsTo( 'App\UserModel' , 'user_ID' , 'ID' ); // 属于一对多 kapy } }
          • add this protected $fillable = ['Product_Name']; add your all field (field DB) on this array
          猜你喜欢
          • 2018-01-08
          • 2021-05-30
          • 1970-01-01
          • 1970-01-01
          • 2017-09-22
          • 2019-09-21
          • 1970-01-01
          • 1970-01-01
          • 2014-09-26
          相关资源
          最近更新 更多