【问题标题】:laravel - database not updatinglaravel - 数据库未更新
【发布时间】:2017-08-06 21:28:14
【问题描述】:

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

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

问候并感谢您的帮助

【问题讨论】:

  • 您是否关闭了第二个表单?而且我认为您不需要指定方法,因为它是model-binding
  • 你的更新功能在哪里?
  • @eddythedove 检查更新

标签: php mysql laravel


【解决方案1】:

在您的更新函数中,您没有得到注释对象。你会的

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

【讨论】:

    猜你喜欢
    • 2020-03-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2012-08-29
    • 2019-03-10
    相关资源
    最近更新 更多