dsiv4041 2017-11-29 08:27
浏览 511
已采纳

更新Laravel中destroy方法中的值

I make a clone system in Laravel. I want to clone activities.

When I click on "Clone", my line is cloned and receives as the value in the column "parent_id" the ID of the original.

The original gets a value of 1 in the "hasClone" column.

But when I want to delete a clone, in my destroy method, I try to set hasClone (the original entry) to NULL before deleting the clone.

Here is my code :

  public function destroyChanges(Activity $activity)
  {

    $parentActivity = Activity::findOrFail($activity->parent_id)->first();
    $parentActivity->hasClone = NULL;
    $parentActivity->save();

    $activity->delete();

    return redirect()->to('/admin/activity/');
  }

Here is my route :

Route::delete('activity/destroyChanges/{id}', ['as' => 'cancel.activity', 'uses' => 'ActivityCrudController@destroyChanges']);

Here is my button :

{{ Form::open([ 'method'  => 'DELETE', 'route' => [ 'cancel.activity', $entry->getKey() ] ])  }}
    <button class="btn btn-default"><i class="fa fa-ban"></i> {{ trans('backpack::crud.cancel') }}</button>
{{ Form::close() }}

The entry of the clone is deleted correctly. But he does not update the original entry. How to do ? thank you very much

EDIT : I also tried that :

 public function destroyChanges($id)
  {
    $activity = Activity::findOrFail($id);

    $parentActivity = Activity::where('id', '=', $activity->parent_id)->first();
    $parentActivity->hasClone = NULL;
    $parentActivity->save();

    $activity->delete();

    return redirect()->to('/admin/activity/');
  }

Even if I put a die (); in the destroy method, he ignores it. Why does not it take it into account?

  • 写回答

1条回答 默认 最新

  • dtr84664 2017-11-29 08:52
    关注

    You need to define parent method in your model and access the parent like the best answer in the following toppic:

    How to implement a self referencing (parent_id) model in Eloquent Orm

    when you access to that can change hasClone field and save that before the update.

    Like this:

     $parentActivity = $activity->parent()->first();
     $parentActivity->hasClone = null;
     $parentActivity->save();
    

    Ensure to have something like this in your activity model:

    public function parent()
    {
        return $this->belongsTo('Activity', 'parent_id');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测