在laravel中将图片上传到数据库

问题描述 投票:1回答:1

我创建了一个用户可以添加新闻的表单,我希望他们也可以上传图片。我已经尝试过阅读其他用户创建上传表单的内容,但无法理解。上传的图片需要将图片路径和所有其他信息存储在我创建的数据库中。原因是我想稍后显示用户创建的新闻和图片,因此可以查看。所以我需要一些帮助,因为我不太明白如何做到这一点,而且我是laravel的初学者。我不想使用任何.js或其他脚本,因为我想首先学习这些基础知识。

我的数据库:

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');
        });
    }

我的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>

我的控制器,我不明白如何设置:

public function uploadFile()
{

}

我的路线:

Route::post('adminpanel/newsmodule/create',
    [
        'uses' => 'NyhetsController@uploadFile',
        'as' => 'adminpanel.newsmodule.upload'
    ]
);
php database laravel image-uploading
1个回答
0
投票

表格打开标签应该是

{{ Form::open(array('url' => 'adminpanel.newsmodule.create', 'files' => true)) }}

在第一行,没有必要重新声明表单里面的表单文件应该设置为true,这样就可以发送文件了

和存储图像,

$news = News::find(1)
$news->image = Input::file('image');
$news->save();

因为我以为你只是保存路径,

$pathToFile = '/foo/bar/baz.jpg
Image::make(Input::file('image')->save($pathToFile);
$news->picture_path = $pathToFile;
$news->save();
© www.soinside.com 2019 - 2024. All rights reserved.