laravel 在laravel中将图片上传到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28430353/
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
Uploading picture to database in laravel
提问by Smajo
I have created a form where a user can add news, and i want them to be able to upload a picture as well. I have tried too read what other users create a upload form, but cant understand it. The picture that gets uploaded needs to store the picture path and all the other information in the database that i have created. Reason for this is that i want to later display the news and picture that the user created, so it can be viewed. So i need some help, since i don't quite understand how to do this and I am a beginner in laravel. I don't want to use any .js or other scripts since i want to learn these basics first.
我创建了一个用户可以添加新闻的表单,我希望他们也能够上传图片。我也试过阅读其他用户创建的上传表单,但无法理解。上传的图片需要在我创建的数据库中存储图片路径和所有其他信息。这样做的原因是我想稍后显示用户创建的新闻和图片,以便可以查看。所以我需要一些帮助,因为我不太明白如何做到这一点,而且我是 Laravel 的初学者。我不想使用任何 .js 或其他脚本,因为我想先学习这些基础知识。
My database:
我的数据库:
class NyhetsmodulTable extends Migration {
public function up()
{
Schema::create('news', function($table)
{
$table->increments('id');
$table->string('title');
$table->string('author');
$table->string('message');
$table->boolean('active');
$table->timestamps();
$table->string('picture_path');
});
}
My create.plade.php file (not sure if my file form is right):
我的 create.plade.php 文件(不确定我的文件格式是否正确):
{{ Form::open(array('route' => 'adminpanel.newsmodule.store', 'files' => true)) }}
<ul>
<li>
{{ Form:: label ('title', 'Title: ' )}}
{{ Form:: text ('title')}}
</li>
<li>
{{ Form:: label ('author', 'Author: ' )}}
{{ Form:: text ('author')}}
</li>
<li>
{{ Form:: label ('message', 'News: ' )}}
{{ Form:: textarea ('message') }}
</li>
<li>
{{ Form::file('image')}}
</li>
<li>
</li>
<li>
{{ Form::submit('Submit') }}
</li>
</ul>
My controller, that i dont understand how to set up:
我的控制器,我不明白如何设置:
public function uploadFile()
{
}
My route:
我的路线:
Route::post('adminpanel/newsmodule/create',
[
'uses' => 'NyhetsController@uploadFile',
'as' => 'adminpanel.newsmodule.upload'
]
);
回答by Umar Hasan
Form open tag should be
表单打开标签应该是
{{ Form::open(array('url' => 'adminpanel.newsmodule.create', 'files' => true)) }}
in first line, there is no need to redeclare form inside form files should be set true, so it can send files
在第一行,不需要在表单文件中重新声明表单应该设置为真,这样它就可以发送文件
and to Store image,
并存储图像,
$news = News::find(1)
$news->image = Input::file('image');
$news->save();
since i thougt u just save path,
因为我认为你只是保存路径,
$pathToFile = '/foo/bar/baz.jpg
Image::make(Input::file('image')->save($pathToFile);
$news->picture_path = $pathToFile;
$news->save();