dongzhan9100 2018-04-16 09:05
浏览 66
已采纳

Laravel json哪里不行

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 ?

  • 写回答

1条回答 默认 最新

  • drux41001 2018-04-16 10:32
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀