我对Laravel还不熟悉,我想创建一个小型的本地主机网站,使用MySQL来获取产品。
在我研究并应用了一些答案之后,我仍然有一些问题:
正在尝试获取非对象的属性(视图:
F:\ xampp\htdocs\Laravel\resources\views\welcome.blade.php)
web.php网站
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
//return view('welcome');
$products = DB::table('laravel_products')->pluck('product_name', 'product_about', 'producer_ID', 'product_added');
return view('welcome', ['products' => $products]);
});
欢迎.blade.php
<table class="table table-striped">
<thead>
<tr>
<td>Product name</td>
<td>Description</td>
<td>Date added</td>
</tr>
</thead>
<tbody>
<?php
foreach ($products as $value) {
echo '
<tr>
<td>' . $value->product_name . '</td>
<td></td>
<td></td>
</tr>
';
}
?>
</tbody>
</table>
我应该怎么做才能从MySQL中获取?
最佳答案
你应该试试这个:
Route::get('/', function () {
//return view('welcome');
$products = DB::table('laravel_products')->select('product_name', 'product_about', 'producer_ID', 'product_added')->get();
return view('welcome', compact('products'));
});
<table class="table table-striped">
<thead>
<tr>
<td>Product name</td>
<td>Description</td>
<td>Date added</td>
</tr>
</thead>
<tbody>
@if(isset($products))
@foreach($products as $value)
<tr>
<td> {{$value->product_name}}</td>
<td></td>
<td></td>
</tr>
@endforeach
@endif
</tbody>
</table>
希望这对你有用!!!