Laravel 5:数据库无法通过下拉列表保存

时间:2016-03-09 09:31:42

标签: php laravel-5 laravel-5.2

我想通过下拉列表保存一些数据..加载页面后,数据库也会使用下拉列表获取但是在我单击保存按钮后它不会保存。我认为课程迁移表存在问题,但我无法得到它。 [风景是在参加课程时,学生可以从下拉列表中选择一门课程。]

这是我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Course;
use App\TheClass;
use Redirect;

class courseController extends Controller
{
    public function index()
    {
        $alldata=Course::all();
        return  view('course.index',compact('alldata'));
    }


    public function create()
    {
         $input=\App\TheClass::all();   
        return view('course.create',compact('input'));
    }


    public function store(Request $request)
    {
        $input = $request->all();
        Course::create($input);
        return redirect('course');
    }
}

这是我的观看页面:

<html>
    <head> 
        <title> Create Course </title>
         <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
    </head>
    <body>

        <div class="container" >
            <h3> Create course </h3>
        {!! Form::open(array('route' => 'course.store','class'=>'form-horizontal'))  !!}
        {!! Form::token(); !!}
            <?php echo csrf_field(); ?>
            <div class="form-group">
                <label>Course Code</label>
                <input type="text" name="course_code" class="form-control"  placeholder="Code">
            </div>
            <div class="form-group">
                <label>Course Title</label>
                <input type="text" name="course_title" class="form-control"  placeholder="Title">
            </div>
            <div class="form-group">
                <label>Course Credit</label>
                <input type="text" name="course_credit" class="form-control"  placeholder="Credit">
            </div>
   <div class="form-group">
            <label for="">Class</label>
            <select class="form-control input-sm" name="class_id" >
            @foreach($input as $row)
            <option value="{{$row->class_id}}">{{$row->class_name}}</option>

            @endforeach
            </select>
          </div>

            <button type="submit" class="btn btn-default">Submit</button>
        {!! Form::close() !!}
        </div>
    </body>
</html>

课程表迁移:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCourseTable extends Migration
{
    public function up()
    {
        Schema::create('courses', function (Blueprint $table) {
            $table->increments('course_id');
            $table->string('course_code',10);
             $table->string('course_title',50);
             $table->string('course_credit');
            $table->integer('class_id')->unsigned(); 
        $table->timestamps();
        $table->foreign('class_id')->references('id')->on('classes');  
        });
    }

    public function down()
    {
        Schema::drop('courses');
    }
}

Class表Migration:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateClassTable extends Migration
{

    public function up()
    {
        Schema::create('classes', function (Blueprint $table) {
            $table->increments('id');
            $table->string('class_name',10);            
            $table->timestamps();
        });
    }


    public function down()
    {
        //
    }
}

2 个答案:

答案 0 :(得分:1)

{{$row->class_id}}更改为{{$row->id}}

因为您的班级表没有 class_id 列。

答案 1 :(得分:0)

  • 首先,您需要将$fillable添加到模型create 方法使用Mass Assignment
  • 其次,我在迁移中看不到任何category字段。你的select 名称为category,因此在数据库中也应该是category字段。

基本上你需要使用One To Many

P.S。没有足够的评论积分,所以回答。