Reputation: 2233
I want to save some data through dropdownlist .. after loading the page, database also fetched with the dropdown but it doesn't save after I clicked the save button. I think there is problem with Course migration table but I couldn't get it. [Scenery is while taking courses student can take a class from the dropdown list.]
Here is my contoller:
<?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');
}
}
Here is my view page:
<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>
Course Table Migration:
<?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');
}
}
The Class table 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()
{
//
}
}
Upvotes: 0
Views: 191
Reputation: 10631
Change {{$row->class_id}}
to {{$row->id}}
Because your classes table does not have class_id column.
Upvotes: 2
Reputation: 331
$fillable
to your model as create
method uses Mass Assignment.category
field in migrations. Your select
has name category
so in database also should be category
field.Basically here you need to use One To Many
P.S. Don't have enough points for comments so answered.
Upvotes: 0