【问题标题】:couldn't filter request through middleware无法通过中间件过滤请求
【发布时间】:2020-05-27 02:27:55
【问题描述】:

我正在尝试使用中间件过滤我的 http 请求。
我想检查我从 http 请求中获得的 "friends_id" 和我通过 Auth 传递的 "my_id" 是否已经不存在于同一行中, 如果他们这样做,我想重定向到主页,如果他们不这样做,我想执行正常请求,最终将插入我正在检查的数据

错误是“尝试获取非对象的属性'friends_id'”

这是我的“朋友”中间件:-

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class Friends
{

    public function handle($request, Closure $next)
    {  $auth = Auth()->user()->id;
        $dost = $request->friends_id;

        $ip = DB::table('friends')
   ->where('my_id', '=', $auth)
        ->where('friends_id', '=', $dost)
        ->get();

   if($ip->friends_id != $dost){
        return $next($request);
    }


return redirect('/home');

    }
}

这是我的好友表:-

 public function up()
    {
        Schema::create('friends', function (Blueprint $table) {
            $table->id();
        $table->string('my_id');
        $table->string('friends_id');
        $table->string('name');     
            $table->timestamps();
        });
    }

这是我的路线:-

Route::post('/f', 'FriendsController@store')->middleware('friends');

-谢谢你

【问题讨论】:

    标签: database laravel laravel-middleware


    【解决方案1】:

    试试这个

    您的查询将错误地使用where 而不是whereColumn,并且当您获得第一条记录时仅使用first() 而不是get()

     use DB;
    
     $ip = DB::table('friends')
           ->select('my_id', 'friends_id')
           ->where([['my_id', '=', '$auth'],['friends_id', '=', '$dost']])
           ->first();
    

    编辑

    public function handle($request, Closure $next)
    {  
        $auth = Auth()->user()->id;
        $dost = $request->friends_id;
    
        $ip = DB::table('friends')
           ->select('my_id', 'friends_id')
           ->where([['my_id', '=', '$auth'],['friends_id', '=', '$dost']])
           ->first();
    
        if($ip->friends_id == $dost){
            return redirect('/home');
        } else {
            return $next($request);
        }
    }
    

    【讨论】:

    • 如果您解释一下为什么上述方法会起作用,答案可能会更有帮助。
    • 好吧,你的查询会出错,使用where而不是whereColumn,当你获得第一条记录时,只使用first()而不是get(),就是这样.....
    • @vikas katariya 我收到此错误 “尝试获取非对象的属性 'friends_id'”
    • @vikas katariya "尝试访问 null 类型值的数组偏移量"这是当前错误
    • ` if($ip->friends_id == $dost){ return redirect('/home'); } 其他 { 返回 $next($request); }`
    【解决方案2】:

    解决办法是:-

    好友中间件:-

    public function handle($request, Closure $next)
        {
            if (auth()->check()) {
                $ip = DB::table('friends')->where('my_id', '=', auth()->id())->where('friends_id', '=', $request->friends_id)->first();
    
                if ($ip && $ip->friends_id == $request->friends_id) {
                    return redirect('/home');
                }
            }
    
            return $next($request);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 2015-09-16
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      相关资源
      最近更新 更多