Laravel 5.4 版本调用未定义的方法 Illuminate\Database\Query\Builder::quoutes()。错误是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44614702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.4 version Call to undefined method Illuminate\Database\Query\Builder::quoutes(). What is the error?
提问by Vladimir Kirov
I get a
我得到一个
Call to undefined method Illuminate\Database\Query\Builder::quoutes()
调用未定义的方法 Illuminate\Database\Query\Builder::quoutes()
error message inside of my Controller, this is how my Controller looks like:
我的控制器内部的错误消息,这是我的控制器的样子:
My QuoteController.php:
我的 QuoteController.php:
class QuoteController extends Controller
{
public function postQuote(Request $request)
{
$authorText = ucfirst($request['author']);
$quoteText = $request['quote'];
$author = Author::where('name', $authorText)->first();
if (!$author) {
$author = new Author();
$author->name = $authorText;
$author->save();
}
$quote = new Quote();
$quote->quote = $quoteText;
$author->quoutes()->save($quote);
return redirect()->route('index')->with([
'success' => 'Quote saved!'
]);
}
回答by Vladimir Kirov
Author code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Author extends Model
{
public function quotes()
{
return $this->hasMany('App\Quote');
}
}
回答by Lorn Titya
You call the name of quotes wrong:
你称引号的名称是错误的:
$author->quoutes()->save($quote);
Replace it with:
替换为:
$author->quotes()->save($quote);