简体   繁体   English

(2/2)ErrorException试图获取非对象的属性

[英](2/2) ErrorException Trying to get property of non-object

In course table have department field where the value is number and when i get data form this table i don't want to show number.I want to show department name but i can not do it i want expert help.I have attached picture and code this is course table . 在课程表中有部门字段,值是数字,当我从该表中获取数据时,我不想显示数字。我想显示部门名称,但我不能这样做,我需要专家帮助。代码this is course table

Controller code : 控制器代码:

public function index()
{

    $allCourse = Course::paginate(10);
    return view ('Admin.course.index',['allCourse'=> $allCourse]);

}

index.blade.php index.blade.php

@foreach($allCourse as $course)

                    <tr class="info">
                        <td class="success">{{$course->id}}</td>
                        <td class="success">{{$course->code}}</td>
                        <td class="success">{{$course->name}}</td>
                        <td class="success">{{$course->credit}}</td>
                        <td class="success">{{$course->description}}</td>
                        <td class="success">{{$course->department->$department}}</td>
                        <td class="success">{{$course->semester}}</td>
                        <td class="success">
                            <div class="btn btn-success">
                                <a href="{{ url('/Admin/course',[$course->id,'edit']) }}"><i class="fa fa-pencil" aria-hidden="true"></i></a>
                            </div>

                            <div class="btn btn-danger">
                                <a class="delete_link" href="{{ url('/Admin/course',[$course->id,'delete']) }}"><i class="fa fa-trash" aria-hidden="true"></i></a>
                            </div>

                            {{--  {!! Form::open(['url' => 'foo/bar']) !!}
                                      <div>
                                          {{ Form::button('<i class="fa fa-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger '] )  }}
                                      </div>
                              {!! Form::close() !!}--}}

                        </td>

                    </tr>

                @endforeach

            </table>
            {{ $allCourse->links() }}
        </div>
    </div>

</div>

@endsection

error page 错误页面

Make sure you have Relation between course and department 确保您具有coursedepartment之间的关系

You can find the documentation here 您可以在这里找到文档

Secondly, you have a typo in your code. 其次,您的代码中有一个错字。 Change this to 更改为

<td class="success">{{$course->department->$department}}</td> this <td class="success">{{$course->department->$department}}</td>

<td class="success">{{$course->department->department}}</td>

Hope this helps. 希望这可以帮助。

Eloquent just fetch everything inside your specified table. 雄辩的只是获取指定表中的所有内容。 If you want department name (i assume you have department table), you need to define the relationship. 如果需要部门名称(假设您有部门表),则需要定义关系。 Eloquent do have method for defining relationship. 雄辩的人确实有定义关系的方法。

For me, just use fluent 'join'. 对我来说,只需使用流畅的“加入”即可。

Course::join('department_table','Course.department','=','department_table.department_id')->paginate(10);

Once you change your query to this, change from 将查询更改为此后,请从

{{$course->department->$department}}

to

{{$course->department_name}}

This is the easier way, but if you are willing to study, have a look here https://laravel.com/docs/5.5/eloquent-relationships 这是更简单的方法,但是如果您愿意学习,请在这里看看https://laravel.com/docs/5.5/eloquent-relationships

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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