[英]Class name problem on laravel 5.8 with Request->input
I'm trying to follow a tutorial to create a simple web scraper here using Laravel, but symfony threw a "Class name must be a valid object or string" error on line 49. In phpstorm it did gave me a light warning of field accessed with magic method on $website->title 我正在尝试使用教程在这里使用Laravel创建一个简单的Web爬虫,但是symfony在第49行上抛出了“类名必须是有效的对象或字符串”错误。在phpstorm中,它确实给了我有关访问字段的警告在$ website-> title上使用魔术方法
I've tried to declare $title as a public var in my App/Website.php but it still gave me this error. 我试图在我的App / Website.php中将$ title声明为一个公共变量,但是它仍然给我这个错误。
here's the snippet of the code with error 这是有错误的代码片段
public function store(Request $request)
{
$this->validate($request,[
'title'=>'required',
'url'=>'required',
'logo'=>'required'
]);
$website = new Website();
$website->title = new $request->input('title');
$website->url = $request->input('url');
$website->logo = $this->uploadFile('logo', public_path('uploads/'), $request)["filename"];
$website->save();
return redirect()->route('websites.index');
}
/**
* Display the specified resource.
*
and here's my App/Website Class: 这是我的应用程序/网站类:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Website extends Model
{
protected $table = "website";
public $title;
/**
* @var array|string|null
*/
public $url;
public $logo;
}
It should've saved the title, url and logo to a sql db i named scraper but it keeps throwing this error. 它应该已经将标题,URL和徽标保存到了我名为scraper的sql db中,但是它一直抛出此错误。 Please help.
请帮忙。
Edit 2:
I apologize it seems i copied the code shown by symfony, my actual WebsiteController is like this
copied wrong code again, here's the actual actual code: 编辑2:
很抱歉,我似乎复制了symfony显示的代码,我的实际WebsiteController就像是
再次复制了错误的代码,这是实际的实际代码:
public function store(Request $request)
{
$this->validate($request,[
'title'=>'required',
'url'=>'required',
'logo'=>'required'
]);
$website = new Website();
$website->title = new $request->input('title');
$website->url = $request->input('url');
$website->logo = $this->uploadFile('logo', public_path('uploads/'), $request)["filename"];
$website->save();
return redirect()->route('websites.index');
}
Here new
should not be there , $website->title = new $request->input('title');
这里
new
应该不存在, $website->title = new $request->input('title');
, You are not making object from any class. ,您没有从任何类中创建对象。 i guess you miss type.
我想你想念类型。
and you can still get your data by just $request->title;
而且您仍然可以通过
$request->title;
来获取数据$request->title;
i prefer this because it short your code and still readable. 我喜欢这样做,因为它可以缩短您的代码并且仍然可读。
Do it like below 像下面那样做
$website->title = $request->input('title');
Or 要么
$website->title = $request->title;
and also for saving data into db you don't need to declare variable. 并且也可以将数据保存到db中,而无需声明变量。
simply in model add protected $fillable=['title','url','logo'];
只需在模型中添加受保护的
$fillable=['title','url','logo'];
or if you using save()
method you don't even need to add $fillable
或者,如果您使用
save()
方法,甚至不需要添加$fillable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.