Laravel请求在Controller中有效,但在Model中无效

时间:2018-07-30 20:09:41

标签: php laravel

我有一个简短的问题,我不明白为什么:

我有一种将数据发布到控制器的表单,在其中我称为模型的函数,在其中更新模型:

控制器:

public function vacancieEdit(EditVacancieRequest $request, Vacancie $vacancie)
    {
        $company = Company::find($vacancie->company_id);
        if ($company->userHasEditAccess()) {
            $vacancie->shortcutedit($request);
            return back();
        }
        return back()->with('message', 'Sie haben keine Rechte für diese Seite!');
    }

在我更新的“空缺”模型中:

public function shortcutedit($request)
    {

        $company = Company::find(clean($request->company));

        if(!$company->userHasEditAccess() || !Branche::where('value', clean($request->branche))->exists()) {
            return false;
        }
        if(clean($request->visible) == 'on') {
            $visible = 1;
        } else {
            $visible = 0;
        }
        if (clean($request->siteapplication) == 'on') {
            $siteapplication = 1;
        } else {
            $siteapplication = 0;
        }
            $this->company_id = clean($request->company);
            $this->expdate = clean($request->expdate);
            $this->siteapplication = clean($request->siteapplication);
            $this->visible = $visible;
            $this->siteapplication = $siteapplication;
            $this->save();
    }

除模型的“可见”变量外,其他所有方面都在变化。

但是,如果我将可见的代码放置在Controller中,则该变量正在更改。为什么?

Controller中的代码正在工作:

    if(clean($request->visible) == 'on') {
        $vacancie->visible = 1;
    } else {
        vacancie->visible = 0;
    }
    $vacancie->save();

如何使它在模型中工作?


编辑:我已经按照答案1的建议将空缺率模型编辑为以下内容,但它也不起作用。

public function shortcutedit($request)
    {

        $company = Company::find(clean($request->company));

        if(!$company->userHasEditAccess() || !Branche::where('value', clean($request->branche))->exists()) {
            return false;
        }
        if(clean($request->visible) == 'on') {
            $this->visible = 1;
        } else {
            $this->visible = 0;
        }
        if (clean($request->siteapplication) == 'on') {
            $siteapplication = 1;
        } else {
            $siteapplication = 0;
        }
            $this->company_id = clean($request->company);
            $this->title = clean($request->title, 'tinymce');
            $this->branche = clean($request->branche);
            $this->text = clean($request->text);
            $this->visible = clean($request->visible);
            $this->startdate = clean($request->startdate);
            $this->expdate = clean($request->expdate);
            $this->siteapplication = clean($request->siteapplication);

            $this->siteapplication = $siteapplication;

            $this->save();
    }

3 个答案:

答案 0 :(得分:2)

看看Reserved column names in Eloquent

不知道它在Controller中如何工作。

模型中有一个import org.springframework.util.StringUtils; public void setFileName(String fileName) { // If the extension does not exist trim the trailing period this.fileName = StringUtils.trimTrailingCharacter(fileName,'.'); } 属性,用于在调用protected $visible方法时隐藏/显示属性。

https://laravel.com/docs/5.6/eloquent-serialization#hiding-attributes-from-json

toArray更改$this->visible会发生什么?

答案 1 :(得分:1)

尝试使用以下代码:

public function shortcutedit($request)
{

    $company = Company::find(clean($request->company));

    if ( !$company->userHasEditAccess() || !Branche::where('value', clean($request->branche))->exists()) {
        return false;
    }

    $this->attributes['company_id'] = clean($request->company);
    $this->attributes['title'] = clean($request->title, 'tinymce');
    $this->attributes['branche'] = clean($request->branche);
    $this->attributes['text'] = clean($request->text);
    $this->attributes['visible'] = (clean($request->input('visible')) == 'on') ? 1 : 0;
    $this->attributes['startdate'] = clean($request->startdate);
    $this->attributes['expdate'] = clean($request->expdate);
    $this->attributes['siteapplication'] = (clean($request->input('siteapplication')) == 'on') ? 1 : 0;

    $this->save();
}

答案 2 :(得分:0)

因此,在您的编辑中,您表示您正在执行此main.js检查:

if()

并将if(clean($request->visible) == 'on') { $this->visible = 1; } else { $this->visible = 0; } 设置为$this->visible1,但是您很快就会在下面用以下命令覆盖它:

0

因此,在此函数结束时,调用$this->visible = clean($request->visible); 时,$this->save();将为$this->visiblenull

删除第二个设置"on",此设置将为您服务。