2015-04-15 25 views
1

我試圖用laravel發佈一些東西到數據庫中,但它似乎沒有工作的圖像數據庫...發帖內容與使用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當我嘗試「後」的消息,因爲這是我嘗試做,它只是refreses的頁面。該網址到頁面mydomain.com/admin/news/write

我的路由器是這樣的:

Route::group(array('before' => 'auth'), function() 
    { 
      Route::resource('admin', 'AdminController'); 
      Route::resource('admin/news/write', '[email protected]'); 
    }); 

這一切工作:

Route::resource('admin/news/write', '[email protected]'); 

首先,它是一組認證,但是當我更改Route ::資源('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); 
    } 

} 

的問題是,我不知道我怎麼可以將圖像存儲

/公/圖片/消息

然後完整的文件名存儲到數據庫中,如果有人可以幫助我......我需要一個反應迅速,東陽我有一個最後期限...: {

最親切的問候

回答

3

首先,你需要使用,這是將要上傳文件的laravel助手告訴你的表格......

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

在你的控制器,你想從文件輸入

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

現在將文件從它被上傳的臨時位置移動到一個更穩定的位置撥打以下(其中$ filename是你要撥打的uploade什麼d文件)...

$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(){...} 
} 
+0

好的,但我的控制器如何看起來像? – Robin

+0

首先看看你想要進入的錯誤,這取決於你的laravel版本,但是laravel 4的app/config/app.php或者laravel 5的config/app.php,並且將debug選項改爲true 。 這將防止錯誤被取消 - 訪問您的控制器時。 –

+0

至於資源永遠記住每個路由和方法的用途:http://laravel.com/docs/5.0/controllers#restful-resource-controllers - 所以如果create被用作GET請求 - 那麼明確地將你的表單設置爲'method'=>'GET'。 如果您發送POST,然後在您的控制器中使用存儲方法來處理傳入的請求。希望開啓調試將有助於排除故障。 –

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的幫助!