我是Laravel的新手,我想製作一個使用MySQL獲取產品的小本地主機網站。Laravel從MySQL獲取錯誤:試圖獲取非對象的屬性
我研究並應用了一些答案後,我仍然有問題:
試圖讓非對象(查看物業: F:\ XAMPP \ htdocs中\ Laravel \資源\意見\ 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]);
});
welcome.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獲取?
'$ value ['product_name']'pluck會返回一個數組而不是集合對象 – linktoahref
我試過了,結果是:Illegal string offset'product_name'(View:F:\ xampp \ htdocs \ Laravel \ resources \ views \ welcome.blade.php) –