使用 Laravel 将包含图像的内容发布到数据库中

Posting content into the database with an image using Laravel

我正在尝试 post 使用 laravel 将一些内容存入数据库,但它似乎不起作用...

这是我得到的:

HTML:

{{ Form::open(array('role' => 'form')) }}
                            <div class="form-body">
                                <div class="form-group">
                                    <label>Titel</label>
                                    <input type="text" class="form-control" name="title" placeholder="Titel komt hier">
                                </div>

                                <div class="form-group">
                                    <label>Textarea</label>
                                    <textarea class="form-control" name="message" rows="5" placeholder="Uw bericht..."></textarea>
                                </div>

                                <div class="form-group">
                                    <label for="exampleInputFile1">Nieuws afbeelding</label>
                                    <input type="file" name="img">
                                </div>

                            </div>

                            <div class="form-actions">
                                <input type="submit" class="btn green" value="Oplsaan" />
                            </div>

                        {{ Form::close() }}

                        @if ($errors->any())
                            <ul>
                                {{ implode('', $errors->all('<li class="error">:message</li>')) }}
                            </ul>
                        @endif

显示的都很好....

Exept 当我尝试 'post' 新闻时,因为那是我尝试做的,它只是刷新页面。该页面的 URL 是我的域。com/admin/news/write

我的路由器是这样的:

Route::resource('admin/news/write', 'AdminController@create');

首先在群里认证:

Route::group(array('before' => 'auth'), function()
    {
            Route::resource('admin', 'AdminController');
            Route::resource('admin/news/write', 'AdminController@create');
    });

一切正常,但是当我更改 Route::resource('admin/news/write', 'AdminController@create');至 Route::post('admin/news/write', 'AdminController@create');我收到一个错误,我看不到...

很好,现在我的控制器:

public function store()
{
    $rules = array(
        'title' => 'required',
        'message'   => 'required',
    );  

    $validator = Validator::make(Input::all(), $rules);

    if ($validator->passes())
    {
        if (Input::only('title', 'message'))
        {

            return Redirect::to('admin/news/write')->with('message', 'Het nieuws werd gemaakt!');

        }

    }
    else
    {
        return Redirect::to('admin/news/write')->with('message', "Er ging iets mis: ")->withErrors($validator);
    }

}

问题是,我不知道如何将图像存储到

/public/pictures/news

然后将完整的文件名存储到数据库中,如果有人可以帮助我...我需要快速回复,因为我有截止日期...:{

最诚挚的问候

首先,您需要使用 laravel 助手告诉您的表单,这将上传一个文件...

Form::open(['method'=>'POST', 'role' => 'form', 'files' => true])

在您的控制器中,您想从输入中获取文件

$imgFile = Input::file('img');

现在要将文件从已上传的临时位置移动到更永久的位置,请调用以下命令(其中 $filename 是您要调用的已上传文件)...

$dir = '../storage/app/upload/';
$imgFile->move($dir.$filename);

此处应用程序的根目录路径是 ../(来自 public)所以.. ../storage/app/upload/ 将是用于上传文件的绝佳位置。

然后你可以写:

$dir.$filename;

返回数据库 - 工作完成:)

编辑 :: -- 你的控制器 --

你的解析控制器是基于资源的...

所以你的路线是:

Route::group(array('before' => 'auth'), function()
{
    Route::resource('admin', 'AdminController');
}

您的控制器本身将具有如下结构(记住这一点:http://laravel.com/docs/4.2/controllers#restful-resource-controllers):

class AdminController extends BaseController {
    public function index(){...}
    public function create(){...}
    public function 
    //The store() method is an action handled by the resource controller
    //Here we're using it to handle the post action from the current URL
    public function store()
    {
       $imgFile = Input::file('img');
       //processing code here....
    }
    public function show(){...}
    public function edit(){...}
    public function update(){...}
    public function destroy(){...}   
}

我解决了这个问题。

我的控制器:

    <?php
class AdminNewsController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        return View::make('admin.news.create');
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('admin.news.create');
    }


    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $rules = array(
            'title'         => 'required',
            'message'       => 'required',
            'publish'       => 'required'
            );
        $validator = Validator::make(Input::all(), $rules);

        //process the storage
        if ($validator->fails())
        {
            Session::flash('error_message', 'Fout:' . $validator->errors());
            return Redirect::to('admin/news/create')->withErrors($validator);
        }else{

            //store
            $news                   = new News;
            $news->title            = Input::get('title');
            $news->message          = Input::get('message');
            $news->img_url          = Input::file('img')->getClientOriginalName();
            $news->posted_by        = Auth::user()->username;
            $news->published_at     = time();
            $news->published        = Input::get('publish');
            $news->save();

            //save the image
            $destinationPath = 'public/pictures/news';

            if (Input::hasFile('img'))
{
    $file = Input::file('img');
    $file->move('public/pictures/news', $file->getClientOriginalName());
}
            //redirect
            Session::flash('success', 'Nieuws succesvol aangemaakt!');
            return Redirect::to('admin/news/create');

        }
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        //
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }


}

我的create.blade.php

<div class="portlet-body form">
                    {{ Form::open(['method'=>'POST', 'role' => 'form', 'files' => true]) }}
                            <div class="form-body">
                                <div class="form-group">
                                    <label>Titel</label>
                                    <input type="text" class="form-control" name="title" placeholder="Titel komt hier">
                                </div>

                                <div class="form-group">
                                    <label>Textarea</label>
                                    <textarea class="form-control" name="message" rows="5" placeholder="Uw bericht..."></textarea>
                                </div>

                                <div class="form-group">
                                    <label>Nieuws afbeelding</label>
                                    {{ Form::file('img') }}
                                </div>

                                <div class="form-group">
                                    <label>Bericht publiceren?</label>
                                    <div class="radio-list">

                                        <label class="radio-inline">
                                        <span>
                                        {{ Form::radio('publish', '1') }}
                                        </span>
                                        <b style="color:green">Publiceren</b>
                                        </label>

                                        <label class="radio-inline">
                                        <span>
                                        {{ Form::radio('publish', '0', true) }}
                                        </span>
                                        <b style="color:red">Niet publiceren</b>
                                        </label>


                                    </div>
                                </div>

                            </div>

                            <div class="form-actions">
                                <input type="submit" class="btn green" value="Oplsaan" />
                            </div>

                        {{ Form::close() }}
                    </div>

然后一切正常!

感谢马特·巴伯的帮助!