php - MainController.php 中的 PHP / LARAVEL 5 ErrorException
问题描述
全部。做了一些新步骤后我遇到了问题。我是 Rails 开发人员,但现在我必须在 php/laravel 项目中做一些支持。在项目中制作了一些 UI + 后端(在项目 + 管理员中添加 OpenGraph) - 我做了一个命令 - php artisan migrate:fresh。现在我有这个代码错误。
(1/1) ErrorException Trying to get property 'title' of non-object
在 MainController.php 第 78 行
元标记.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'
));
}
我希望能解决这个小问题。谢谢!
解决方案
检查此行是否返回任何内容:
$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 = "";
}
推荐阅读
- java - Rr-从指定步骤运行一个进程
- excel - VBA自动过滤器从一张纸到另一张
- html - 嵌入式 span 元素中的 FlexBox 对齐问题
- python - 在 python np.diff() 中意味着 out[n] = a[n+1] - a[n] 但我想要 out[n]=a[n]-a[n-1]?
- python - 使用 pandas 对同一列中的数值差异求和
- android - 奥利奥更新后,如何以编程方式检测设备何时唤醒或解锁?
- javascript - JQuery Datatables 需要时间在 Internet Explorer 上加载,但在 Chrome 上加载正常
- node.js - 在处理第一个 API 时,Express 没有为第二个 API 提供服务
- api - 验证/清理和 OOP
- apache-spark - Log4j 在火花流的 foreachRDD 方法中不起作用