ysfkaya
ysfkaya

Reputation: 398

Laravel json where not working

I try where query in json structure. But always returns null. Am I doing wrong ?

My scope method in Model :

public function scopeWhereDeveloper(Builder $scope)
{
  return $scope->where('parameters->developer','yes');
}

My static method in Model :

public static function getDeveloper($columns = ['*'])
{
    return static::whereDeveloper()->first($columns);
}

Migration :

public function up()
    {
        Schema::create('admins', function(Blueprint $table) {
            $table->increments('id');
            $table->string('full_name');
            $table->string('user_name')->unique();
            $table->string('email')->unique();
            $table->string('password');
            $table->json('parameters')->nullable();
            $table->timestamps();
            $table->rememberToken();
        });
    }

Test method :

/** @test */
public function it_can_get_admin_developer_by_parameter()
{
    $admin = create(Admin::class, ['parameters' => ['developer' => 'yes']]);

    $developer = Admin::getDeveloper();

    dd($developer); // return null

    $this->assertEquals($admin->email,$developer->email);

}

So what I do ?


So interesting.When I using this query : return->where('parametersdeveloper','yes'); always returns null in. But when I test in HomeController with ::getDeveloper()` then I can get the user. Why did this happen ?

Upvotes: 1

Views: 774

Answers (1)

lucidlogic
lucidlogic

Reputation: 1944

Don't name a scope with a where prefix. Eloquent will assume that you're trying to use shorthand for ->where('developer').

Rename your scope to something like scopeDeveloper

Upvotes: 3

Related Questions