PHPのLaravel更新データの新しいレコードが代わりに追加したとき

ヤングL.:

私が代わりに私は新しいレコードを追加私のテーブル内の更新データをしようとしているときこんにちは、私は問題を持っています。私の更新方法は、次のとおりです。

 public function update(CompanyUpdateRequest $request, Company $company)
    {
        try{
            $request['id'] = 1;
            $this->companyService->setModel($company)->update($request->all());
            $request->session()->flash('success', 'Nastavenie bolo zmenené!');
            return redirect('admin/company');
        }
        catch(\Exception $e){
            $request->session()->flash('warning', 'Nastavenie sa nepodarilo uložiť!' . $e);
            return redirect('admin/company');
        }
    }

私のサービス更新:

    public function update(array $data, $modelOrId = null): BaseModelService
    {
        if ($modelOrId !== null) {
            if ($modelOrId instanceof Model) {
                $this->setModel($modelOrId);
            } elseif (is_int($modelOrId)) {
                $modelOrId = $this->getRepository()->find($modelOrId);
                $this->setModel($modelOrId);
            }
        }
        $this->getModel()->fill($data)->save();
        //Here is the problem when i try to save
        return $this;
    }

表では、私はいつも、ID 1を持つレコードを更新する。しかし、私はメッセージの重複主キーを取得しますID = 1の上の写真でお見せして、私が更新をしようとする必要があるので、常に一つの会社が存在します。どこに問題くださいすべきですか?

App\Company {#306 ▼
  #table: "company"
  #fillable: array:13 [▼
    0 => "phone_number"
    1 => "name"
    2 => "email"
    3 => "instagram"
    4 => "facebook"
    5 => "youtube"
    6 => "gps"
    7 => "address"
    8 => "psc"
    9 => "city"
    10 => "house_number"
    11 => "name_executive_manager"
    12 => "surname_executive_manager"
  ]
  #connection: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▼
    0 => "*"
  ]
}

私の更新ルート:

Route::put('/admin/company/{id}', 'Admin\CompanyController@update')->name('admin_company_update');
ヤングL.:

問題がルートにあった:悪い1:

Route::put('/admin/company/{id}', 'Admin\CompanyController@update')->name('admin_company_update');

良いもの:

Route::put('/admin/company/{company}', 'Admin\CompanyController@update')->name('admin_company_update');

ただ、会社とIDを変更

この問題を解決するために私を助けるためのTHXメリーJuyelラナ

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=342973&siteId=1
おすすめ