我正在尝试使用中间件来过滤我的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')
->select('my_id', 'friends_id')
->whereColumn([
['my_id', '=', '$auth'],
['friends_id', '=', '$dost'],
])->get()->first();
if($ip->friends_id == $dost){
return redirect('/home');
}
return $next($request);
}
}
这里是我的朋友,表格:-
public function up()
{
Schema::create('friends', function (Blueprint $table) {
$table->id();
$table->string('my_id');
$table->string('friends_id');
$table->string('name');
$table->timestamps();
});
}
-ThankYou
尝试一下
您的查询将是错误的,请使用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);
}
}