如何更新其他 table 中的状态?

How to update status in other table?

我有两个 table inspectionsrequisitions.

在请购单中 table 有一个字段 status 和默认值 0。我想当用户按下提交 status 更新为 1.

检查table

public function up()
{
    Schema::create('inspections', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('requisition_id')->unsigned();
        $table->string('encouragement_data')->nullable(true);
        $table->text('infraction_data')->nullable(true);
                $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->foreign('requisition_id')->references('id')->on('requisitions')->onDelete('cascade');
    });
}

请购单table

public function up()
{
    Schema::create('requisitions', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('school_id')->unsigned();
        $table->integer('user_id')->unsigned();
        $table->integer('type');
        $table->boolean('status')->default(0);
        $table->string('date');
        $table->boolean('approved');
                $table->timestamps();

        $table->foreign('school_id')->references('id')->on('schools')->onDelete('cascade');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

InspectionController.php

public function store(Request $request)
{
    $inspection = new Inspection();
    $inspection->infraction_data = $request->infractions;
    $inspection->encouragement_data = $request->encouragement;
    $inspection->user_id = auth()->user()->id;
    $inspection->requisition_id = 1;
    $inspection->update()->requisitions->staus = 1;
    $inspection->save();
    return redirect()->route('inspection-results.index');
}

Inspection.php

public function requisitions()
{
    $this->belongsTo(Requisition::class);
}

在blade.php

将以下代码段设置为用户提交按钮

@foreach($tasks as $task)
<a href="/status/{{$task_>id}}" class="btn-btn-primary">submit</a>
@endforeach

在web.php

Route::get('/status/{id},'InspectionController@updateStatus');

在InspectionController.php

创建以下方法。

public function updateStatus($id){
$task=requisitionsModelName::find($id);
$task->status=1;
$task->save();
return redirect()->back();
}

申请中table

$data=requisitionsModelName::all();
return view('relevantViewname')->with('tasks',$data);