PHP / LARAVEL 5 MainController.php 中的错误异常

PHP / LARAVEL 5 ErrorException in MainController.php

全部。 进行一些新步骤后我遇到了问题。我是 rails 开发人员,但现在我必须在 php/laravel 项目中做一些支持。在项目中制作一些 UI + 后端(在项目 + 管理员中添加 OpenGraph)之后 - 我做了一个命令 - php artisan migrate:fresh。 现在我有这个代码错误。

(1/1) 错误异常 尝试获取 属性 'title' 的非对象

在 MainController.php 第 78 行

MetaTag.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Setting
 *
 * @mixin \Eloquent
 */
class MetaTag extends Model
{
    /**
     * @var array
     */
    protected $table = "meta_tags";
    protected $fillable = ['title', 'description', 'keywords','og_type','og_title','og_description'];

}

MainController.php的一部分




<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Jenssegers\Agent\Agent;
use App\Http\Requests;

class MainController extends Controller
{
    public function index()
    {
        $main_banner = \App\Banner::where('type', '1')
            ->where('page', '1')
            ->where('active', '1')
            ->orderBy('order', 'asc')
            ->first();

        $premium_blocks = \App\Banner::where('type', '2')
            ->where('active', '1')
            ->orderBy('order', 'asc')
            ->get();

        $active_animations = \App\Setting::where('alias', 'anumations_active')->first();
        $active_films = \App\Setting::where('alias', 'films_active')->first();
        $active_games = \App\Setting::where('alias', 'games_active')->first();
        $active_heroes = \App\Setting::where('alias', 'heroes_active')->first();
        $active_news = \App\Setting::where('alias', 'news_active')->first();
        $active_soundtracks = \App\Setting::where('alias', 'soundtracks_active')->first();

        $animations = \App\Animation::where('active', '1')
            ->orderBy('order', 'asc')
            ->get();
        $films = \App\Film::where('active', '1')
            ->where('checked', '1')
            ->orderBy('order', 'asc')
            ->get();
        $games = \App\Game::where('active', '1')
            ->where('checked', '1')
            ->orderBy('order', 'asc')
            ->get();
        $heroes = \App\Hero::where('active', '1')
            ->where('checked', '1')
            ->orderBy('order', 'asc')
            ->get();
        $news = \App\News::where('active', '1')
            ->where('checked', '1')
            ->orderBy('order', 'asc')
            ->get();
        $soundtracks = \App\Soundtrack::where('active', '1')
            ->where('checked', '1')
            ->orderBy('order', 'asc')
            ->get();

        $arr_month_rus = \Config::get('settings.arr_month_rus');
        $arr_month_rus_lower = \Config::get('settings.arr_month_rus_lower');
        $arr_games_types = \Config::get('settings.arr_games_types');
        $arr_news_types = \Config::get('settings.arr_news_types');

        $agent = new Agent();

        $browser = $agent->browser();
        $v = explode('.',$agent->version($agent->browser()));
        $version = $v[0];

        if (
            ($browser == "Internet Explorer" && $version > 10) ||
            ($browser == "Edge" && $version > 11) ||
            ($browser == "Firefox" && $version > 46) ||
            ($browser == "Opera" && $version > 38) ||
            ($browser == "Safari" && $version > 8) ||
            ($browser == "Chrome" && $version > 51)
        )
            $outdated = false;
        else $outdated = true;

        $meta_tags = \App\MetaTag::where('alias',  '=', 'main_page')->first();
        $title = $meta_tags->title;
        $description = $meta_tags->description;
        $keywords = $meta_tags->keywords;
        $og_title = $meta_tags->og_title;
        $og_description = $meta_tags->og_description;
        $og_type = $meta_tags->og_type;

        return view((($agent->isMobile()) ? 'mobile.home' : 'home'), compact(
            'main_banner',
            'premium_blocks',
            'animations',
            'films',
            'games',
            'heroes',
            'news',
            'soundtracks',
            'active_animations',
            'active_films',
            'active_games',
            'active_heroes',
            'active_news',
            'active_soundtracks',
            'arr_games_types',
            'arr_month_rus_lower',
            'arr_news_types',
            'arr_month_rus',
            'outdated',
            'title',
            'description',
            'keywords',
            'og_title',
            'og_type',
            'og_description'
        ));
    }

我希望能解决这个小问题。 谢谢!

检查此行 returns 是否有任何内容:

$meta_tags = \App\MetaTag::where('alias',  '=', 'main_page')->first();

您可以像这样添加 if 语句(但要为变量设置默认值以防止出错):

$meta_tags = \App\MetaTag::where('alias', 'main_page')->first(); // You CAN ignore the equal sign
if($meta_tags){
    $title = $meta_tags->title;
    $description = $meta_tags->description;
    $keywords = $meta_tags->keywords;
    $og_title = $meta_tags->og_title;
    $og_description = $meta_tags->og_description;
    $og_type = $meta_tags->og_type;
} else {
    $title = "";
    $description = "";
    $keywords = "";
    $og_title = "";
    $og_description = "";
    $og_type = "";
}

我认为你应该试试这个。这里的逻辑是先检查记录是否存在于数据库中。

$meta_tags_count = \App\MetaTag::where('alias', 'main_page')->count();
if($meta_tags_count>0){
    $meta_tags = \App\MetaTag::where('alias', 'main_page')->first(); // You CAN  ignore the equal sign
    $title = $meta_tags->title;
    $description = $meta_tags->description;
    $keywords = $meta_tags->keywords;
    $og_title = $meta_tags->og_title;
    $og_description = $meta_tags->og_description;
    $og_type = $meta_tags->og_type;
}
 else {
    $title = "";
    $description = "";
    $keywords = "";
    $og_title = "";
    $og_description = "";
    $og_type = "";
}