Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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
带有“LIKE”语句的雄辩查询在Laravel6中不起作用_Laravel_Eloquent_Eager Loading_Laravel Query Builder - Fatal编程技术网

带有“LIKE”语句的雄辩查询在Laravel6中不起作用

带有“LIKE”语句的雄辩查询在Laravel6中不起作用,laravel,eloquent,eager-loading,laravel-query-builder,Laravel,Eloquent,Eager Loading,Laravel Query Builder,我正在尝试制作一个用户搜索引擎,它使用一个字符串将其与集合中每个用户的用户名进行比较,并返回那些将该字符串作为其用户名子字符串的搜索引擎,在我的Laravel项目中,我有一个与其自身相关的用户模型,这是一个与pivot表的多对多关系,这些表是通过迁移生成的,下面显示了两种迁移的up方法 创建用户表迁移中的up方法 创建\跟随\表迁移中的up方法 现在,关系在用户模型中定义如下 用户模型 最后,在我的UserController中有以下方法 用户控制器 它与api.php路由文件中定义的以下路由相

我正在尝试制作一个用户搜索引擎,它使用一个字符串将其与集合中每个用户的用户名进行比较,并返回那些将该字符串作为其用户名子字符串的搜索引擎,在我的Laravel项目中,我有一个与其自身相关的用户模型,这是一个与pivot表的多对多关系,这些表是通过迁移生成的,下面显示了两种迁移的up方法

创建用户表迁移中的up方法

创建\跟随\表迁移中的up方法

现在,关系在用户模型中定义如下

用户模型

最后,在我的UserController中有以下方法

用户控制器

它与api.php路由文件中定义的以下路由相关

Route::group(["namespace" => "API"], function(){
    Route::get("search_followed/{username}", "UserController@searchFollowed");
}
所有这些都不能正常工作,因为searchFollowered方法返回所有通过lazy-eager加载加载的跟踪用户,而不管方法参数字符串如何。如果我取消对this方法中的注释行的注释,我将获得异常SQLSTATE[23000]:完整性约束冲突:字段列表中的1052列“id”不明确SQL:选择“id”、“usename”、“name”、“lastname”、“follower\u followered”.“follower\u id”作为“pivot\u follower\u id”,'follower\u followered`.'followered\u id`作为'users'内部联接'follower\u followerd`中的'pivot\u followerd'id`.'id`='follower\u followered`.'follower\u id`其中'follower\u followered`.'followered\u id`在1中为'follower\u id',在'username'中为%%。我希望我的意图是明确的

我试过了,但没用

有人能帮我吗

提前谢谢

$user->load(["followed" => function($query) use ($username) { $query->where('username', 'LIKE', "%{$username}%"); } ]);

希望它能帮助您轻松使用

->其中的'username','LIKE',%{$username}%像这样使用我已经做过了。。。我把。dd$query的输出是什么$用户->加载[跟随=>函数$query使用$username{$query->where'username','LIKE',%{$username}%;}];=>像这样使用way@AnkitaDobariya哇!那工作,谢谢你!我不需要%{$username}%中的卷发。
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",
        "password",
        "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_followed", "follower_id", "followed_id");
    }
}
public function searchFollowed($username){
    
    $user = Auth::user();
    
    $user->load([
        "followed" => function($query){
            global $username;
            $query
                // ->select(["id", "usename", "name", "lastname"])
                ->where("username", "like", "%$username%");
         }
     ]);
     
    return response()->json($user->followed);
}
Route::group(["namespace" => "API"], function(){
    Route::get("search_followed/{username}", "UserController@searchFollowed");
}
$user->load(["followed" => function($query) use ($username) { $query->where('username', 'LIKE', "%{$username}%"); } ]);