2

I'm using laravel spatie in my app. I have a problem. When an user updates his list of image. Admin must to be approve a list of image which an user just update. If user add image, i just add more. But when an user change an image he had, how to do it? I can't change collection_name because if i change, this image is not appear in user profile. Do you have any ideal for me? I have an ideal: i add an in custom_properties column with value {'change' => 'false'}. When user change this image, i will change custom_properties to {'change' => 'true'}. And when admin approve the change, i must update custom_properties to {'change' => 'false'}. But i can't access in value of custom_properties column. (As you know, laravel spatie give us an media table , it has a column custom_properties have json data in it). So help me access in to this column and update data in it.

So sorry because my english is not good.

    $mediaOld = Media::where(['model_id' => $doctor_id, 'collection_name' => 'doctor_achieved'])->get();
    if (isset($mediaOld[$index])) {
        $mediaOld[$index]->update(['custom_properties->change' => 'true']);
        $mediaOld[$index]->save(); // Its not working 
        return $mediaOld[$index]->custom_properties; 
    }

This is data response of this record This is data response of this record

apokryfos
  • 38,771
  • 9
  • 70
  • 114
Ngoc Tran
  • 69
  • 2
  • 12

1 Answers1

5

this might help:

$mediaItem = Media::find($id);

$mediaItem->setCustomProperty('name', 'value'); // adds a new custom propery or updates an existing one
$mediaItem->forgetCustomProperty('name'); // removes a custom propery

$mediaItem->save();

https://docs.spatie.be/laravel-medialibrary/v7/advanced-usage/using-custom-properties/

Patrik
  • 51
  • 1
  • 3