dongtan8979 2017-01-02 20:21
浏览 112
已采纳

laravel给我FatalThrowableError:在null上调用成员函数course()

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.

  • 写回答

1条回答 默认 最新

  • duanqia9034 2017-01-02 20:35
    关注

    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);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统