1

I'm using php 7 and MySQL 5.7. attributes field is defined as json column in my table.

I defined the casts in my model file.

protected $casts = ['attributes' => 'array'];

I don't have any problem adding data.

Transaction::create([

            'name'          => $this->name,
            'subject_id'    => $this->id,
            'subject_type'  => get_class($this),
            'attributes'    => ['status' => 0, 'type' => 'owner'],
            'user_id'       => $owner->id

        ]);

But I can not update the json data. I tried these methods:

$transaction = $this->transaction->where('content_key', $this->request->key)->first(); $transaction->update(['attributes->status' => 1]);

or

$transaction->attributes['status'] = 1; $transaction->save();

None work.

These methods work:

$transaction->update(['attributes'=> ['status' => 1, 'type' => 'owner'] ]);

(I need to update all data in this method)

or

Db::table('transactions')->where('content_key', $this->request->key)->update(['attributes->status' => '1'])

How can I do this with Eloquent?

Gökhan YILDIZ
  • 131
  • 3
  • 16
  • Did you tried $transaction->status = ['status' => 0, 'type' => 'owner']; $transaction->save(); ? – Muthu17 Mar 07 '17 at 10:13
  • I'm having exactly the same problem. I'm using a PostgreSQL database, but the principle is the same. It's really quite frustrating why it doesn't work. Did you ever find a solution? – cartbeforehorse Nov 07 '17 at 21:47

1 Answers1

2

So having experienced the same problem as you, I took a day to figure out what was going on under the hood. Try this on for size:

$transaction->setAttribute ('attributes->status', 1);

If you ask me, Laravel's documentation is seriously lacking in detail on the subject! (But then I suppose that's just a standard feature of Laravel).

cartbeforehorse
  • 3,045
  • 1
  • 34
  • 49
  • https://github.com/laravel/framework/issues/15433#issuecomment-247648472. It was already discussed. Besides, instead of just dumping on Laravel why don't you share what you found? – impeto Oct 02 '22 at 19:48
  • @impeto I did share what I found: use of function `setAttribute()`. Had I found supporting docs I would have included a link, but the fact that I found none supports my point that documentation is lacking. Your link is related, but doesn't directly answer the original question. If you have a better answer, you're free to add your own rather than dumping on other people's. Also, don't get me wrong - I love Laravel. Its docs are an okay intro to the framework and the v9 docs have improved since 2017. However, developers shouldn't have to revert to SO to find out how to update a JSON column. – cartbeforehorse Oct 17 '22 at 10:55
  • If you look at the link I gave you, the discussion predates the initial question and it's clear why they chose to keep things this way. Not every scenario can be covered in docs and a dev's job is not limited to just reading the docs. – impeto Oct 27 '22 at 10:34
  • @impeto I know the discussion predates this thread. What's your point? What part of my comments made you think I didn't realise that? Also, if you bothered looking at your own link, you'd notice that it doesn't mention `setAttribute()`. – cartbeforehorse Oct 28 '22 at 14:10
  • If you look at Mohamed's comment it links to this https://github.com/laravel/framework/pull/15464#issuecomment-371330795 It mentions your idea. Consider I don't have a point. But before you say something is "lacking" make sure you have looked everywhere. Anyway... – impeto Oct 28 '22 at 21:08
  • @impeto Lol. 1: I never said it wasn't "anywhere". I still maintain that it isn't sufficiently documented if you have to drill into GitHub comments to find obscure examples that are only tangentially related to what your (the developer's) actual problem is (and anyway, the comment claims it doesn't work for their requirements anyway). 2: You may have noticed that the internet is a big place. Looking "everywhere" is rather a big task. – cartbeforehorse Oct 30 '22 at 02:41