[英]Non object in laravel
I am working on my laravel project but i am having some issues with it and getting the error "Trying to get property 'game_id' of non-object"我正在处理我的 laravel 项目,但我遇到了一些问题并收到错误“尝试获取非对象的属性 'game_id'”
I am new to laravel and i kinda need some help and or advice thanks.我是 laravel 的新手,我需要一些帮助和/或建议,谢谢。
Code:代码:
namespace App\Http\Controllers;
use App\Config;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Redis;
use Auth;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected $user;
protected $redis;
protected $config;
public function __construct()
{
$this->redis = Redis::connection();
$this->config = Config::find(1);
$settings = (object) [
'appId' => $this->config->game_id,
'domain' => $this->config->domain,
'sitename' => $this->config->sitename,
'title' => $this->config->title,
'description' => $this->config->description,
'keywords' => $this->config->keywords,
'vk_group' => $this->config->vk_group,
'support_email' => $this->config->support_email,
'site_name_protocol' => $this->config->site_name_protocol,
'free' => $this->config->bonus_free
];
view()->share('settings', $settings);
$this->middleware(function ($request, $next) {
$this->user = Auth::user();
My app\config:我的应用\配置:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Config extends Model
{
protected $fillable = [
'domain', 'sitename', 'title', 'description', 'keywords',
'game_id', 'bot_steamid', 'bot_key', 'bot_username', 'bot_password', 'bot_shared_secret', 'bot_identity_secret',
'vk_group', 'support_email', 'site_name_protocol',
'payment',
'freekassa_id', 'freekassa_secret1', 'freekassa_secret2',
'unitpay_public', 'unitpay_secret',
'bonus_free'
];
}
Screenshot: screenshot截图:截图
you need to check whether $this->config is null or not.您需要检查 $this->config 是否为 null。
$this->config = Config::find(1);
if($this->config){
$settings = (object) [
'appId' => $this->config->game_id,
'domain' => $this->config->domain,
'sitename' => $this->config->sitename,
'title' => $this->config->title,
'description' => $this->config->description,
'keywords' => $this->config->keywords,
'vk_group' => $this->config->vk_group,
'support_email' => $this->config->support_email,
'site_name_protocol' => $this->config->site_name_protocol,
'free' => $this->config->bonus_free
];
}
else u need to use findOrFail否则你需要使用 findOrFail
$this->config = Config::findOrFail(1);
it will return 404 if config is not find如果找不到配置,它将返回 404
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.