2016-09-22 51 views
0

我不能編輯的即時通訊在我的控制器創建這樣一個項目:廣東話執行UPDATE操作 - 編輯項目

public function newProject(Request $request) 
    { 
     $data = $request->all(); 

     $attributes = []; 
     $attributes['title'] = $data['title']; 
     $attributes['start_date'] = date("Y-m-d h:i:s", strtotime($data['start_date'])); 
     $attributes['end_date'] = date("Y-m-d h:i:s", strtotime($data['end_date'])); 
     $attributes['created_by'] = Auth::user()->id; 
     $attributes['description'] = $data['description']; 
     $attributes['air'] = '10'; 
     $attributes['water'] = '19'; 
     $attributes['lat'] = $data['lat']; 
     $attributes['lng'] = $data['lng']; 


//  var_dump($attributes); 
//  return; 

     $project = Projects::create($attributes); 

     if($project) 
      return redirect('home')->with('success', 'Project added successfully'); 

     var_dump($data); 
     return; 
    } 

現在,我想編輯我上述功能創建的項目。我嘗試了相同的步驟,只是對路線進行了更改:

Route::post('projects/new', [ 
    'uses' => '[email protected]', 
    'as' => 'projects.new', 
]); 

注意:這是我創建項目的函數和路由。任何提示如何編輯?

UPDATE:

我的刀片模板文件

@extends('layouts.app') 

@section('content') 
    <div class="container"> 

     <div class="row"> 
      <div class="col-md-12"> 
       <div class="panel panel-default"> 
        <div class="panel-heading">Add Project</div> 


        <div class="panel-body"> 
         <form class="form-horizontal" role="form" method="POST" action="{{ route('projects.update') }}"> 
          {{ csrf_field() }} 

          <div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}"> 
           <label for="title" class="col-md-4 control-label">Title</label> 

           <div class="col-md-7"> 
            <input id="title" type="text" class="form-control" name="title" value="{{ $project->title }}"> 

            @if ($errors->has('title')) 
             <span class="help-block"> 
             <strong>{{ $errors->first('title') }}</strong> 
            </span> 
            @endif 
           </div> 
          </div> 

          <div class="form-group{{ $errors->has('date') ? ' has-error' : '' }}"> 
           <label for="email" class="col-md-4 control-label">Start Date</label> 

           <div class="col-md-7"> 
            <input id="date" type="date" class="form-control" name="start_date" value="{{ $project->start_date }}"> 

            @if ($errors->has('date')) 
             <span class="help-block"> 
             <strong>{{ $errors->first('date') }}</strong> 
            </span> 
            @endif 
           </div> 
          </div> 

          <div class="form-group{{ $errors->has('date') ? ' has-error' : '' }}"> 
           <label for="date" class="col-md-4 control-label">End Date</label> 

           <div class="col-md-7"> 
            <input id="date" type="date" class="form-control" name="end_date" value="{{ $project->end_date }}"> 

            @if ($errors->has('date ')) 
             <span class="help-block"> 
             <strong>{{ $errors->first('date') }}</strong> 
            </span> 
            @endif 
           </div> 
          </div> 


          <div class="form-group{{ $errors->has('text') ? ' has-error' : '' }}"> 
           <label for="email" class="col-md-4 control-label">Description</label> 
           <div class="col-md-7"> 
            <textarea class="form-control" rows="3" name="description" >{{ $project->description }} </textarea> 

            @if ($errors->has('email')) 
             <span class="help-block"> 
             <strong>{{ $errors->first('textarea') }}</strong> 
            </span> 
            @endif 
           </div> 
          </div> 

          <label class="col-md-7 control-label"></label> 

          <span> 
           <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample" > 
            Air 
           </a> 
          </span> 
          <span> 
           <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample2" aria-expanded="false" aria-controls="collapseExample2"> 
            Water 
           </a> 
          </span> 

          <span> 
           <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample3" aria-expanded="false" aria-controls="collapseExample"> 
            Location 
           </a> 
          </span> 



          <div class="collapse" id="collapseExample"> 
           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-4 control-label">Temperature</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="temperature" class="form-control" placeholder=" °C" value="{{$project->temperature}}"> 
             </div> 


            </div> 
           </div> 




           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-4 control-label">Radiation</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="radiation" class="form-control" placeholder=".col-xs-1" vlaue="{{$project->radiation}}"> 
             </div> 


            </div> 
           </div> 




           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-4 control-label">Dust</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="dust" class="form-control" placeholder=".col-xs-1" value="{{$project->dust}}"> 
             </div> 


            </div> 
           </div> 


           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-4 control-label">Noise</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="noise" class="form-control" placeholder=".col-xs-1" value="{{$project->noise}}"> 
             </div> 


            </div> 
           </div> 

          </div> 

          <p> </p> 

          <div class="collapse" id="collapseExample2"> 
           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Natrium</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="natrium" class="form-control" placeholder=".col-xs-1" value="{{$project->natrium}}"> 
             </div> 


            </div> 
           </div> 




           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Phosporus</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="phosporus" class="form-control" placeholder=".col-xs-1" value="{{$project->phosphorus}}"> 
             </div> 


            </div> 
           </div> 



           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Arsinic</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="arsinic" class="form-control" placeholder=".col-xs-1" value="{{$project->arsinic}}"> 
             </div> 


            </div> 
           </div> 



           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Barium</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="barium" class="form-control" placeholder=".col-xs-1" value="{{$project->barium}}"> 
             </div> 


            </div> 
           </div> 


           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Cadmium</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="cadmium" class="form-control" placeholder=".col-xs-1" value="{{$project->cadium}}"> 
             </div> 


            </div> 
           </div> 


           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Mercury</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="mercury " class="form-control" placeholder=".col-xs-1" value="{{$project->mercury}}"> 
             </div> 


            </div> 
           </div> 


           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Selenium</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="selenium" class="form-control" placeholder=".col-xs-1" value="{{$project->selenium}}"> 
             </div> 


            </div> 
           </div> 



           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Nickel </label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="nickel" class="form-control" placeholder=".col-xs-1" value="{{$project->nickel}}"> 
             </div> 


            </div> 
           </div> 



           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Thallium</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="thallium" class="form-control" placeholder=".col-xs-1" value="{{$project->thallium}}"> 
             </div> 


            </div> 
           </div> 




           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Berillyum</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="berillyum" class="form-control" placeholder=".col-xs-1" value="{{$project->berillyum}}"> 
             </div> 


            </div> 
           </div> 




           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Phenols</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="phenols" class="form-control" placeholder=".col-xs-1" value="{{$project->phenols}}"> 
             </div> 


            </div> 
           </div> 



           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Oil</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="oil" class="form-control" placeholder=".col-xs-1" value="{{$project->oil}}"> 
             </div> 


            </div> 
           </div> 

           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Temperature</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="w_temperature" class="form-control" placeholder="°C" value="{{$project->w_temperature}}"> 
             </div> 


            </div> 
           </div> 


           <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}"> 
            <label for="text" class="col-md-7 control-label">Oxygen</label> 
            <div class="row"> 
             <div class="col-xs-1"> 
              <input type="text" name="oxygen" class="form-control" placeholder="mg/L" value="{{$project->oxygen}}"> 
             </div> 


            </div> 
           </div> 


          </div> 



          <label for="text" class="col-md-4 control-label">Location</label> 
          <div class="row"> 
           <div class="col-md-7"> 

            <h1>Location</h1> 
            <p></p> 

            <!--map div--> 
            <div id="map"></div> 


            <!--our form--> 
            <h2 class="text-left">Coordinates</h2> 
            <p> 
             <input class="form-control" type="text" id="lat" name="lat" value="{{$project->lat}}" /> 
             <input class="form-control" type="text" id="lng" name="lng" value="{{$project->lng}}"/> 
            </p> 


           </div> 


          </div> 



          <div class="form-group"> 
           <div class="col-md-7 col-md-offset-4"> 
            <button type="submit" class="btn btn-primary"> 
             <i class="fa fa-btn fa-user"></i> Add Project 
            </button> 
           </div> 
          </div> 
        </div> 
        </form> 
       </div> 
      </div> 
     </div> 
    </div> 



    </div> 

@endsection 
@section('footer') 

@endsection 
+1

您是否收到錯誤或什麼? – Samsquanch

回答

1

我會建議去簡單的方法

控制器功能

public function update($id, Request $request) 
{ 
    $data = $request->all(); 

    $project = Projects::findOrFail($id); 
    $project->update($data); 

    return redirect(/home); 

} 

路線

Route::patch('projects/{id}', [ // this is the project id 
    'uses' => '[email protected]', 
    'as' => 'projects.update', 
]); 

(假設你使用laravel集體形式)

{!! Form::model($project, [ 
    'method' => 'PATCH', 
    'url' => ['/project', $project->id], // this is where you submit the id of the project 
    'class' => 'form-horizontal', 
    'files' => true 
]) !!} 

       <div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}"> 
      {!! Form::label('name', 'Name', ['class' => 'col-sm-3 control-label']) !!} 
      <div class="col-sm-6"> 
       {!! Form::text('name', null, ['class' => 'form-control']) !!} 
       {!! $errors->first('name', '<p class="help-block">:message</p>') !!} 
      </div> 
     </div> 


    <div class="form-group"> 
     <div class="col-sm-offset-3 col-sm-3"> 
      {!! Form::submit('Update', ['class' => 'btn btn-primary form-control']) !!} 
     </div> 
    </div> 
{!! Form::close() !!} 

這是最好的方式去!除非你確定你想要什麼,否則我不會推薦使用相同的功能。

UPDATE

<form class="form-horizontal" role="form" method="POST" action="{{ url('projects', $project->id) }}"> 

    {{ csrf_field() }} 
<input name="_method" type="hidden" value="PATCH"> // add this 
// here you put the rest of the form 

</form> 

,如果你沒有這個$project變量可用 比請告訴我,顯示編輯頁面 ,因爲我需要知道你將如何通過項目的ID的功能你想要更新(用戶輸入與否)

+0

這正是我所期待的。但我使用的是laravel blade模板引擎。 我在問題中添加了刀片模板文件@achraf Khouadja – dailyadd

+0

我將更新答案 –

+0

好吧,我會等待 – dailyadd

0

你可以做到這一切在一個函數:

public function storeOrUpdate(Request $request) { 
    try{ 
     DB::transaction(function() use($request) { 
      Projects::firstOrNew([ 
       'id' => $request->has('id') ? $request->get('id') : null 
       'title' => $request->get('title'), 
       'start_date' => $request->has('start_date') ? Carbon\Carbon::parse($request->get('start_date')) ? null, 
       'end_date' => $request->has('end_date') ? Carbon\Carbon::parse($request->get('end_date')) ? null, 
       'created_by' => auth()->user()->id, 
       'description' => $request->get('description'), 
       'air' => 19, 
       'water' => 10, 
       'lat' => $request->get('lat'), 
       'lng' => $request->get('lng') 
      ]); 
     }); 

     DB::commit(); 
     return redirect('home')->with('Success', 'Project added successfully'); 
    } catch (\Exception $e){ 
     DB::rollback(); 

     return redirect()->back()->withErrors(collect($e->getMessage())->toArray); 
    } 
});