欢迎!我做了一个简单的应用程序,用户有自己的笔记。我正在使用外键和关系(belongsTo,hasMany)。当我添加新笔记并选择它属于哪个用户时,没关系,但是当我尝试更新它时,除了用户之外更改所有用户。
试点模式:
class Pilot extends Model
{
protected $table = 'pilots';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'phone', 'email',
];
public function note() {
return $this->hasMany(Note::class);
}
}
注意型号:
class Note extends Model
{
protected $table = 'notes';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'data', 'zadanie', 'uwagi', 'pilot_id',
];
public function pilot() {
return $this->belongsTo(Pilot::class);
}
}
Notes Controller(商店编辑和更新功能)
public function store(Request $request)
{
$this->validate($request, [
'data' => 'required',
'zadanie' => 'required',
'uwagi' => 'required',
]);
$note = new Note ();
$note->data = $request->data;
$note->zadanie = $request->zadanie;
$note->uwagi = $request->uwagi;
$note->pilot_id = $request->pilots;
// Commit to the database
$note->save();
return redirect()->route('uwagi.index')
->with('success','Uwagi dodane poprawnie');
}
public function edit($id)
{
$note = Note::find($id);
$pilots = Pilot::all();
return view('uwagi.edit',['note'=>$note, 'pilots'=>$pilots]);
}
public function update(Request $request, $id)
{
$this->validate($request, [
'data' => 'required',
'zadanie' => 'required',
'uwagi' => 'required',
'pilot_id' =>'required',
]);
Note::find($id);
$note->data = $request->data;
$note->zadanie = $request->zadanie;
$note->uwagi = $request->uwagi;
$note->pilot_id = $request->pilots;
// Commit to the database
$note->save();
return redirect()->route('uwagi.index')
->with('success','Zaktualizowano poprawnie');
}
编辑刀片:
<div class="panel panel-transparent">
{!! Form::model($note, ['method' => 'PATCH','route' => ['uwagi.update', $note->id]]) !!}
<div class="panel-heading">
<div class="panel-title">Dodaj Uwagę
</div>
</div>
<div class="panel-body">
<h3>Wybierz ucznia oraz dodaj do niego uwagę</h3>
<p>Wszystkie pola są wymagane</p>
<br>
<div>
</div>
</div>
</div>
</div>
<div class="col-sm-7">
<div class="panel panel-transparent">
<div class="panel-body">
{!! Form::open(array('route' => 'uwagi.store','method'=>'POST')) !!}
<p>Dane</p>
<label>Pilot</label>
<select class="form-control" id="pilot" name="pilots">
@foreach($pilots as $pilot)
<option value="{!! $pilot->id !!}">
{!! $pilot->name !!}
</option>
@endforeach
</select>
</div>
<div class="row clearfix">
<div class="col-sm-6">
<div class="form-group form-group-default required">
<label>Data</label>
{!! Form::date('data', null, array('placeholder' => 'Data','class' => 'form-control ')) !!}
</div>
</div>
<div class="col-sm-6">
<div class="form-group form-group-default required">
<label>Zadanie</label>
{!! Form::text('zadanie', null, array('placeholder' => 'Zadanie','class' => 'form-control ')) !!}
</div>
</div>
<div class="col-sm-6">
<div class="form-group form-group-default required">
<label>Uwagi</label>
{!! Form::textarea('uwagi', null, array('placeholder' => 'Uwagi','class' => 'form-control ')) !!}
</div>
</div>
</div>
</div>
<br>
<button class="btn btn-success btn-cons m-b-10" type="submit"><i class="fa fa-floppy-o" aria-hidden="true"></i> <span class="bold">Dodaj</span></button>
<a href="{{ route('pilot') }}"><button class="btn btn-info btn-cons m-b-10" type="button"><i class="fa fa-arrow-left"></i> <span class="bold">Powrót</span>
</button></a>
{!! Form::close() !!}
</div>
它改变了除了飞行员之外的所有内容。
关心并感谢您的帮助
答案 0 :(得分:0)
在您的更新功能中,您没有获得注释对象。你做了
Note::find($id);
改为
$note = Note::find($id);
$note->pilot_id = $request->pilots;
$note->save(); // $note->update();
另外,您的观点是,您打开表单两次,一次使用Form::model()
(您没有关闭),稍后再使用Form::open()
。
您需要使用
分别关闭每个表单{!! Form::close() !!}
此外,您只在form :: open中定义了select
一次。您还需要在Form :: model()中添加它。
<select class="form-control" id="pilot" name="pilots">