php - laravel - 数据库不更新

标签 php mysql laravel

欢迎!我做了一个简单的应用程序,用户有自己的笔记。我正在使用外键和关系(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');

}

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

它改变了除了飞行员之外的一切。

问候并感谢您的帮助

最佳答案

在您的更新功能中,您没有获得注释对象。你做

Note::find($id);

改为这样做

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

此外,在您的 View 中,您打开了两次表单,一次是使用 Form::model() (您没有关闭),然后是一次使用 Form::open().

您需要使用

分别关闭每个表单
{!! Form::close() !!}

此外,您只在 form::open 中定义了一次 select。您还需要在 Form::model() 中添加它。

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

关于php - laravel - 数据库不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42821664/

相关文章:

php - 在 MVC 中处理片段和 View

php - 无法从 PDO 数组获取值

MySQL删除所有未选中的记录

php - laravel 插入查询值空

javascript - Laravel 中 VueJS 组件实现中的鼠标悬停

php - 参数和选项有什么区别?

php - 如何将表转换为 TreeView ?

php - 让用户选择他们想要的模块

使用 Select、Count 和 Group By 的 MySQL 性能问题

mysql - 插入后触发sql