dplo59755 2016-07-27 08:33
浏览 41
已采纳

如何在laravel 5.2中编写更新查询

I am fetching data from the table to one form using laravel 5.2 for editing or updating existing data. But when i click on update button its not updating. Can anyone please help me to find out the mistake that i did in my code . My edit form is add.blade.php given below

   @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

controller page GroupController.php is

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
    }

}

and routes.php is

    Route::any('group/edit/{id}', 'GroupController@edit');
Route::post('group/update', 'GroupController@update');

my model Group.php is

    <?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',
    ];
}
  • 写回答

1条回答 默认 最新

  • duanhuang1699 2016-07-27 08:40
    关注

    If you're using models:

    $device = Group::where("groupID", "=", $request['groupID'])->first();
     if ($device)
     {
          $device->description = $request['description'];
          $device->save();
     }
     else
          echo "Can't find group";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序