Q:

laravel giving me FatalThrowableError: call to memeber function course() on null

I have a web app that has been working for sometime but now i am suddenly receiving the following error: 

Symfony\Component\Debug\Exception\FatalThrowableError: Call to a
  member function course() on null in
  /home/forge/default/releases/20161130183258/app/Http/Controllers/CoursePageController.php:37

when i open up the file location i see the following code:
    <?php

namespace App\Http\Controllers;

use App\CMS\Page;
use Illuminate\Http\Request;

use App\Http\Requests;

class CoursePageController extends Controller
{

    /**
     * Display the specified resource.
     * @param $course_slug
     * @param $slug
     * @return \Illuminate\Http\Response
     * @internal param int $id
     */
    public function show($course_slug, $slug)
    {
        $page = $this->getPage($course_slug, $slug);

        return view($page->layout->templateFullPath())
            ->withPage($page);
    }

    /**
     * Gets page by slug.
     * @param $course_slug
     * @param $slug
     * @return mixed
     */
    protected function getPage($course_slug, $slug)
    {
        $page = Page::with('layout', 'contentRegions')->whereSlug($slug)->first();
        $course = $page->course()->whereSlug($course_slug)->first();
        abort_if(! isset($page) || !isset($course), 404);

        return $page;
    }}

not sure why i would be getting a null error for $course. I'm the new (and only) admin for this app and i basically have the laravel knowledge of a toddler so any help would be greatly appreciated. 

A:

You have to check if it got a page before using it: 
<?php

namespace App\Http\Controllers;

use App\CMS\Page;
use Illuminate\Http\Request;

use App\Http\Requests;

class CoursePageController extends Controller
{

    /**
     * Display the specified resource.
     * @param $course_slug
     * @param $slug
     * @return \Illuminate\Http\Response
     * @internal param int $id
     */
    public function show($course_slug, $slug)
    {
        $page = $this->getPage($course_slug, $slug);

        return view($page->layout->templateFullPath())
            ->withPage($page);
    }

    /**
     * Gets page by slug.
     * @param $course_slug
     * @param $slug
     * @return mixed
     */
    protected function getPage($course_slug, $slug)
    {
        if ($page = Page::with('layout', 'contentRegions')->whereSlug($slug)->first()) {
            if ($page->course()->whereSlug($course_slug)->first()) {
                return $page;
            }
        }

        abort(404);
    }
}

