【问题标题】:How to update/edit custom_properties in laravel spatie?如何在 laravel spatie 中更新/编辑 custom_properties?
【发布时间】:2019-01-22 04:02:45
【问题描述】:

我在我的应用程序中使用 laravel spatie。我有个问题。 当用户更新他的图像列表时。管理员必须批准用户刚刚更新的图像列表。如果用户添加图像,我只是添加更多。但是,当用户更改他拥有的图像时,该怎么做呢?我无法更改 collection_name,因为如果我更改,此图像不会出现在用户配置文件中。你对我有什么理想吗? 我有一个理想:我在 custom_properties 列中添加了一个值为{'change' => 'false'} 的值。当用户更改此图像时,我会将 custom_properties 更改为 {'change' => 'true'}。当管理员批准更改时,我必须将 custom_properties 更新为{'change' => 'false'}。但我无法访问 custom_properties 列的值。 (如你所知,laravel spatie 给了我们一个媒体表,它有一个 custom_properties 列,里面有 json 数据)。因此,请帮助我访问此列并更新其中的数据。

很抱歉,因为我的英语不好。

    $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; 
    }

这是这条记录的数据响应

【问题讨论】:

    标签: php laravel laravel-medialibrary


    【解决方案1】:

    这可能会有所帮助:

    $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/

    【讨论】:

      猜你喜欢
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2018-11-06
      • 2019-03-10
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      相关资源
      最近更新 更多