laravel 5.2数据库不会通过存储方法存储在控制器中

时间:2016-03-08 04:52:00

标签: php laravel laravel-routing laravel-5.2

我想通过表单输入存储一些数据。但是当我提交保存按钮而不是保存数据时,它会重定向到其他页面。任何发现问题的人,请提供一个很好的解决方案。

这是我的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'];
}

1 个答案:

答案 0 :(得分:0)

这是解决方案:实际上存储方法错误地我已经把

 return redirect('/tasks');

而不是

return redirect('/test');
BTW感谢所有到目前为止尝试过的人。