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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug