“未定义的变量:Pro(查看:E:\ xampp \ htdocs \ mypro \ resources \ views \ folder \ product.blade.php)”我正在使用laravel 5.6 试图从现有数据库中获取数据。
产品型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $table='products';
protected $primaryKey = 'p_id';
protected $fillable = ['p_title', 'p_element', 'p_description', 'p_duration', 'p_amount', 'p_startDate', 'p_endDate', 'p_old_price', 'p_new_price', 'p_keyWords', 'p_category', 'p_status', 'prefertime'];
}
产品控制器
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
$pro=Product::all();
return view('Folder.product'), compact('pro'));
}
}
Product.blade.php @extends( 'layouts.app')
@section('content')
<h1>Products</h1>
<table style="width:100%">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
<tr>
@foreach($Pro as $row)
<td>{{$row['p_id']}}</td>
<td>{{$row['p_title']}}</td>
@endforeach
</tr>
</table>
@endsection
答案 0 :(得分:0)
首都,你需要使用$ pro而不是$ Pro。
@foreach($pro as $row)
<td>{{$row['p_id']}}</td>
<td>{{$row['p_title']}}</td>
@endforeach
我希望它有效。
答案 1 :(得分:0)
由于已确定pro不是大写的,因此您应该更正如下。在执行页面之前,您还应该检查数组是否为空,否则您会发现自己有更多错误;
另请注意,我已经将您的内容移到了foreach内部,因为需要这样才能使桌面正常显示。
@section(&#39;内容&#39)
<h1>Products</h1>
<table style="width:100%">
<tr>
<th>ID</th>
<th>Name</th>
</tr>
@if(! empty($pro))
@foreach($pro as $row)
<tr>
<td>{{$row['p_id']}}</td>
<td>{{$row['p_title']}}</td>
</tr>
@endforeach
@else
<tr>
<td colspan="2">No records</td>
</tr>
@endif
</table>
@endsection
此外,在您的控制器中应该读取视图
return view('Folder.product', compact('pro'));
不
return view('Folder.product'), compact('pro'));