简体   繁体   中英

(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 .

Controller code :

public function index()
{

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

}

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

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>

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

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