在laravel 5上调用null上的成员函数tasks()

时间:2017-06-06 11:03:37

标签: laravel-5

让我用几个字来解释我的问题。 在我的控制器上我有这一行:

$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();

这适用于现有用户,但是当我尝试创建新的用户时

Call to a member function tasks() on null

我能用这样的东西或你的建议吗?

if(!is_null($tasks))
    $tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();
}

这是我在控制器上显示的功能

public function show(){
    $user = Auth::user();

    if(!$user)
        return redirect('/')->withErrors(config('constants.NA'));

    $countries = Country::all()->lists('name', 'id')->toArray();
    $profile = $user->profile;
  $student = $profile->student;

  // Tasks showed on students profile
  if ($student->group) {
      $tasks = $student->group->tasks()
          ->orderBy('created_at', 'desc')
          ->withPivot('id')
          ->get();
  }
  // Classmates
  if ($student->group) {
      $classmates = $student->group->students()
      ->where('id', '!=', $student->id)
      ->get();

  }

  // Activated books
  $books = Auth::user()->books()->orderBy('grade_id', 'asc')->get();


  if(!is_null($student->group))
    $iTasks = $student->group->tasks->count();
  else {
    $iTasks = 0;
  }

  $iTodos = $user->todos->count();

    return view('student.profile.show',compact('user','profile', 'countries', 'iTasks', 'iTodos', 'tasks', 'classmates', 'books'));
}

这是我的展示视图,用于任务

<div class="tab-pane <?php if(isset($tab) && $tab == 'timeline'): ?> active <?php endif; ?>" id="timeline">
                        @if($tasks->count())
                          <div class="timeline">
                            @foreach($tasks as $task)
                              <!-- TIMELINE ITEM -->
                              <div class="timeline-item">
                                <div class="timeline-badge">
                                  <div class="timeline-icon">
                                    <i class="mdi mdi-clipboard-text font-red-intense"></i>
                                  </div>
                                </div>
                                <div class="timeline-body">
                                  <div class="timeline-body-arrow"> </div>
                                  <div class="timeline-body-head">
                                    <div class="timeline-body-head-caption">
                                      <span class="timeline-body-title font-blue-madison">
                                        {{ $task->professor->profile->user->name }}
                                      </span>
                                      <span class="timeline-body-time font-grey-cascade">ju ca caktuar një <a href="/student/tasks" class="font-red sbold">detyrë</a> të re.</span>
                                    </div>
                                  </div>
                                  <div class="timeline-body-content">
                                    <span class="font-grey-cascade">
                                      {{ $task->pivot->comment }}
                                    </span>
                                  </div>
                                  <hr>
                                  Lenda: <span class="timeline-body-time font-grey-cascade sbold">{{ $task->subject->name }}</span>
                                  <div class="pull-right">
                                    Krijuar më: <span class="timeline-body-time font-grey-cascade sbold">{{ $task->created_at->format(Config::get('klasaime.date_format')) }}</span>
                                  </div>
                                </div>
                              </div>
                              <!-- END TIMELINE ITEM -->
                            @endforeach
                          </div>
                        @else
                          <div class="alert">
                            Ju nuk keni asnje detyrë të caktuar!
                          </div>
                        @endif
                      </div>

2 个答案:

答案 0 :(得分:0)

在我看来,您还没有将学生添加到某个群组,或者某种程度上还没有坚持下去。如果您在创建和保存学生后将学生添加到组中,请尝试以下操作:

$student->load('group')

跑步前:

$tasks = $student->group->tasks()->orderBy('created_at', 'desc')->withPivot('id')->get();

我需要查看更多代码才能提供更准确的答案。但是,您收到的错误与您学生的->group有关,而与您的学生本身无关。

答案 1 :(得分:0)

进行简单的检查以查看该组是否存在,然后继续执行您需要执行的任何操作。

// controller
$tasks = null;

if ($student->group) {
    $tasks = $student->group->tasks()
        ->orderBy('created_at', 'desc')
        ->withPivot('id')
        ->get();
}

// view
@if($tasks)
    {{ $tasks->id }}
@else
    No tasks found
@endif

修改:在您的控制器中,在顶部添加$tasks = null;$classmates = null;。在您的观点中,将@if($tasks->count())更改为@if($tasks)。我不确定您使用同学变量的位置,但在使用它之前添加一个检查以查看它是否为空。