这是我的SubscriptionController代码:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SubscribersController extends Controller
{
//
//This method is to process the form
public function postSubmit() {
//we check if it's really an AJAX request
if(Request::ajax()) {
$validation = Validator::make(Input::all(), array(
//email field should be required, should be in an email//format, and should be unique
'email' => 'required|email|unique:subscribers,email'
)
);
if($validation->fails()) {
return $validation->errors()->first();
} else {
$create = Subscribers::create(array(
'email' => Input::get('email')
));
if($create){
return Redirect::to('/')
->with('success','You have been successfully subscribe to us.');
}else{
echo 'We could not save your address to oursystem, please try again later';
}
}
} else {
return Redirect::to('subscribers');
}
}
}
这是我的Eloquent
代码class CreateSubscribersTable extends Migration
{
public function up()
{
Schema::table('subscribers', function (Blueprint $table) {
$table->increments('id');
$table->string('email,100)->default('');
$table->timestamps();
});
}.....
在Route / web中:
Route::post('/subscribers', 'SubscribersController');
这是welcome.blade.php的代码
<form action="/subscribers" method="post">
<input type="email" name="email" placeholder="Email Address" required>
<button type="button" class="btn btn-danger">Subscribe</button>
</div>
</form>
代码不要插入数据库中,也不要在控制台中显示任何错误。如果出现错误而没有任何内容我输入了postubmit函数回声。 我已经搜索了一个教程而没有找到。 我是Laravel的菜鸟。
答案 0 :(得分:0)
在您的路线文件中,您的路线需要发送到某个操作,而不仅仅是控制器。
Route::post('/subscribers', 'SubscribersController');
应该是这个
Route::post('/subscribers', 'SubscribersController@postSubmit');
将csrf字段添加到html表单中:
<form action="/subscribers" method="post">
{{ csrf_field() }}
<input type="email" name="email" placeholder="Email Address" required>
<button type="button" class="btn btn-danger">Subscribe</button>
</form>
答案 1 :(得分:0)
路线应为Route::post('/subscribers','SubscribersController@postSubmit');
并将csrf_field()
添加到表单。
<form action="/subscribers" method="post">
{{csrf_field()}}
<input type="email" name="email" placeholder="Email Address" required>
<button type="button" class="btn btn-danger">Subscribe</button>
</div>
</form>`
答案 2 :(得分:0)
首先,你跑了吗?
php artisan migrate
其次,正如霍林斯所说,你的路线:
Route::post('/subscribers', 'SubscribersController@postSubmit');
第三,在SubscribersController的顶部,添加:
use Subscribers;
除了让CreateSubscribersTable扩展Migration之外,还需要另一个名为Subscribers的模型类来扩展Model。您可能需要的所有课程:
SubscribersController extends Controller (you have it)
CreateSubscribersTable extends Migration (you have this too)
Subscribers extends Model/Eloquent (Do you have this?)
对不起,这就是我能想到的。将尝试检查更多以帮助。
或者如果你真的想知道问题出在哪里,可以尝试回复一些可能的问题&#34;测试&#34;,在你的每个内容中。
答案 3 :(得分:0)
1)您的migration
内容错误
public function up()
{
Schema::create('subscribers', function (Blueprint $table) { //** 'create', not 'table'
$table->increments('id');
$table->string('email,100)->default('');
$table->timestamps();
});
}
删除您的数据库内容和php artisan migrate
2)您的表格是
<form action="/subscribers" method="post">
{{ csrf_field() }} //** add token
<input type="email" name="email" placeholder="Email Address" required>
<button type="submit" class="btn btn-danger">Subscribe</button> //** Button type : submit
</div>
</form>
您的路线:
Route::post('/subscribers', 'SubscribersController@postSubmit');
您的控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Subscribers; //********* change this
class SubscribersController extends Controller
{
//This method is to process the form
public function postSubmit(Request $request) {
$validation = Validator::make($request->all(), [
//email field should be required, should be in an email format, and should be unique
'email' => 'required|email|unique:subscribers,email',
]);
if($validation->fails()) {
return $validation->errors()->first();
}
$create = Subscribers::create(array(
'email' => $request->email,
));
return Redirect::to('/')->with('success','You have been successfully subscribe to us.');
}
}
将protected $table = 'subscribers';
添加到您的Subscribers
型号。
您为什么使用if (Request::ajax())
?您没有通过AJAX提交表单。