2017-03-15 6 views
1

Willkommen! Ich habe eine einfache App erstellt, in der Benutzer eigene Notizen haben. Ich verwende Fremdschlüssel und Beziehungen (belongsTo, hasMany). Wenn ich neue Notiz hinzufüge und wähle, welchem ​​Benutzer es gehört, ist es ok, aber wenn ich versuche, es zu aktualisieren, ändern Sie alle außer Benutzer.Laravel - Datenbank wird nicht aktualisiert

Pilot Modell:

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); 
     } 
    } 

Hinweis Modell:

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); 
    } 
} 

Hinweise Controller (Speicher bearbeiten und Update-Funktion)

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'); 

} 

bearbeiten Klinge:

<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> 

Es ändert sich alles außer Pilot, wem die Notiz gehört.

Grüße und vielen Dank für die Hilfe

+0

Haben Sie Ihre zweite Form geschlossen? und ich glaube nicht, dass Sie die Methode angeben müssen, da sie "modellbindend" ist. –

+0

Wo ist Ihre Update-Funktion? – EddyTheDove

+0

@eddythedove Aktualisierung überprüfen – tomczas

Antwort

0

In Ihrer Update-Funktion Sie nicht die Notiz Objekt zu bekommen. Sie tun

Note::find($id); 

diese

$note = Note::find($id); 
$note->pilot_id = $request->pilots; 
$note->save(); // $note->update(); 

Auch stattdessen tun, Ihrer Meinung nach, können Sie das Formular zweimal öffnen, einmal mit Form::model() (Sie nicht schließen) und später mit Form::open().

Sie müssen jede Form separat mit

{!! Form::close() !!} 

Außerdem schließen, können Sie die select nur einmal definiert, innerhalb der Form :: offen. Sie müssen es auch in Form :: model() hinzufügen.

<select class="form-control" id="pilot" name="pilots"> 
Verwandte Themen