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

时间:2015-04-15 11:56:32

标签: php laravel-4 controller blade

我正在尝试使用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

显示得很好......

当我尝试“发布”新闻时,请执行此操作,因为这是我尝试做的事情,它只会刷新页面。该页面的URL是mydomain.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'); to 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);
    }

}

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

  

/公共/图片/消息

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

最诚挚的问候

2 个答案:

答案 0 :(得分:3)

首先,您需要使用laravel帮助器告诉您的表单,这将是上传文件......

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

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

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

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

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

此处应用程序根目录的路径是../(一个来自公共场所)所以.. ../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(){...}   
}

答案 1 :(得分:0)

我解决了这个问题。

我的控制器:

    <?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>

然后一切正常!

感谢Matt Barber的帮助!