Laravel - 尝试获取非对象的属性(使用auth::)

时间:2017-04-18 09:11:26

标签: laravel authentication laravel-5

我在使用' Auth ::'时遇到了困难。在laravel中,我试图让它工作,以便检查用户ID,然后显示正在使用该用户ID的所有约会。在约会表格中,它使用' user_id'

AppointmentController.php第22行中的ErrorException: 试图获得非对象的属性

约会控制器(这是错误的一行)

  function index()
    {   
        $appointments = Appointment::where('user_id', Auth::user()->id);
        return view ('appointment/userappointments',['appointments' => $appointments]);
    }

userappointments.blade

@extends('layouts.master')

@section('title', 'Appointments')

@section('content')
    <h1>Your Appointments</h1>
     @foreach ($appointments as $appointment)
        <p>
                 <a href="{{url('details/'.$appointment->id)}}" >{{$appointment->doctor->name}} : {{$appointment->time}} : {{$appointment->date}}</a>   
        </p>
    @endforeach
@endsection

1 个答案:

答案 0 :(得分:1)

更改

$appointments = Appointment::where('user_id', Auth::user()->id);

$appointments = Appointment::where('user_id', Auth::user()->id)->get();

请参阅Laravel文档中的this示例。