无法通过中间件过滤请求

couldn't filter request through middleware

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

错误是"Trying to get property 'friends_id' of non-object "

这是我的 "Friends" 中间件:-

<?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');

    }
}

这是我的朋友Table:-

 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');

-谢谢

试试这个

您的查询将是错误的使用 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);
    }
}

解决方法是:-

好友中间件:-

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);
    }
}