Welcome ! I made an simple app where user has it's own notes. I'm using foreign key and relationships ( belongsTo, hasMany). When i add new note and choose to which user it belongs it's ok, but when i try to update it change all besides user.
Pilot model:
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);
}
}
Note model:
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 ( store and edit function)
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]);
}
Edit blade:
<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=""><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>
It changes everything besides pilot to whom belongs note.
Regards and thank you for help
via tomczas