Laravel:在非对象上调用成员函数 getRealPath()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38439041/
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: Call to a member function getRealPath() on a non-object
提问by Grey
I'm creating a website for a client with a gallery for images etc.
我正在为客户创建一个网站,其中包含一个图片库等。
I'm running into this error:
我遇到了这个错误:
ImageController Line 16: Call to a member function getRealPath() on a non-object
ImageController Line 16: Call to a member function getRealPath() on a non-object
ImageController
looks like this:
ImageController
看起来像这样:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Image;
use Illuminate\Http\Request;
class ImageController extends Controller {
public function __construct() {
}
public function store(Request $request) {
$file = $request->file('image')->getRealPath();
$image = new Image();
$image->title = $request->get('title');
$image->desc = $request->get('desc');
//$image->date = $request->get('created_at');
$image->image = base64_encode(file_get_contents($file));
$image->type = pathinfo($file, PATHINFO_EXTENSION);
if($image->save()) {
return redirect(route('web.home'))->with('status', 'Uploaded!');
} else {
return redirect(route('store.upload'))->with('status', 'Upload failed!')->withInput();
}
}
}
and my form looks like this:
我的表格是这样的:
<form action="{{ url(route('store.upload')) }}" method="post" enctype="multipart/form-data">
{!! csrf_field() !!}
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="title" name="title">
<label class="mdl-textfield__label" for="title">Titel</label>
</div>
<div class="mdl-textfield mdl-js-textfield">
<textarea class="mdl-textfield__input" type="text" rows="5" id="desc"></textarea>
<label class="mdl-textfield__label" for="desc">Beschrijving</label>
</div>
<input type="file" name="image">
<button class="mdl-button mdl-js-button mdl-button--fab mdl-js-ripple-effect mdl-button--colored right" type="submit">
<i class="material-icons">add</i>
</button>
This is basically an edit of an earlier gallery I created and should (in theory) work.
这基本上是我创建的早期画廊的编辑,应该(理论上)工作。
采纳答案by Grey
Okay, I found the problem, I declarede one of the routes wrong, after which the error went away, I got another error, which was because I forgot to add a file type column to the table, after which I forgot to set the name for the description, so that also resulted in an error, every error is now fixed and works perfectly
好的,我发现问题了,我声明了其中一个路由错误,之后错误消失了,我又出现了另一个错误,这是因为我忘记在表中添加文件类型列,之后我忘记设置名称对于描述,因此也导致了错误,现在每个错误都已修复并且可以完美运行