【发布时间】:2016-06-21 22:01:11
【问题描述】:
我想通过我的表单输入存储一些数据。但是,当我提交保存按钮而不是保存数据时,它会重定向到其他页面。谁发现问题,请提供一个好的解决方案。
这是我的 routes.php
<?php
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', function () {
return view('welcome');
});
Route::resource('test','TestController');
Route::get('/home', 'HomeController@index');
});
这是我的测试控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Test;
use App\Http\Requests;
use Redirect;
class TestController extends Controller
{
public function index()
{
$alldata=Test::all();
return view('test.itemlist',compact('alldata'));
}
public function create()
{
return view('test.create_item');
}
public function store(Request $request)
{
$input = $request->all();
Test::create($input);
return redirect('/tasks');
}
}
这是查看页面:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Create Item</div>
{!! Form::open(array('url' => 'test/store','class'=>'form-horizontal','method'=>'POST')) !!}
{!! Form::token(); !!}
<?php echo csrf_field(); ?>
<div class="form-group">
<label>Item Code</label>
<input type="text" name="item_code" class="form-control" placeholder="Code">
</div>
<div class="form-group">
<label>Item Name</label>
<input type="text" name="item_name" class="form-control" placeholder="Name">
</div>
<button type="submit" class="btn btn-default">Submit</button>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
@endsection
这是我的迁移表代码:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTestTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('test_id"');
$table->string('item_code');
$table->string('item_name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
以及测试模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Test extends Model
{
protected $table="tests";
protected $primaryKey="test_id";
protected $fillable=['item_code','item_name'];
}
【问题讨论】:
-
您是否在 Laravel 中遇到任何质量分配错误,您需要在您的
Test模型类中定义protected $fillable = ['column_name1','column_name1']; -
我已经用 fillabale 数组定义了我的测试模型
-
迁移和模型看起来都很好。请发布
dd($request->all());在 store 方法中执行的结果。 -
定义“其他页面”是什么意思?
标签: php laravel laravel-routing laravel-5.2