2016-06-07 116 views
0
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'); 
} 

這是我的控制器中的功能,當我使用它觸發以下錯誤,我不知道我的代碼中有什麼問題。調用未定義的方法Illuminate Database Query Builder :: save()錯誤如何解決?

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

我的網頁的看法是: enter image description here

我試圖保存請求的功能,但它不能正常工作。 如果你知道如何解決這個PLZ解釋它。

回答

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

非常感謝你,它的作品 –

0

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

save方法存在於Model.phpEloquent)中。您的模型必須從Model延伸,然後您可以使用save

+0

我已經延長我的模型從model.then有什麼問題。「類藝術品擴展模式」 –

+0

其他車型從模型擴展?什麼代碼行導致錯誤? –

+0

所有這些擴展。錯誤是「Builder.php行2161」,它不適合我。 –

0

你在的if else condition.Like不同以下

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

節省$art請看看當你保存$art有關係$art是一個新的實例,如果它是一個新的實例,你怎麼可以讓那關係並保存關係。你可以保存關係,如果它和現有的行,沒有一個現有的它不會發現任何關係,所以關係不會保存

+0

我已經做了.i延伸。如果你使用php工匠make:模型創建一個類與它默認。這正是讓我困惑。我不能undrest和什麼問題 –

+0

@AMIRHOSSEINSOROURI我更新了我的答案 –

相關問題