为什么在使用模态编辑数据时,更改所有 ID?
why when editing data using modal, change all IDs?
为什么编辑一个号码时所有id都被调用
控制器
public function update(Request $request, $id)
{
$spent_time = SpentTime::find($id);
$plan = $spent_time->plan;
$total_spent_times = $plan->spent_times()->where('task_category', $spent_time->task_category)->sum('spent_time');
$request['spent_time'] = (int)$total_spent_times + (int)$request->get('daily_spent_time');
$total_percentage = $plan->spent_times()->where('task_category', $spent_time->task_category)->sum('percentage');
$request['percentage'] = (int)$total_percentage + (int)$request->get('daily_percentage');
$spent_time->update($request->all());
return redirect()->route('real.index', compact('spent_time','plan','total_spent_time','total_percentage'));
}
index.blade.php
<td>
<a href="" class="edit_real" data-toggle="modal" data-target="#edit_real-{{$spent_time->plan_id}}">
{{$spent_time->plan->user_story}}
</a>
@include('reals._form')
</td>
_form.blade.php
<div class="modal fade" id="edit_real-{{$spent_time->plan_id}}" role="dialog" >
<div class="modal-dialog modal-sm-8">
<div class="modal-content">
<div class="nav-tabs-custom">
<ul class="nav nav-tabs pull-left">
<li class=""><a href="{{url('plan')}}">Plan</a></li>
<li class="active"><a href="{{url('real')}}">Real</a></li>
</ul>
<div class="tab-content">
<div class="chart tab-pane active" id="revenue-chart">
{!! Form::open([$spent_time, 'url' => route('real.update', $spent_time->id), 'method' => 'POST', 'role'=>'form']) !!}
{{ csrf_field() }} {{ method_field('PUT') }}
<div class="box-body">
<div class="form-group">
{!! Form::label('user_story','Today Plan *', ['class' => 'control-label', 'name'=>'user_story']) !!}</label>
{!! Form::text('user_story', old('plan_id', is_null($spent_time->plan->user_story) ? null : $spent_time->plan->user_story),
['class'=>'form-control', 'readonly' => 'true']) !!}
</div>
<div class="form-group">
{!! Form::label('daily_spent_time','Spent Time *', ['class' => 'control-label', 'name'=>'daily_spent_time']) !!}</label>
{!! Form::text('daily_spent_time', old('daily_spent_time', $spent_time->daily_spent_time ?: null),
['class'=>'form-control', 'id'=>'daily_spent_time']) !!}
</div>
<div class="form-group">
{!! Form::label('daily_percentage','% Done *', ['class' => 'control-label', 'name' => 'daily_percentage']) !!}</label>
{!! Form::text('daily_percentage', old('daily_percentage', $spent_time->daily_percentage ?: null),
['class'=>'form-control', 'id'=>'daily_percentage']) !!}
</div>
<div class="form-group">
{!! Form::label('reason','Reason', ['class' => 'control-label', 'name'=>'reason']) !!}</label>
{!! Form::textarea('reason', old('reason', $spent_time->reason ?: null), ['class'=>'form-control', 'id'=>'reason']) !!}
</div>
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-block btn-primary btn-flat', 'type'=>'submit']) !!}
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script type="text/javascript">
// jQuery
$('.edit_real').on('click', function (event) {
event.preventDefault();
$('#edit_real').modal();
});
初步数据,在2号编辑时,数据会保存到1号数据
为什么在使用模式编辑数据时,更改所有 ID?
有什么需要改进的?必须纠正的错误在哪里?为什么在编辑一个号码时调用所有 ID?
回答
add {!! Form::close() !!}
in _form.blade.php
控制器
public function edit($id)
{
$spent_times = SpentTime::all();
$user_story = Plan::pluck('user_story', 'id');
$spent_time = SpentTime::find($id);
return view('reals.edit', compact('spent_times', 'user_story', 'spent_time'));
}
public function update(Request $request, $id)
{
$spent_time = SpentTime::find($id);
$spent_time->update($request->all());
return redirect()->route('real.index', compact('spent_time'));
}
为什么编辑一个号码时所有id都被调用
控制器
public function update(Request $request, $id)
{
$spent_time = SpentTime::find($id);
$plan = $spent_time->plan;
$total_spent_times = $plan->spent_times()->where('task_category', $spent_time->task_category)->sum('spent_time');
$request['spent_time'] = (int)$total_spent_times + (int)$request->get('daily_spent_time');
$total_percentage = $plan->spent_times()->where('task_category', $spent_time->task_category)->sum('percentage');
$request['percentage'] = (int)$total_percentage + (int)$request->get('daily_percentage');
$spent_time->update($request->all());
return redirect()->route('real.index', compact('spent_time','plan','total_spent_time','total_percentage'));
}
index.blade.php
<td>
<a href="" class="edit_real" data-toggle="modal" data-target="#edit_real-{{$spent_time->plan_id}}">
{{$spent_time->plan->user_story}}
</a>
@include('reals._form')
</td>
_form.blade.php
<div class="modal fade" id="edit_real-{{$spent_time->plan_id}}" role="dialog" >
<div class="modal-dialog modal-sm-8">
<div class="modal-content">
<div class="nav-tabs-custom">
<ul class="nav nav-tabs pull-left">
<li class=""><a href="{{url('plan')}}">Plan</a></li>
<li class="active"><a href="{{url('real')}}">Real</a></li>
</ul>
<div class="tab-content">
<div class="chart tab-pane active" id="revenue-chart">
{!! Form::open([$spent_time, 'url' => route('real.update', $spent_time->id), 'method' => 'POST', 'role'=>'form']) !!}
{{ csrf_field() }} {{ method_field('PUT') }}
<div class="box-body">
<div class="form-group">
{!! Form::label('user_story','Today Plan *', ['class' => 'control-label', 'name'=>'user_story']) !!}</label>
{!! Form::text('user_story', old('plan_id', is_null($spent_time->plan->user_story) ? null : $spent_time->plan->user_story),
['class'=>'form-control', 'readonly' => 'true']) !!}
</div>
<div class="form-group">
{!! Form::label('daily_spent_time','Spent Time *', ['class' => 'control-label', 'name'=>'daily_spent_time']) !!}</label>
{!! Form::text('daily_spent_time', old('daily_spent_time', $spent_time->daily_spent_time ?: null),
['class'=>'form-control', 'id'=>'daily_spent_time']) !!}
</div>
<div class="form-group">
{!! Form::label('daily_percentage','% Done *', ['class' => 'control-label', 'name' => 'daily_percentage']) !!}</label>
{!! Form::text('daily_percentage', old('daily_percentage', $spent_time->daily_percentage ?: null),
['class'=>'form-control', 'id'=>'daily_percentage']) !!}
</div>
<div class="form-group">
{!! Form::label('reason','Reason', ['class' => 'control-label', 'name'=>'reason']) !!}</label>
{!! Form::textarea('reason', old('reason', $spent_time->reason ?: null), ['class'=>'form-control', 'id'=>'reason']) !!}
</div>
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-block btn-primary btn-flat', 'type'=>'submit']) !!}
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script type="text/javascript">
// jQuery
$('.edit_real').on('click', function (event) {
event.preventDefault();
$('#edit_real').modal();
});
初步数据,在2号编辑时,数据会保存到1号数据
为什么在使用模式编辑数据时,更改所有 ID?
有什么需要改进的?必须纠正的错误在哪里?为什么在编辑一个号码时调用所有 ID?
回答
add
{!! Form::close() !!}
in _form.blade.php
控制器
public function edit($id)
{
$spent_times = SpentTime::all();
$user_story = Plan::pluck('user_story', 'id');
$spent_time = SpentTime::find($id);
return view('reals.edit', compact('spent_times', 'user_story', 'spent_time'));
}
public function update(Request $request, $id)
{
$spent_time = SpentTime::find($id);
$spent_time->update($request->all());
return redirect()->route('real.index', compact('spent_time'));
}