You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 5.6视图报错:未定义变量Pro,无法获取数据库数据

Fixing the "Undefined variable: Pro" Error in Laravel 5.6 View

Hey there, let's work through why you're hitting that Undefined variable: Pro error in your view. This is a super common issue with Laravel, and we can nail it down with a few quick checks:

1. Make sure your controller is passing the Pro variable to the view

This is the most likely culprit. You need to explicitly fetch data from your Product model and send it to the view in your controller method. Here's how to do it properly:

// Example in your ProductController.php
namespace App\Http\Controllers;

use App\Product;

class ProductController extends Controller
{
    public function showProducts()
    {
        // Fetch data from the products table (adjust this to your needs)
        // Get a single product: $Pro = Product::find(1);
        // Get all products: $Pro = Product::all();
        $Pro = Product::where('p_cat', 'your-category')->get();

        // Pass the variable to your view (match the view path exactly)
        return view('folder.product', compact('Pro'));
        // Alternative syntax: return view('folder.product')->with('Pro', $Pro);
    }
}

If you skip this step and don't pass $Pro to the view, Laravel has no way to know what that variable is—hence the error.

2. Watch out for case sensitivity in variable names

PHP variables are case-sensitive! If your controller uses $pro (lowercase 'p') but your view references $Pro (uppercase 'P'), Laravel will treat them as two different variables. Double-check that the variable name matches exactly in both places.

3. Confirm your Product model is set up correctly

From the code you shared, your model looks properly configured (you've set $table and $primaryKey correctly), but just to be thorough:

  • Ensure your database actually has a products table with the columns listed in $fillable
  • Test that your model can fetch data by running a quick query in tinker (run php artisan tinker in your project root, then try Product::first()—if it returns a product, your model is working)

4. Verify your view path is correct

Your view lives at resources/views/folder/product.blade.php, so make sure your controller is returning view('folder.product') (the dot corresponds to the directory structure). A typo here (like folder.products instead of folder.product) would mean Laravel loads the wrong view, which might not have the variable passed to it.

5. Clear Laravel's cache (just in case)

Sometimes cached views or configs can cause weird issues. Run these commands in your project root to clear them:

php artisan view:clear
php artisan cache:clear

Example working view usage

Once you've passed $Pro correctly, you can use it in your product.blade.php like this:

@if($Pro)
    @foreach($Pro as $product)
        <h3>{{ $product->p_title }}</h3>
        <p>{{ $product->p_description }}</p>
    @endforeach
@else
    <p>No products found.</p>
@endif

内容的提问来源于stack exchange,提问作者tSharif

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:33:58