我使用laravel 5.2从表中获取数据到一个表单,用于编辑或更新现有数据。但是当我点击更新按钮时它没有更新。任何人都可以帮我找出我在代码中所犯的错误。 我的编辑表单是下面给出的add.blade.php
@extends('app')
@section('content')
<div class="templatemo-content-wrapper">
<div class="container">
<ol class="breadcrumb">
<li><a href="{{ url("/") }}"><font color="green">Home</font></a></li>
<li class="active">View/Edit Group</li>
</ol>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-success">
<div class="panel-heading">View/Edit Group Information</div>
<div class="panel-body">
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form class="form-horizontal" role="form" method="POST" action="{{ url('group/update/') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
@foreach($groups as $grp)
<div class="form-group">
<label class="col-md-4 control-label">Group ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="userID" value="{{ ($grp->groupID)}}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Group Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="description" value="{{ ($grp->description) }}" >
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-warning">
Update
</button>
</div>
</div>
@endforeach
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
控制器页面GroupController.php是
public function edit($id)
{
try {
//Find the user object from model if it exists
$groups = DB::table('devicegroup')->where('groupID', '=', $id)->get();
//$user = User::findOrFail($id);
//Redirect to edit user form with the user info found above.
return view('group.add')->with('groups', $groups);
} catch (ModelNotFoundException $err) {
//redirect to your error page
}
}
// Update Group
public function update(Request $request)
{
try {
$data=DB::table('devicegroup')
->where("groupID",$request['groupID'])
->update(array("description"=>$request['description']
));
$groups = DB::table('devicegroup')->simplePaginate(10);
return view('group.groupAdmin')->with('groups', $groups);
} catch (ModelNotFoundException $err) {
//Show error page
}
}
和routes.php是
Route::any('group/edit/{id}', 'GroupController@edit');
Route::post('group/update', 'GroupController@update');
我的模型Group.php是
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Group extends Model
{
protected $table = 'devicegroup';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'groupID', 'description',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
//'password', 'remember_token',
];
}
答案 0 :(得分:0)
如果您正在使用models
:
$device = Group::where("groupID", "=", $request['groupID'])->first();
if ($device)
{
$device->description = $request['description'];
$device->save();
}
else
echo "Can't find group";