Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.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 update生成新的表规则,而不是更新_Php_Mysql_Laravel - Fatal编程技术网

Php Laravel update生成新的表规则,而不是更新

Php Laravel update生成新的表规则,而不是更新,php,mysql,laravel,Php,Mysql,Laravel,我有一个公司表和一个属性表,里面有各种各样的值 一个公司有许多属性,一个属性属于一个公司 现在我在属性表中有一个值,带有“account\u nr\u start”(例如,当一个新用户添加到一个公司时,其account\u id开始从1000开始计数) 控制器: public function __construct(Company $company, User $user) { if(Auth::user()->usertype_id == 7) { $t

我有一个公司表和一个属性表,里面有各种各样的值

一个公司有许多属性,一个属性属于一个公司

现在我在属性表中有一个值,带有“account\u nr\u start”(例如,当一个新用户添加到一个公司时,其account\u id开始从1000开始计数)

控制器:

public function __construct(Company $company, User $user)
{
    if(Auth::user()->usertype_id == 7)
    {
        $this->company = $company;
    }
    else
    {
        $this->company_id = Auth::user()->company_id;
        $this->company  = $company->Where(function($query)
        {
            $query->where('id', '=', $this->company_id )
                ->orWhere('parent_id','=', $this->company_id);
        }) ;
    }

    $this->user = $user;

    $this->middleware('auth');
}



public function edit(Company $company, CompaniesController $companies)
{
    $companies = $companies->getCompaniesName(Auth::user()->company_id);

    $attributes = $company->attributes('company')
        ->where('attribute', '=', 'account_nr_start')
        ->get();

    foreach ($attributes as $k => $v) {
        $nr_start[] = $v->value;
    }

    return view('company.edit', ['company' => $company, 'id' => 'edit', 'companies' => $companies, 'nr_start' => $nr_start]);
}



public function update(UpdateCompanyRequest $request, $company, Attribute $attributes)
{
        $company->fill($request->input())->save();

        $attributes->fill($request->only('company_id', 'attribute_nr', 'value'))->save();

        return redirect('company');
}
HTML/Blade:

<div class="form-group {{ $errors->has('_nr_') ? 'has-error' : '' }}">
    {!! HTML::decode (Form::label('account_nr_start', trans('common.account_nr_start').'<span class="asterisk"> *</span>', ['class' => 'form-label col-sm-3 control-label text-capitalize'])) !!}
    <div class="col-sm-6">
        {!! Form::text('value', $nr_start[0], ["class"=>"form-control text-uppercase"]) !!}
        {!! $errors->first('account_nr_start', '<span class="help-block">:message</span>') !!}
    </div>
</div>

{!!HTML::decode(Form::label('account\u nr\u start',trans('common.account\u nr\u start').*',['class'=>'Form label col-sm-3 control label text capitalize'])
{!!Form::text('value',$nr_start[0],“class”=>“Form control text uppercase”])
{!!$errors->first('account\nr\u start',':message')
当我现在更新一家公司时,它将像这里的最后一个输入一样上传:


因此,它创建了一个新规则,同时它需要编辑当前属性规则,而不是创建一个公司id/属性为空的新规则。

如果我理解您的意图,我认为这将解决您的问题。问题是属性模型是模型的新实例,而不是检索所需的模型

在从attributes方法运行fill()之前,请尝试以下操作

$new_attribute = $attributes->where('company_id', '=', $company->id)->where('attribute', '=', 'account_nr_start')->first();
然后运行fill()


谢谢,工作起来很有魅力,first()方法到底是如何工作的?第一个方法返回数据库中的第一个对象,这样您就只收到您需要的一个对象。如果需要访问数据库中的多个对象,请使用get()方法并foreach对象
$new_attribute->fill($request->only('company_id', 'attribute_nr', 'value'))->save();