Blade 不显示数据库中的模型值
Blade not displaying model values from the database
我正在尝试为用户显示产品列表,令我惊讶的是这并没有显示在页面上,但是,它被插入到数据库中,这可能是什么问题?
public function index()
{
$listProducts = Product::where('user_id', '=', Auth::user()->id)->orderBy('productname', 'desc');
return view('product.index')
->with('products', $listProducts);
}
Blade 文件:
@if(Auth::check())
@foreach($products as $product)
<TR><TD>{!! $product->companyname !!}</TD><TD>{!! $product->productname !!}</TR>
@endforeach
@endif
问题是您需要return数组中的数据:
return view('product.index', ['products' => $listProducts]);
以及改变这个:
$listProducts = Product::where('user_id', '=', Auth::user()->id)->orderBy('productname', 'desc');
为此:
$listProducts = Product::where('user_id', '=', Auth::user()->id)->orderBy('productname', 'desc')->get();
我正在尝试为用户显示产品列表,令我惊讶的是这并没有显示在页面上,但是,它被插入到数据库中,这可能是什么问题?
public function index()
{
$listProducts = Product::where('user_id', '=', Auth::user()->id)->orderBy('productname', 'desc');
return view('product.index')
->with('products', $listProducts);
}
Blade 文件:
@if(Auth::check())
@foreach($products as $product)
<TR><TD>{!! $product->companyname !!}</TD><TD>{!! $product->productname !!}</TR>
@endforeach
@endif
问题是您需要return数组中的数据:
return view('product.index', ['products' => $listProducts]);
以及改变这个:
$listProducts = Product::where('user_id', '=', Auth::user()->id)->orderBy('productname', 'desc');
为此:
$listProducts = Product::where('user_id', '=', Auth::user()->id)->orderBy('productname', 'desc')->get();