2014-01-07 50 views
0

我有点难以理解为什么表单数据没有被从下面的代码提交到db中。我对Laravel 4有点新,我猜测我错过了代码中的某个地方。当我点击提交时,我被重新路由,但该条目未被添加到数据库。在指出我缺少的东西时,我会很感激。非常感谢。Laravel 4 - 表单数据没有被提交到db

EntriesController.php

<?php 

class EntriesController extends BaseController { 

#Handles "GET /" request 
public function getIndex() 
{ 
    return View::make('home')->with('entries', Entry::all()); 
} 

#Handles "POST /" request 
public function postIndex() 
{ 
    $entry = array(
     'username' => Input::get('frmName'), 
     'email' => Input::get('frmEmail'), 
     'comment' => Input::get('frmComment') 
    ); 

// save the guestbook entry to the database 
Entry::create($entry); 

return Redirect::to('/'); 
} 
} 

?> 

home.blade.php

<html> 
<head> 
    <title>Laravel 4 Guestbook</title> 
</head> 
<body> 
    @foreach ($entries as $entry) 
     <p>{{ $entry->comment }}</p> 
     <p>Posted on {{ $entry->created_at->format('M jS, Y') }} by 
      <a href="mailto:{{ $entry->email }}">{{ $entry->username}}</a> 
     </p><hr> 
    @endforeach 

    <form action="/" method="post"> 
     <table border="0"> 
      <tr> 
       <td>Name</td> 
       <td><input type="text" name="frmName" value="" size="30" maxlength="50"></td> 
      </tr> 

      <tr> 
       <td>Email</td> 
       <td><input type="text" name="frmEmail" value="" size="30" maxlength="100"></td> 
      </tr> 
      <tr> 
       <td>Comment</td> 
       <td><input textarea name="frmComment" row="5" cols="30"></textarea></td> 
      </tr> 

      <tr> 
       <td></td> 
       <td> 
        <input type="submit" name="submit" value="submit"> 
        <input type="reset" name="reset" value="reset"> 
       </td> 
      </tr> 
     </table> 
    </form> 
</body> 

Entry.php

<?php 

类条目扩展雄辩{

/** 
    * The database table used by the model. 
    * 
    * @var string 
    */ 
    protected $table = 'entries'; 

} 

?> 

routes.php文件

Route::controller('/', 'EntriesController'); 

回答

0

试试这个:

EntriesController.php

<?php 

class EntriesController extends BaseController { 


    public function index() 
    { 

     $entries = Entry::all(); 

     //->with('entries', Entry::all()); 
     //in Laravel 4.1, variable pass with 'With' will be process via Session. 
     // changed 'with' to 'compact' 

     return View::make('home', compact('entries')); 
    } 


    public function store() 
    { 
     $entry = array(
      'username' => Input::get('frmName'), 
      'email' => Input::get('frmEmail'), 
      'comment' => Input::get('frmComment') 
     ); 

     // save the guestbook entry to the database 
     Entry::create($entry); 

     // Name route `index` 
     // resourceful controller create 7 routes for you 
     // http://laravel.com/docs/controllers#resource-controllers 

     return Redirect::route('index'); 
    } 
} 

routes.php文件

Route::resource('/', 'EntriesController'); 

home.b lade.php

<html> 
<head> 
    <title>Laravel 4 Guestbook</title> 
</head> 
<body> 
    @foreach ($entries as $entry) 
     <p>{{ $entry->comment }}</p> 
     <p>Posted on {{ $entry->created_at->format('M jS, Y') }} by 
      <a href="mailto:{{ $entry->email }}">{{ $entry->username}}</a> 
     </p><hr> 
    @endforeach 

    {{ Form::open(array('route' => 'store')) }} 
     <table border="0"> 
      <tr> 
       <td>Name</td> 
       <td><input type="text" name="frmName" value="" size="30" maxlength="50"></td> 
      </tr> 

      <tr> 
       <td>Email</td> 
       <td><input type="text" name="frmEmail" value="" size="30" maxlength="100"></td> 
      </tr> 
      <tr> 
       <td>Comment</td> 
       <td><input textarea name="frmComment" row="5" cols="30"></textarea></td> 
      </tr> 

      <tr> 
       <td></td> 
       <td> 
        <input type="submit" name="submit" value="submit"> 
        <input type="reset" name="reset" value="reset"> 
       </td> 
      </tr> 
     </table> 
    {{ Form::close() }} 
</body> 
+0

我已经尝试了您的建议,将表单标签切换出来,虽然它不会将错误或重定向到我,但它不会将新表单值添加到页面。它只是提交并不显示刚刚提交的新信息。 –

+0

嗨,你想使用资源控制器,而不是restfull控制器?为什么?阅读:http://philsturgeon.co.uk/blog/2013/07/beware-the-route-to-evil – Anam

+0

如果你想使用资源控制器,让我知道。 – Anam

0

它看起来像你的窗体操作指向错误的地方。 '/'路径是您的getIndex()路线。我相信它应该是:

<form action="postIndex" method="post"> 
     <table border="0"> 
      <tr> 
       <td>Name</td> 
       <td><input type="text" name="frmName" value="" size="30" maxlength="50"></td> 
      </tr> 
+0

嗯,我只是去尝试,并得到了以下错误 - 没有发现的Symfony \分量\ HttpKernel \异常\ NotFoundHttpException 控制方法。 –