【问题标题】:PHP / LARAVEL 5 ErrorException in MainController.phpMainController.php 中的 PHP / LARAVEL 5 ErrorException
【发布时间】:2019-10-17 18:08:27
【问题描述】:

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

(1/1) 错误异常 试图获取非对象的属性“标题”

在 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'
        ));
    }

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

【问题讨论】:

  • 愚蠢的问题...数据库是否包含select * from meta_tags where alias="main_page"

标签: php laravel laravel-5 laravel-5.5 php-7


【解决方案1】:

检查此行是否返回任何内容:

$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 = "";
}

【讨论】:

  • 请记住,compact() 调用不存在变量
  • @Rouhollag - 尝试你的方法,没有帮助。我认为主要问题是 $meta_tags = \App\MetaTag::where('alias', '=', 'main_page')->first();不返回任何东西。此 MetaTag 历史记录来自该项目的另一位开发人员
  • 您的数据库中有记录吗?
  • @RouhollahMazarei 新删除所有数据库信息确实是个问题。 mysql恢复后-一切正常。谢谢!
【解决方案2】:

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

$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 = "";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-24
    • 2018-02-22
    • 2015-07-05
    • 2016-04-09
    • 2015-10-09
    • 2017-10-29
    • 2017-10-17
    • 2016-02-01
    相关资源
    最近更新 更多