gpt4 book ai didi

php - 拉维尔 5 : Data does not saved through blade form

转载 作者:太空宇宙 更新时间:2023-11-03 11:50:43 24 4
gpt4 key购买 nike

我想通过 Laravel 5.0 保存一个表单输入页面重新加载并返回到索引页面。但是数据没有通过路由保存到mysql。

这是 Controller :

<?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',
]);

最佳答案

要从表单中保存数据,您可以执行以下操作:

在你的表单中替换

{!! 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');

然后在你的 Controller 中替换

 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!!!');

}

如果您想探索更多,请阅读以下链接中的文档:

Laravel Routes

Laravel Request

Laravel Controllers

希望对您有所帮助。快乐编码 :) .

关于php - 拉维尔 5 : Data does not saved through blade form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35668384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com