我有点难以理解为什么表单数据没有被从下面的代码提交到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');
我已经尝试了您的建议,将表单标签切换出来,虽然它不会将错误或重定向到我,但它不会将新表单值添加到页面。它只是提交并不显示刚刚提交的新信息。 –
嗨,你想使用资源控制器,而不是restfull控制器?为什么?阅读:http://philsturgeon.co.uk/blog/2013/07/beware-the-route-to-evil – Anam
如果你想使用资源控制器,让我知道。 – Anam