使用Laravel Eloquent

时间:2018-11-27 10:23:06

标签: php laravel

我有一个calls table,是从表单填充的,

通话表     

public function up()
{
    Schema::create('calls', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned()->nullable();
        $table->timestamps();
        $table->text('terminal_id', 20);
        $table->text('terminal_name', 100);
        $table->text('fault_description');
        $table->string('call_status', 10)->default('New call');
        $table->text('assigned_FE', 20)->nullable();
        $table->text('closed_on', 20)->nullable();
        $table->text('closed_by', 50)->nullable();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

我只希望使用此模态表单上的用户输入来获取和更新assigned_FE中的calls table

<div class="modal-body">
    <form action="{{route('Call.update')}}" method="POST" style="padding:30px 0px">

        @csrf
        <div class="col-md-6">
            <div class="input-group" style="width: 100%;">
                <label for="assigned_FE">{{ __('Name of field engineer') }}</label><br>
                <input type="text" name="assigned_FE" id="assigned_FE" class="form-control" placeholder="Name of field engineer" style="padding: 20px;" required>
            </div>
        </div>
        <button type="submit" class="btn-primary" style="padding: 10px; font-size: 14px; border: 0; margin-top:25px">{{ __('Submit') }}</button>
    </form>
</div>

如何在不获取呼叫行中所有数据的情况下实现这一目标? 我不知道在CallsController中放置什么内容 这是我的CallsController

public function edit($id)
{
//find the call in the db and save it as a variable
$call = Call::find($id);
//return it to the view and pass in the variable


}

/**
* Update the specified resource in storage.
*
* @param  \Illuminate\Http\Request  $request
* @param  int  $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

这是我的通话模式(Call.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Call extends Model{

    protected $fillable = [
        'terminal_id',
        'terminal_name',
        'branch_address',
        'fault_description'
      ];
    //describing a one-to-many-relationship between calls and users
    public function user(){

        return $this->belongsTo('App\User');
}
}

3 个答案:

答案 0 :(得分:1)

好吧,首先在您的模型中,应将所有列名称都包括在您的呼叫模型丢失的已填充数组中。

class Call extends Model{

protected $fillable = [
    'terminal_id',
    'terminal_name',
    'branch_address',
    'atm_variant',
    'assigned_FE',
    'closed_on',
    'closed_by',
    'fault_description'
  ];
//describing a one-to-many-relationship between calls and users
public function user(){

    return $this->belongsTo('App\User');

}

在确保所有列都存在于可填充数组中之后,将您的“表单”操作更改为指向call.update路由

<div class="modal-body">
<form action="{{route('call.update')}}" method="POST" style="padding:30px 0px">

    @csrf
    <div class="col-md-6">
        <div class="input-group" style="width: 100%;">
            <label for="assigned_FE">{{ __('Name of field engineer') }}</label><br>
            <input type="text" name="assigned_FE" id="assigned_FE" class="form-control" placeholder="Name of field engineer" style="padding: 20px;" required>
        </div>
    </div>
    <button type="submit" class="btn-primary" style="padding: 10px; font-size: 14px; border: 0; margin-top:25px">{{ __('Submit') }}</button>
</form>

您的控制器

use App\Call;
public function update(Request $request, $id)
{
$assigned_FE = $request->assigned_FE;
$call = Call::findOrFail($id); 
$call->assigned_FE = $assigned_FE;
$call-save();

return redirect()->back();

}

您的路线文件应具有以下内容:

route::post('call/{id}', CallsController@update)->name('call.update);

答案 1 :(得分:0)

如果我想了解您的问题,您就去这里!

public function update(Request $request, $id)
{
  if($request->isMethod('post')){
  $data = $request->all();

  //this part will get your item with that specific id updated just match database columns with form input names.
  call::where(['id'=> $id])
  ->update(['terminal_name'=> $data['terminal_name'], 'fault_description' =>$data['fault_description']]);

  //redirected to another page after updating or
  //return back(); to stay on same page.
  return redirect('/page');
  }
}

答案 2 :(得分:0)

update函数中获取所需的记录,然后对其进行更新。

public function update(Request $request, $id)
{
    $call = Call::find($id);

    $input = $request->all();

    $call->assigned_FE = $input['assigned_FE'];
    $call->update();

    // redirect wherever you want
    return redirect()->back();
}