3

we try to update the json data key value.we stored record in database product_name column like

{"ar":"arabic product","en":""}

we try to achieve this both below two method

DB::table('products')
        ->where('id', 1)
        ->update(['productname->en' => 'english product']);

$post = product::find($product_id);
        $post->product_name = [$language => $edittitle];
        $post->save();

we want to update the json en key value.

4
  • what laravel version are you using? @GANESHJAGDHANE? Commented Jan 24, 2019 at 8:20
  • we using laravel 5.7 version Commented Jan 24, 2019 at 8:24
  • What exactly is the column name product_name or productname? Commented Jan 24, 2019 at 8:41
  • column name product_name Commented Jan 24, 2019 at 9:10

2 Answers 2

2

A quick and easy solution to update all the entries in one go:

Product::each(function ($product) {
    $productName = json_decode($product->product_name, true);
    $productName['en'] = 'english product';
    $product->product_name = json_encode($productName);
    $product->save();
});

If you want to update a single product, this should work:

$product = Product::find($product_id);
$productName = json_decode($product->product_name, true);
$productName['en'] = 'english product';
$product->product_name = json_encode($productName);
$product->save();
Sign up to request clarification or add additional context in comments.

4 Comments

$product = product::find($product_id); product::each(function ($product) { $productName = json_decode($product->product_name, true); $productName['en'] = 'english product'; $product->product_name = json_encode($productName); $product->save(); }); like this
I have updated my answer with how to change a single product. Hope this helps!
its shown me error like "json_decode() expects parameter 1 to be string, array given" but when we comment this code in product model ' protected $casts = [ 'product_name' => 'array', ]; ' its works.
after commenting this " protected $casts = [ 'product_name' => 'array' ];" in product model insert is not working while inserting product_name need to mention as array.
1

First thing is to create that field with the right type in the migration file, the keyword we need there is json:

...
$table->json('productname');
...

Then we need to cast that attribute to the right type when retrieving it as Eloquent model by using $casts attribute on the model itself:

...
   /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'productname' => 'array'
    ];
...

Then we can access and save data for this attribute like so:

$myModel = product::find(1);
$myModel->productname['en'] = 'something';
// or
$myModel->productname = ['en' => 'something']; // This will override the whole attribute

For further details you can take a look at: https://laravel.com/docs/5.7/eloquent-mutators#attribute-casting

9 Comments

thank you '$myModel->product_name = ['en' => 'something']' work for me but This will override the whole attribute we just want to update en key value. we also use $myModel->product_name['en'] = 'something'; it's return error message "Indirect modification of overloaded property App\Models\user\product::$product_name has no effect"
this is my model ' protected $casts = [ 'product_name' => 'array' ];'
Please remove __get and __set magic methods from your model in order for it to work properly. This is because Model class that each of your models extends from has those methods implemented :) @GANESHJAGDHANE
@Nikola-remove __get and __set magic methods from your model , sory not getting what exactly do
no when we using '$myModel->productname['en'] = 'something';' it's return error message "Indirect modification of overloaded property App\Models\user\product::$product_name has no effect" we not use any __set() and __get() methods inside product model only write protected $casts = [ 'productname' => 'array' ]; and belongsToMany and hasMany relationship's functions
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.