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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示