问题描述
我正在尝试创建一个用户搜索引擎,该搜索引擎使用字符串将其与集合中每个用户的用户名进行比较,并返回以该字符串作为其用户名子字符串的用户,而我拥有一个{ 1}}模型在我的User
项目中与自身相关,这是与Laravel
数据透视表的many to many
关系,此表是通过迁移以及两者的follower_followed
方法生成的迁移显示如下。
up
up method inside create_users_table migration.
public function up(){
Schema::create('users',function (Blueprint $table) {
$table->bigIncrements("id");
$table->string("username",15);
$table->string("name",35);
$table->string("lastname",35);
$table->string("country",35)->nullable();
$table->string("city",35)->nullable();
$table->string("phone_number",35)->nullable();
$table->string("email",35)->unique();
$table->string('biography',120)->nullable();
$table->string("password",255);
$table->bigInteger("role_id")->unsigned()->default(1);
$table->timestamp("email_verified_at")->nullable();
$table->rememberToken();
$table->softDeletes();
$table->timestamps();
});
}
up method inside create_follower_followed_table migration.
现在,这种关系在public function up(){
Schema::create('follower_followed',function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger("follower_id")->unsigned();
$table->bigInteger("followed_id")->unsigned();
$table->foreign("follower_id")->references("id")->on("users")->onDelete("cascade");
$table->foreign("followed_id")->references("id")->on("users")->onDelete("cascade");
$table->timestamps();
});
}
模型中定义。
User
User model.
最后,我的namespace App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements JWTSubject{
use Notifiable;
protected $fillable = [
"role_id","username","name","lastname","country","city","phone_number","email","password","biography"
];
protected $hidden = [
"role_id","remember_token","email_verified_at","deleted_at","created_at","updated_at"
];
protected $casts = [
"email_verified_at" => "datetime",];
protected $appends = [
"following"
];
protected $with = ["profile_picture"];
public function getFollowingAttribute(){
return DB::table("follower_followed")
->where("follower_id",Auth::user()->id)
->where("followed_id",$this->attributes["id"])
->exists();
}
public function getJWTIdentifier(){
return $this->getKey();
}
public function getJWTCustomClaims(){
return [];
}
public function getRouteKeyName(){
return "username";
}
public function role(){
return $this->belongsTo(Role::class);
}
public function profile_picture(){
return $this->hasOne(UserProfilePicture::class);
}
public function followers(){
return $this->belongsToMany(User::class,"follower_followed","followed_id","follower_id");
}
public function followed(){
return $this->belongsToMany(User::class,"follower_id","followed_id");
}
}
中使用以下方法。
UserController
UserController
它与public function searchFollowed($username){
$user = Auth::user();
$user->load([
"followed" => function($query){
global $username;
$query
// ->select(["id","usename","lastname"])
->where("username","like","%$username%");
}
]);
return response()->json($user->followed);
}
路由文件中定义的以下路由有关。
api.PHP
所有这一切均无法正常工作,因为如果我取消注释此方法内的注释行,则无论方法参数字符串如何,Route::group(["namespace" => "API"],function(){
Route::get("search_followed/{username}","UserController@searchFollowed");
}
方法都会返回通过searchFollowed
加载的所有followed
用户获取异常lazy eager loading
。我希望我的意图很清楚。
我尝试了this,但没有用。
有人可以帮我吗?
谢谢。
解决方法
$user->load(["followed" => function($query) use ($username) { $query->where('username','LIKE',"%{$username}%"); } ]);
充分希望它将对您有帮助