如何获得id&从laravel 5.1中的选项值更新id到数据库

时间:2016-08-15 10:26:35

标签: php mysql laravel laravel-5.1

我目前正在做一个名为医生管理系统的项目。在这个项目中,我被一个选项值所困,我希望得到用户提交的id&将我的id更新到我的数据库中。但我不能这样做。请帮我 我的编辑文件是

<?php $active="profile"; ?>
@extends('admin.dashboard')
@section('content')
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        User Profile
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">User profile</li>
      </ol>
    </section>

<!-- Main content -->
    <section class="content">
      <div class="row">
        <!-- left column -->
        <div class="col-md-10">
          <!-- general form elements -->
          <div class="box box-primary">
            <div class="box-header with-border">
              <h3 class="box-title">Quick Example</h3>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form action="{{ route('update') }}" role="form" method="post" enctype="multipart/form-data">
              <div class="box-body">
                <div class="form-group">
                  <label for="name">Name</label>
                  <input type="text" class="form-control" name="name" id="name" {{ $errors->has('name') ? 'class=has-error' : '' }} value="{{ Request::old('name') ? Request::old('name') : isset($doctor) ? $doctor->name : '' }}">
                </div>
                <div class="form-group">
                  <label for="institute">Institute Name</label>
                  <input type="text" class="form-control" name="institute" id="institute" {{ $errors->has('institute') ? 'class=has-error' : '' }} value="{{ Request::old('institute') ? Request::old('institute') : isset($doctor) ? $doctor->institute : '' }}">
                </div>

                <div class="form-group">
                  <label for="education">Degree</label>
                  <input type="text" class="form-control" name="education" id="education" {{ $errors->has('education') ? 'class=has-error' : '' }} value="{{ Request::old('education') ? Request::old('education') : isset($doctor) ? $doctor->education : '' }}">
                </div>

                <div class="form-group">
                  <label for="specialty">Specialty</label>
                  <input type="text" class="form-control" name="specialty" id="specialty" {{ $errors->has('specialty') ? 'class=has-error' : '' }} value="{{ Request::old('specialty') ? Request::old('specialty') : isset($doctor) ? $doctor->specialty : '' }}">
                </div>

                <div class="form-group">
                  <label for="phone">Mobile No.</label>
                  <input type="number" class="form-control" name="phone" id="phone" {{ $errors->has('phone') ? 'class=has-error' : '' }} value="{{ Request::old('phone') ? Request::old('phone') : isset($doctor) ? $doctor->phone : '' }}">
                </div>

                <div class="form-group">
                  <label for="time">Visiting Time</label>
                  <input type="text" class="form-control" name="time" id="time" {{ $errors->has('time') ? 'class=has-error' : '' }} value="{{ Request::old('time') ? Request::old('time') : isset($doctor) ? $doctor->time : '' }}">
                </div>

                <div class="form-group">
                  <label for="fee">Fee</label>
                  <input type="number" class="form-control" name="fee" id="fee" {{ $errors->has('fee') ? 'class=has-error' : '' }} value="{{ Request::old('fee') ? Request::old('fee') : isset($doctor) ? $doctor->fee : '' }}">
                </div>

                <div class="form-group">
                <label for="division">Division</label>


                  <select id="division_id">
                  @foreach($divisions as $division)
                    <option value="{{ $division->id }}">{{ $division->id}}</option>
                    @endforeach
                  </select>

                </div>

                <div >
                    <p>
                        {!! Form::label('Chose the picture:') !!}
                        {!! Form::file('pic') !!}
                    </p>

                </div>
              </div>
              <!-- /.box-body -->

              <div class="box-footer">
                <button type="submit" class="btn btn-primary">Submit</button>
                <input type="hidden" name="_token" value="{{ Session::token() }}">
                <input type="hidden" name="id" value="{{ $doctor->id }}">
              </div>
            </form>
          </div>
          <!-- /.box -->

        </div>
        <!--/.col (left) -->
      </div>
      <!-- /.row -->
    </section>
    <!-- /.content -->
@endsection    

我的控制器

public function getUpdate($id) {
        $divisions = Division::all();

        $profile = Auth::user();
        $doctor=Doctor::find($id);

        // $post = Post::find($post_id);
        // if(!$post) {
        //     return redirect()->route('blog.index')->with(['fail' => 'Post Not Found']);
        // }
        return view('admin.article.edit')
        ->with('divisions',$divisions)
        ->with('profile',$profile)
        ->with('doctor',$doctor);
    }

    public function postUpdate(Request $request ) {

        $this->validate($request, [
            'name' => 'required|max:120',
            'fee' => 'required|max:5',
            'division_id' => '',
            'district_id' => '',
            'dcategory_id' => '',
            'education' => '',
            'institute' => '',
            'specialty' => '',
            'hospital' => '',
            'time' => '',
            'phone' => ''

            ]);

        $doctor = Doctor::find($request['id']);
        $doctor->name = $request['name'];
        $doctor->division_id = $request['division_id'];
        $doctor->district_id = $request['district_id'];
        $doctor->dcategory_id = $request['dcategory_id'];
        $doctor->institute = $request['institute'];
        $doctor->education = $request['education'];
        $doctor->specialty = $request['specialty'];
        $doctor->hospital = $request['hospital'];
        $doctor->phone = $request['phone'];
        $doctor->time = $request['time'];
        $doctor->fee = $request['fee'];

        // $file = Input::file('pic');
        // $img = Image::make($request->file('pic')->getRealPath());
        // //$img = Image::make($file);
        // Response::make($img->encode('jpeg'));

        // $doctor->pic = $img;
        $doctor->update();

        return redirect()->route('profile')->with(['success' => 'Profile Successfully Updated']);

    }

请帮我解决这个问题。提前致谢

1 个答案:

答案 0 :(得分:0)

您应该在视图文件中写下这个

<?php $active="profile"; ?>
@extends('admin.dashboard')
@section('content')
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        User Profile
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">User profile</li>
      </ol>
    </section>

<!-- Main content -->
    <section class="content">
      <div class="row">
        <!-- left column -->
        <div class="col-md-10">
          <!-- general form elements -->
          <div class="box box-primary">
            <div class="box-header with-border">
              <h3 class="box-title">Quick Example</h3>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form action="{{ route('update') }}" role="form" method="post" enctype="multipart/form-data">
              <div class="box-body">
                <div class="form-group">
                  <label for="name">Name</label>
                  <input type="text" class="form-control" name="name" id="name" {{ $errors->has('name') ? 'class=has-error' : '' }} value="{{ Request::old('name') ? Request::old('name') : isset($profile) ? $profile->name : '' }}">
                </div>
                <div class="form-group">
                  <label for="institute">Hospital Name</label>
                  <input type="text" class="form-control" name="institute" id="institute" {{ $errors->has('institute') ? 'class=has-error' : '' }} value="{{ Request::old('institute') ? Request::old('institute') : isset($profile) ? $profile->institute : '' }}">
                </div>

                <div class="form-group">
                  <label for="education">Degree</label>
                  <input type="text" class="form-control" name="education" id="education" {{ $errors->has('education') ? 'class=has-error' : '' }} value="{{ Request::old('education') ? Request::old('education') : isset($profile) ? $profile->education : '' }}">
                </div>

                <div class="form-group">
                  <label for="specialty">Specialty</label>
                  <input type="text" class="form-control" name="specialty" id="specialty" {{ $errors->has('specialty') ? 'class=has-error' : '' }} value="{{ Request::old('specialty') ? Request::old('specialty') : isset($profile) ? $profile->specialty : '' }}">
                </div>

                <div class="form-group">
                  <label for="phone">Mobile No.</label>
                  <input type="number" class="form-control" name="phone" id="phone" {{ $errors->has('phone') ? 'class=has-error' : '' }} value="{{ Request::old('phone') ? Request::old('phone') : isset($profile) ? $profile->phone : '' }}">
                </div>

                <div class="form-group">
                  <label for="time">Visiting Time</label>
                  <input type="text" class="form-control" name="time" id="time" {{ $errors->has('time') ? 'class=has-error' : '' }} value="{{ Request::old('time') ? Request::old('time') : isset($profile) ? $profile->time : '' }}">
                </div>

                <div class="form-group">
                  <label for="fee">Fee</label>
                  <input type="number" class="form-control" name="fee" id="fee" {{ $errors->has('fee') ? 'class=has-error' : '' }} value="{{ Request::old('fee') ? Request::old('fee') : isset($profile) ? $profile->fee : '' }}">
                </div>

                <div class="form-group">
                <label for="division">Division</label>


                  <select name="division_id">
                  @foreach($divisions as $division)
                    <option value="{{ $division->id }}">{{ $division->name}}</option>
                    @endforeach
                  </select>

                </div>

                <div class="form-group">
                <label for="district">District</label>


                  <select name="district_id">
                  @foreach($districts as $district)
                    <option value="{{ $district->id }}">{{ $district->name}}</option>
                    @endforeach
                  </select>

                </div>

                <div class="form-group">
                <label for="dcategory">Category</label>


                  <select name="dcategory_id">
                  @foreach($dcategories as $dcategory)
                    <option value="{{ $dcategory->id }}">{{ $dcategory->name}}</option>
                    @endforeach
                  </select>

                </div>

                <div class="form-group">
                  <label for="image">Chose the picture</label>
                  <input type="file" class="form-control" name="image" id="image" {{ $errors->has('image') ? 'class=has-error' : '' }} value="{{ Request::old('image') ? Request::old('image') : isset($photo) ? $photo->image : '' }}">
                </div>
              </div>
              <!-- /.box-body -->

              <div class="box-footer">
                <button type="submit" class="btn btn-primary">Submit</button>
                <input type="hidden" name="_token" value="{{ Session::token() }}">
                <input type="hidden" name="id" value="{{ $profile->id }}">
              </div>
            </form>
          </div>
          <!-- /.box -->

        </div>
        <!--/.col (left) -->
      </div>
      <!-- /.row -->
    </section>
    <!-- /.content -->
@endsection   

,您的控制器将

public function getUpdate() {
        $divisions = Division::all();
        $districts = District::all();
        $dcategories = Dcategory::all();
        $profile = Auth::user();

        return view('admin.article.edit')
        ->with('divisions',$divisions)
        ->with('districts',$districts)
        ->with('dcategories',$dcategories)
        ->with('profile',$profile);
    }

    public function postUpdate(Request $request ) {

        $this->validate($request, [
            'name' => 'required|max:120',
            'fee' => 'required|max:5',
            'division_id' => '',
            'district_id' => '',
            'dcategory_id' => '',
            'education' => '',
            'institute' => '',
            'specialty' => '',
            'hospital' => '',
            'time' => '',
            'phone' => '',
            'image' =>''            
            ]);


        $doctor = Auth::user();
        $doctor->name = $request['name'];
        $doctor->division_id = $request['division_id'];
        $doctor->district_id = $request['district_id'];
        $doctor->dcategory_id = $request['dcategory_id'];
        $doctor->institute = $request['institute'];
        $doctor->education = $request['education'];
        $doctor->specialty = $request['specialty'];
        $doctor->hospital = $request['hospital'];
        $doctor->phone = $request['phone'];
        $doctor->time = $request['time'];
        $doctor->fee = $request['fee'];
        $logo=$request->file('image');
        if(!empty($logo)) {
            $upload='uploads/logo';
            $filename=$logo->getClientOriginalName();
            $success=$logo->move($upload,$filename);

            $doctor->image = $filename;
        }
        $doctor->update();


        return redirect()->route('profile')->with(['success' => 'Profile Successfully Updated']);

    }

希望这可以解决您的问题