Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel从MySQL获取错误:尝试获取非对象的属性_Php_Mysql_Laravel - Fatal编程技术网

Php Laravel从MySQL获取错误:尝试获取非对象的属性

Php Laravel从MySQL获取错误:尝试获取非对象的属性,php,mysql,laravel,Php,Mysql,Laravel,我是Laravel的新手,我想创建一个小型的localhost网站,使用MySQL获取产品 在我研究并应用了一些答案之后,我仍然存在一些问题: 正在尝试获取非对象的属性(视图: F:\xampp\htdocs\Laravel\resources\views\welcome.blade.php) web.php <?php /* |-------------------------------------------------------------------------- | Web

我是Laravel的新手,我想创建一个小型的localhost网站,使用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]);
});
<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>
路由::get('/',函数(){ //返回视图(“欢迎”); $products=DB::table('laravel_products')->select('product_name','product_about','PRODUCTOR_ID','product_ADTED')->get(); 返回视图(“欢迎”,紧凑型(“产品”); }); 产品名称 描述 添加日期 @if(isset(产品)) @foreach(产品为$value) {{$value->product_name} @endforeach @恩迪夫
希望这为你工作

$value['product\u name']
因为Pulk将返回一个数组而不是集合对象,我尝试了这样做,结果是:非法字符串偏移量'product\u name'(视图:F:\xampp\htdocs\Laravel\resources\views\welcome.blade.php),我想你已经接近了,但是仍然说:使用未定义的常量乘积-假定'products'@PanameraTurboS等待检查itIt工作时,缺少了“$”: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));});但不显示任何products@PanameraTurboS不no@PanameraTurboS它的报价错误