简体   繁体   中英

Error of : Trying to get property of non-object

I created a database helper to reduce my code in the laravel framework, and connect to two databases but when I use the function in my helper I receive the error message "Trying to get property of non-object ", this is my helper code:

/**
 * This function Used for get Data For Specific Element.
 */
public static function getDataById($dbName,$tableName,$condition,$data)
{
    $stattment=
        DB::connection($dbName)
            ->table($tableName)
            ->select(['*'])
            ->where($condition, $data)
            ->first();
    return $stattment;
}

this is my controller function

public function all()
{

    $dataView['x']=dBHelper::allData('mysql',
        'products',
        '`status`=? AND `deleted`=?'
        ,array(1,1));

    if(is_object($dataView['x']))
    {
        foreach ($dataView['x'] as $key=>$value):
            $dataView['lang'][$key]=dBHelper::getDataById(
                'mysql2',
                'products',
                'id_product',
                $value->id);
            //var_dump($dataView['lang'][$key]);
        endforeach;
    }

    return view('productss.all',$dataView);
}

this is my view

<div class="content-wrapper">

    <!-- Content Header (Page header) -->

    <section class="content-header">

        <h1>

        </h1>

    </section>

    <!-- Main content -->

    <section class="content">

        <div class="row">

            <div class="col-lg-12">

                <div class="box box-primary">

                    <div class="box-header with-border">

                        <h3 class="box-title ">Show Products</h3>

                    </div>

                    <!-- /.box-header -->

                    <div class="adminform">



                        <!-- form start -->

                        <table id="all_data" class="table table_for_data table-striped table-bordered dt-responsive nowrap tableData" cellspacing="0" width="100%">

                            <thead>

                            <tr>
                                <th>Id</th>
                                <th>User Name</th>
                            </tr>
                            </thead>

                            <tbody>
                            @if(isset($x)&&is_object($x))
                                @foreach($x as $key=>$value)
                                    <tr>
                                    <td>{{$value->id}}</td>
                                    <td>{{$lang[$key]->name}}</td>
                                    </tr>
                                @endforeach

                            @else
                                <td>no data found</td>
                            @endif

                            </tbody>

                        </table>

                    </div>

                </div>

                <!-- /.box -->

            </div>

        </div>

    </section><!-- /.content -->

</div><!-- /.content-wrapper -->

I receive error when I print

<td>{{$lang[$key]->name}}</td>

thanks.

As you return $dataView to the View, you should:

@if(isset($dataView['x']) && is_object($dataView['x']))
    @foreach($dataView['x'] as $key=>$value)
        <tr>
            <td>{{$value->id}}</td>
            <td>{{$value['lang'][$key]->name}}</td>
        </tr>
    @endforeach

@else
    <td>no data found</td>
@endif

Hope this helps. On the other hand, shortcuts like yours make it hard to debug, or expand on a future date. It is better to build out he Models and their relationships, thus making use of Laravel's native ORM. It takes more time but will dramatically speed up the production process of your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM