调用未定义的方法Illuminate \ Database \ Query \ Builder :: save()错误如何修复?

时间:2016-06-07 08:55:51

标签: php laravel

public function AddArtWork(Request $request){
    $art = new Artwork;
    $art->name = $request->name;
    $art->date = $request->date;        

    if (!is_null($request->paintStyle)) {
        # code...
        $paint = new Paint;
        $paint->style = $request->paintStyle;
        $art->paint()->save($paint);
    }
    else if (!is_null($request->calligStyle)) {
        # code...
        $callig = new Calligraphy;
        $callig->style = $request->calligStyle;
        $art->calligraphie()->save($callig);
    }
    else if (!is_null($requst->substance)) {
        # code...
        $sclup = new Sculpture;
        $sculp->substance = $request->substance;
        $art->sculpture()->save($sculp);
    }

    if (!is_null($request->recievedDate)) {
        # code...
        $loan = new Loan;
        $loan->date = $request->loanDate;
        $art->loan()->save($loan);
    }

    else if (!is_null($request->storeOrexpose)) {
        # code...
        $perm = new Permanent_collection;
        $perm->ExposeOrStore = $request->storeOrexpose;
        $art->permanent()->save($perm);
    }

    $art->save();
    return redirect('addartwork');
}

这是我的控制器中的功能,当我使用它时会触发以下错误,我不知道我的代码有什么问题。

Call to undefined method Illuminate\Database\Query\Builder::save()

我的网页浏览量是: enter image description here

我正在尝试在函数中保存请求,但它不起作用。 如果你知道如何修复这个PLZ解释它。

3 个答案:

答案 0 :(得分:3)

这是因为您的ArtWork对象不存在于数据库中,您没有保存它们。当您尝试在对象上设置关系时,Laravel会尝试找到他的 id 并且无法执行此操作。
在保存模型关系之前,您应该保存模型本身。

这应该有效

public function AddArtWork(Request $request){
    $art = new Artwork;
    $art->name = $request->name;
    $art->date = $request->date;   
    $art->save();     

    if (!is_null($request->paintStyle)) {
        # code...
        $paint = new Paint;
        $paint->style = $request->paintStyle;
        $art->paint()->save($paint);
    }
    else if (!is_null($request->calligStyle)) {
        # code...
        $callig = new Calligraphy;
        $callig->style = $request->calligStyle;
        $art->calligraphie()->save($callig);
    }
    else if (!is_null($requst->substance)) {
        # code...
        $sclup = new Sculpture;
        $sculp->substance = $request->substance;
        $art->sculpture()->save($sculp);
    }

    if (!is_null($request->recievedDate)) {
        # code...
        $loan = new Loan;
        $loan->date = $request->loanDate;
        $art->loan()->save($loan);
    }

    else if (!is_null($request->storeOrexpose)) {
        # code...
        $perm = new Permanent_collection;
        $perm->ExposeOrStore = $request->storeOrexpose;
        $art->permanent()->save($perm);
    }

    return redirect('addartwork');
}

答案 1 :(得分:0)

Illuminate\Database\Query\Builder没有save方法。使用updateinsert

save方法存在于Model.phpEloquent)中。您的模型必须从Model延伸,然后您才能使用save

答案 2 :(得分:0)

您将$art保存在不同的if else条件中。如下所示

$art->paint()->save($paint);
$art->calligraphie()->save($callig);
$art->sculpture()->save($sculp);

请在保存$art时查看关系$art是一个新实例,如果是新实例,您如何建立关系并保存关系。如果是,则可以保存关系。现有的行,没有现有的它就找不到任何关系,所以关系不会保存