简体   繁体   English

laravel - 数据库不更新

[英]laravel - database not updating

Welcome ! 欢迎! I made an simple app where user has it's own notes. 我做了一个简单的应用程序,用户有自己的笔记。 I'm using foreign key and relationships ( belongsTo, hasMany). 我正在使用外键和关系(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 edit and update function) Notes控制器(存储编辑和更新功能)

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

}

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="{{ 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>

It changes everything besides pilot to whom belongs note. 除了飞行员之外,它改变了所有的东西。

Regards and thank you for help 关心并感谢您的帮助

In your update function you are not getting the note object. 在您的更新功能中,您没有获得注释对象。 You do 你做

Note::find($id);

Do this instead 这样做

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

Also, your view, you open the Form twice, once with Form::model() (you don't close) and later on with Form::open() . 此外,您的视图,您打开表单两次,一次使用Form::model() (您不关闭),稍后使用Form::open()

You need to close each form separately with 您需要单独关闭每个表单

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

Moreover, you defined the select only once, inside of form::open. 而且,你只在form :: open中定义了一次select You need to add it also inside Form::model(). 您还需要在Form :: model()中添加它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM