JSON Search in laravel eloquent

后端 未结 2 1214
遇见更好的自我
遇见更好的自我 2021-02-05 19:11

I am storing destinations field as json type in mysql

example column value [\"Goa\", \"Moonar\", \"Kochi\"]

I

相关标签:
2条回答
  • 2021-02-05 19:50

    In Laravel 5.6 and higher you can use whereJsonContains method:

    Package::whereJsonContains('destinations',["Goa"])->get();
    

    But not all DB support it: https://laravel.com/docs/5.6/queries#json-where-clauses

    0 讨论(0)
  • 2021-02-05 19:55

    Probably forced to use a partially raw query like:

    use DB; (top of file definition)

    DB::table('packages')->whereRaw('json_contains(destinations, \'["Goa"]\')')->get();

    And if you have a model:

    Package::whereRaw('json_contains(destinations, \'["' . $keyword . '"]\')')->get();

    assuming your query above works in SQL.

    0 讨论(0)
提交回复
热议问题