Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel在更新数据时改为添加新记录_Php_Laravel - Fatal编程技术网

Php Laravel在更新数据时改为添加新记录

Php Laravel在更新数据时改为添加新记录,php,laravel,Php,Laravel,您好,当我尝试更新表中的数据时,我遇到了问题,而不是添加新记录。 我的更新方法是: public function update(CompanyUpdateRequest $request, Company $company) { try{ $request['id'] = 1; $this->companyService->setModel($company)->update($request->

您好,当我尝试更新表中的数据时,我遇到了问题,而不是添加新记录。 我的更新方法是:

 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尝试更新时,我会得到消息duplicate primary key。请问问题应该在哪里

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');
问题是: 坏消息:

好的:

Route::put('/admin/company/{company}', 'Admin\CompanyController@update')->name('admin_company_update');
刚和公司换了身份证

THX Md Juyel Rana帮助我解决了这个问题

问题正在进行中: 坏消息:

好的:

Route::put('/admin/company/{company}', 'Admin\CompanyController@update')->name('admin_company_update');
刚和公司换了身份证


谢谢你帮我解决这个问题

试试这个$request['id']=$company->id$公司->id:1;不,那没用。在那之后仍然保持着新的记录。但是thxshow我是dd($公司);我知道了,你的函数就像公共函数更新一样(CompanyUpdateRequest$request,$id){$company=company::find($id);},也不需要添加这个$request['id']=1;尝试此$request['id']=$company->id$公司->id:1;不,那没用。在那之后仍然保持着新的记录。但是thxshow我是dd($公司);我知道了,你的函数就像公共函数更新一样(CompanyUpdateRequest$request,$id){$company=company::find($id);},也不需要添加这个$request['id']=1;很高兴见到你