有许多通过更新/与枢轴laravel 5.5

时间:2018-05-10 07:13:33

标签: php eloquent laravel-5.5

我有几个表(所有表都带有created_at,updated_at,deleted_at):

  • sector
  • lang_sector
  • valuechains
  • lang_valuechain
  • lang_segment
  • keyneeds
  • keyneed_lang

表按此顺序链接: 部门有很多价值链 价值链有很多细分 细分有许多关键需求

这是我的模特:

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Sector extends Model
{
    use SoftDeletes;

    protected $table = "sectors";
    protected $fillable = ['admin_id'];
    protected $dates = [ 'created_at', 'updated_at', 'deleted_at' ];

    public function langs() {
        return $this->belongsToMany('App\Lang')
            ->withPivot('sectname', 'sectshortname', 'segname_slug',
                'sectdescription', 'sectshortdescription'
            )
            ->withTimestamps();
    }
    public function admin()
    {
        return $this->belongsTo('App\Admin');
    }

    public function valuechains()
    {
        return $this->hasMany('App\Valuechain');
    }
    public function segments()
    {
        return $this->hasManyThrough('App\Segment', 'App\Valuechain');
    }
    public function keyneeds()
    {
        return $this->hasManyThrough('App\Keyneed', 'App\Segment', 'App\Valuechain');
    } 
}

在我的销毁控制器中:

public function destroy($id)
{
    $sector = Sector::findOrFail($id);

    $sector_ids = $sector->langs()->allRelatedIds();
    foreach ($sector_ids as $id){
        $sector->langs()->updateExistingPivot($id, ['lang_sector.deleted_at' => Carbon::now()]);
    }
    $sector->valuechains()->update( [ 'valuechains.deleted_at' => Carbon::now() ] );
    $sector->segments()->update( [ 'segments.deleted_at' => Carbon::now() ] );
    $sector->keyneeds()->update( [ 'keyneeds.deleted_at' => Carbon::now() ] );
    Sector::where('id', $id)->delete();

    return redirect()->route('sectors.index')->with('success', 'Sector deleted');
}

我的问题是它没有更新下面的表:segment和keyneeds(有created_at,updated_at和deleted_at字段)和它们的数据透视表......而且我有一条错误消息:

SQLSTATE [23000]:完整性约束违规:1052字段:' updated_at'字段列表中的segments valuechains = valuechains id设置segments valuechain_id segments }。deleted_at = 2018-05-10 06:54:54,updated_at = 2018-05-10 06:54:54其中valuechainssector_id = 2)

它补充说:updated_at

我成功更新了

  • sector table
  • valuechains表
  • lang_valuechain pivot

我的问题是更新

  • 使用以下分段表格:$ sector-> segments()
  • keyneeds表使用:$ sector-> keyneeds()

他们的数据透视表......我阅读了文档,但它没有帮助。 我使用update()方法,因为$ sector-> segments() - > delete()正试图进行硬删除......

0 个答案:

没有答案