Panamera Turbo S
Panamera Turbo S

Reputation: 45

Laravel fetch from MySQL error: Trying to get property of non-object

I am new to Laravel and I want to make a small localhost website that fetch products using MySQL.

After I researched and apply some answers I still have issues:

Trying to get property of non-object (View: 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]);
});

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>

What should I do to fetch from MySQL?

Upvotes: 1

Views: 128

Answers (1)

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

You should try this :

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>

Upvotes: 1

Related Questions