## web.php ##
Route::resource('students', 'StudentController');
StudentController.php
namespace App\Http\Controllers;
use App\Student; use Illuminate\Http\Request;
class StudentController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $students = Student::all(); return view('admin.student.index', compact('students')); }
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$courses =\App\Course::all();
return view('admin.student.create', compact('courses'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|name',
'email' => 'required|email'
]);
$students = new Student();
$students->name = $request->name;
$students->email = $request->email;
$students->save();
return redirect()->back()->with('success', 'Saved Successfully');
}
index.blade.php
<div class="container">
<div class="row">
<div class="col-md-12">
<br><br>
<a href="{{ url('students/create')}}" class="btn btn-primary pull-right">
<span class="glyphicon glyphicon-plus"></span>Add
Student
</a>
@if (Session::has('success'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Saved</strong>{{ Session::get('success')}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endif
<table class="table table-bordered">
<br><br>
<thead>
<tr>
<th width="10">#</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@if (count($students) == 0)
<tr>
<td colspan="3" class="text-center">No Student found</td>
</tr>
@else
@foreach ($students->all() as $student)
<tr>
<td>{{ $student->id}}</td>
{{-- <td>{{ $}}</td> --}}
<td>{{ strtoupper($student->name)}}</td>
<td>{{ $student->email}}</td>
</tr>
@endforeach
@endif
</tbody>
</table>
</div>
</div>
</div>
create.blade.php
<div class="container">
<div class="row">
<div class="col-md-12">
<br><br>
<form action="{{ route('students.store')}}" method="post">
{{ csrf_field() }}
{{method_field('PUT')}}
<div class="form-group col-md-6">
<label for="exampleFormControlInput1">Name</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="Name">
</div>
<div class="form-group col-md-6">
<label for="exampleFormControlInput1">Email</label>
<input type="text" class="form-control" value="{{ old('email')}}" id="exampleFormControlInput1" placeholder="Email">
</div>
<div class="form-group col-md-6">
<label for="inputState">Select Course</label>
<select id="inputState" name="courses[]" class="form-control courses" multiple="multiple">
@foreach($courses as $course)
<option>{{ $course->name}}</option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-primary pull-right">Create</button>
</form>
</div>
</div>
Comments
Post a Comment