[英]What's wrong with my Laravel 5.5 save() method?
I am using Laravel 5.5, I want to do basic form input where is only one field->"email". 我正在使用Laravel 5.5,我想进行基本的表单输入,其中只有一个字段->“电子邮件”。 I am using Eloquent, model to interact with database for these subscriber inputs.
我正在使用Eloquent模型与这些订户输入的数据库进行交互。 When the controller method is called, this error follows:
调用controller方法时,将出现以下错误:
FatalThrowableError (E_ERROR) Call to a member function save() on string
FatalThrowableError(E_ERROR)在字符串上调用成员函数save()
The thing is I am using exactly the same solution for other form I've got in my application (contact form). 关键是我在应用程序中使用的其他表单(联系表单)使用的是完全相同的解决方案。 That's the reason why I am pretty sure, that namespacing, models or other stuff I written well.
这就是我很确定我写的命名空间,模型或其他内容的原因。
This is my code: 这是我的代码:
SubsController.php SubsController.php
class SubsController extends Controller
{
public function store(Request $request)
{
$subscriber = new Subscriber;
$subscriber=$request->input('email');
$subscriber->save();
return redirect()->to(route('homepage'));
}
}
Please check this line, you just assigned a string value to your $subscriber
variable 请检查这一行,您刚刚为
$subscriber
变量分配了一个字符串值
$subscriber =$request->input('email');
The correct way is 正确的方法是
public function store(Request $request) {
$subscriber = new Subscriber;
$subscriber->email =$request->input('email');
$subscriber->save();
return redirect()->to(route('homepage'));
}
Here is the solution : 这是解决方案:
$subscriber = new Subscriber;
$subscriber->email = $request->input('email');
$subscriber->save();
return redirect()->to(route('homepage'));
One more solution is 另一种解决方案是
public function store() {
$data = request()->validate('email');
Subscriber::create($data);
return redirect()->route('homepage');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.