【问题标题】:how can i change redis to mysql in controller.php我如何在controller.php中将redis更改为mysql
【发布时间】:2017-10-23 07:24:28
【问题描述】:

如何将其更改为不使用 redis。在 .env 中,我没有将 redis 作为 DB_CONNECTION。我知道我如何才能删除这个 redis,因为我无法启动我的网站...非常感谢您的帮助和任何答案。

这是我的controller.php 31-33 中的错误在哪里

<?php

namespace App\Http\Controllers;

use Auth;
use LRedis;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;

abstract class Controller extends BaseController
{
    use DispatchesJobs, ValidatesRequests;

    public $user;
    public $redis;
    public $title;

    public function __construct()
    {
        $this->setTitle('Title not stated');
        if(Auth::check())
        {
            $this->user = Auth::user();
            view()->share('u', $this->user);
        }
        $this->redis = LRedis::connection();
        view()->share('steam_status', $this->getSteamStatus());
    }

    public function  __destruct()
    {
        $this->redis->disconnect();
    }

    public function setTitle($title)
    {
        $this->title = $title;
        view()->share('title', $this->title);
    }

    public function getSteamStatus()
    {
        $inventoryStatus = $this->redis->get('steam.inventory.status');
        $communityStatus = $this->redis->get('steam.community.status');

        if($inventoryStatus == 'normal' && $communityStatus == 'normal') return 'good';
        if($inventoryStatus == 'normal' && $communityStatus == 'delayed') return 'normal';
        if($inventoryStatus == 'critical' || $communityStatus == 'critical') return 'bad';
        return 'good';
    }

}

这是我的 .env

APP_NAME=Laravel
APP_ENV=local
APP_KEY=l2ggh1imVxJ7Oj6BotPUWinAIDgJfBYw
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=csgo
DB_USERNAME=root
DB_PASSWORD=secret

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=localhost
REDIS_PASSWORD=null
REDIS_PORT=4563

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

ppssss 帮帮我!

【问题讨论】:

  • 你用的是mysql数据库吧?
  • 正是..我在这个网站的mysql中的数据库被称为“csgo”
  • 你访问的是什么表
  • 当我启动服务器时,我在线程中添加的 controller.php 出现错误。
  • 你应该删除与redis连接和断开相关的行,并使用两个Eloquent模型从mysql中读取库存和社区状态。

标签: php mysql laravel redis mariadb


【解决方案1】:

您可以像这样访问您的数据库表
首先,您需要在文件顶部添加此代码

use Illuminate\Support\Facades\DB;

然后

$steamDetail= DB::table('steam')->get();//This will return all the data from steam table
$steamStatusArray=DB::table('steam')->pluck('status');//This will return only the status column from steam table

Reference- Laravel Query Builder
注意:你必须删除所有与LRedis相关的代码

如果您不知道从哪里开始,试试这个 tutorial。它可能会给你一些想法。

【讨论】:

  • 我照你说的做了,但又出错了……你能把我所有的controlers.php改成mysql吗?我认为我做错了什么;/
  • 请帮帮我..我删除了所有 LRedis 但仍然不起作用。
  • @danieldaniellllllo 出现错误是因为不能直接访问数据,会以数组的形式返回,需要循环获取数据。你浏览过文档吗?删除与 LRedis 相关的代码也.....显示你得到了什么错误
  • @danieldaniellllllo 删除此行use Illuminate\Routing\Controller as BaseController;
  • 我在回答主题中回答了你。不评论。
猜你喜欢
  • 2017-01-31
  • 2016-09-10
  • 2021-04-14
  • 2010-11-26
  • 2015-10-27
  • 2011-09-30
  • 1970-01-01
相关资源
最近更新 更多