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

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办