我有点难过为什么表单数据没有从下面的代码提交到数据库中。我对Laravel 4有点新鲜,我猜测我在代码的某个地方错过了一个电话。当我单击提交时,我重新路由但该条目未添加到数据库中。在指出我缺少的内容时,将不胜感激。非常感谢你。
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
class Entry扩展了Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'entries';
}
?>
routes.php文件
Route::controller('/', 'EntriesController');
答案 0 :(得分:0)
看起来您的表单操作指向错误的位置。 &#39; /&#39;路径是您的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>
答案 1 :(得分: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.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::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>