我想通过Laravel 5.0保存表单输入 页面重新加载并返回索引页面。但是数据不会通过路由保存到mysql。
这是控制器:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Http\Requests\CreateTaskRequest;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Task;
use Input;
class TodoController extends Controller {
public function index()
{
$data=Task::all();
// $options=Option::whereNotIn('id',$activeCourse)->lists('option_name','id');
return view('home')->with('data',$data);
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//return view('home');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(CreateTaskRequest $request)
{
// dd($request->all());
/*$data=[
'name'=>Input::get('name'),
'status'=>Input::get('status'),
'description'=>Input::get('description'),
'task_img'=>Input::get('task_img'),
'dob'=>Input::get('dob')
];*/
$response=new Task();
$response->name=$request->input('name');
$response->status=$request->input('status');
$response->description=$request->input('description');
$response->description=$request->input('task_img');
$response->description=$request->input('dob');
$response->save();
// dd('working?');
//$response=Task::create(['name'=>$request->input('name'),'status'=>$request->input('status'),'description'=>$request->input('description')]);
if($response){
return redirect()->back()->with('success','Task Created Successfully');
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$data=Task::find($id);
return view('todo_edit')->with('data',$data);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update(CreateTaskRequest $request, $id)
{
$data=[
'name'=>Input::get('name'),
'status'=>Input::get('status')
];
$response=Task::find($id)->update($data);
if($response){
return redirect('/');
}
}
public function destroy($id)
{
$response=Task::find($id)->delete();
if($response){
return redirect('/')->with('success','Task Deleted Successfully');;
}
}
}
这是我的观看页面:
@extends('app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-12 ">
<div class="col-lg-12">
<div class="col-lg-6">
{!! Form::open(array('route' =>'' ,'method'=>'post')) !!}
{!! Form::hidden('_token',csrf_token()) !!}
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Enter a task name"></br>
</br>
<lavel> <input name="status" type="radio" value="Complete"> Complete</lavel></br>
<label> <input checked="checked" name="status" type="radio" value="Incomplete"> Incomplete</label></br>
<textarea class="form-control" name="description" rows="3"></textarea></br>
<label>File input</label>
<input type="file" name="task_img"></br>
<input type="date" class="form-control datepicker" name="dob" style="width:95%">
{!! Form::submit('Save', array('class' => 'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
</div>
</div>
<h3> Todo Application </h3>
<table class="table table-striped table-bordered" id="example">
<thead>
<tr>
<td>Serial No</td>
<td>Task Name</td>
<td>Status</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<?php $i=1; ?>
@foreach($data as $row)
<tr>
<td>{{$i}}</td>
<td>{{$row->name }}</td>
<td>{{$row->status}}</td>
<td>
<a href="{{route('getEditRoute',$row->id)}}" class="btn btn-warning">Edit</a>
<form action="{{route('postDeleteRoute',$row->id)}}" method="POST" style="display:inline;"
onsubmit="if(confirm('Are you sure?')) {return true} else {return false};">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-danger" value="Delete">
</form>
</td>
</tr>
<?php $i++; ?>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
@section('page-script')
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$('#example').DataTable();
} );
</script>
@stop
@endsection
以下是路线:
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/',
'TodoController@index');
Route::post('/', 'TodoController@store');
Route::get('/{id}/edit',['as'=>'getEditRoute','uses'=>'TodoController@edit']);
Route::post('/{id}/edit',['as'=>'postUpdateRoute','uses'=>'TodoController@update']);
Route::post('/{id}/delete',['as'=>'postDeleteRoute','uses'=>'TodoController@destroy']);
//Route::get('home', 'HomeController@index');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
答案 0 :(得分:3)
要从表单保存数据,您可以执行以下操作:
在你的表格中替换
{!! Form::open(array('route' =>'' ,'method'=>'post')) !!}
与
{!! Form::open(array('route' =>'form.store' ,'method'=>'post')) !!}
在你的路线替换
Route::post('/', 'TodoController@store');
与
Route::post('/store',array('as'=>'form.store',uses=>'TodoController@store');
在你的控制器中替换
public function create()
{
//return view('home');
}
带
public function store(Request $request) //Http request object
{
Task::create($this->request->all());//assumes that you want to create task.
return \Redirect::route('/')->with('message', 'Task saved sucessfully!!!');
}
如果您想了解更多信息,请阅读以下链接中的文档:
希望它有所帮助。快乐编码:)。