Php Laravel when updating data new record added instead

Young L. :

Hi i have issue when i'm trying update data in my table instead i add new record. My update method is:

 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');
        }
    }

My service update:

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

In table there will be always one Company so I need to always update record with id 1. But when I try update as i show you on picture above with id=1 i will get message duplicate primary key. Where should be problem please?

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 => "*"
  ]
}

My update route:

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

Problem was in route: Bad one:

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

Good one:

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

Just changed id with company

THX Md Juyel Rana for helping me to solve this problem

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=342434&siteId=1