Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Cookie::get()返回null_Php_Laravel_Cookies_Laravel 5 - Fatal编程技术网

Php Cookie::get()返回null

Php Cookie::get()返回null,php,laravel,cookies,laravel-5,Php,Laravel,Cookies,Laravel 5,我在获取Cookie时遇到问题,我在一个中间件“CheckReferral”中定义了一个Cookie,但是当我在一个控制器中调用Cookie时,Cookie返回null,我在浏览器中检查Cookie,并且Cookie在浏览器中是好的,我不知道Cookie有什么问题。。。我搜索了太多,这是我最后的资源,有人能帮我吗 以下是中间件的代码: <?php namespace App\Http\Middleware; use Closure; class CheckReferral {

我在获取Cookie时遇到问题,我在一个中间件“CheckReferral”中定义了一个Cookie,但是当我在一个控制器中调用Cookie时,Cookie返回null,我在浏览器中检查Cookie,并且Cookie在浏览器中是好的,我不知道Cookie有什么问题。。。我搜索了太多,这是我最后的资源,有人能帮我吗

以下是中间件的代码:

<?php

namespace App\Http\Middleware;

use Closure;

class CheckReferral
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->hasCookie('referral')) {
            return $next($request);
        } else {
            if ($request->query('ref')) {
                return redirect($request->fullUrl())->withCookie(cookie()->forever('referral', $request->query('ref')));
            }
        }

        return $next($request);
    }
以下是浏览器中存储的cookie:

这是数据库。。。所引用的字段存储为null,但应存储cookie的值:


非常感谢,我希望解决问题,并知道原因

确保您已经在
app\Http\Kernel.php
中定义了该中间件。特别是在
'web'
数组中

web.php
(路由)文件中导入中间件

…最后为您的路线添加了中间件,如下所示:

Route::web('/', ['middleware' => CheckReferral']
如果您已经这样做了,那么请确保已将
reference\u by
列添加到
用户
模型的
$filleble[]
数组中

编辑。 这将有助于:

\Request::cookie('referral');

我已经用vanilla PHP解决了这个问题,它是一个全局变量
$\u COOKIE

protected function create(array $data)
{
    // $referred_by = User::where( 'affiliate_id', Cookie::get( 'referral' ) )->first();
    // $referred_user = Cookie::get( 'referral' );

    return User::create([
        'name'         => $data['name'],
        'email'        => $data['email'],
        'password'     => bcrypt($data['password']),
        'affiliate_id' => $this->uniqueRandomString(),
        'referred_by'  => $_COOKIE['referral'],
    ]);
}

其中,
Cookie::get('referral')
$\u Cookie['referral']
不是一个优雅的解决方案,但是工作…

是的,我在内核中定义了中间件,因此Cookie存储在浏览器中。。。而且中间件是全局运行的,不需要在路由中定义。。。我已经输入了
$filleble[]
问题是Cookie::get()返回null,我已经用字段email测试了它,这是不可nulable的,我得到了错误。。。所以,问题是Cookie::get()…如果它实际上是中间件中的值,请尝试转储它<代码>$request->query('ref')@alexcue更新了答案\请求::cookie(“转介”);可以。您可以检查对象是否确实包含任何内容吗:
\Cookie::get()
我已经看到了问题,我已经用
$\u COOKIE进行了测试,并且工作正常,问题在于
COOKIE::get()
protected function create(array $data)
{
    // $referred_by = User::where( 'affiliate_id', Cookie::get( 'referral' ) )->first();
    // $referred_user = Cookie::get( 'referral' );

    return User::create([
        'name'         => $data['name'],
        'email'        => $data['email'],
        'password'     => bcrypt($data['password']),
        'affiliate_id' => $this->uniqueRandomString(),
        'referred_by'  => $_COOKIE['referral'],
    ]);
}