I am having an issue with some small feature of a store function, and it seems that I didn't find any proper answer on the internet that will work for me.
I want to add static data in the array request, so that when sent data the field checked in the pivot table will be automatically filled with some integer.
I have the following tables:
Schema::create('mounter_evaluations', function (Blueprint $table) {
$table->increments('id');
$table->integer('project_id')->index()->unsigned()->nullable();
$table->text('mentions')->nullable();
$table->softDeletes();
$table->timestamps();
});
Schema::create('mounter_procedures', function (Blueprint $table) {
$table->increments('id');
$table->integer('mounter_norm_id')->index()->unsigned()->nullable();
$table->string('detail');
$table->softDeletes();
$table->timestamps();
});
Schema::create('mounter_procedures_checks', function (Blueprint $table) {
$table->increments('id');
$table->integer('mounter_evaluation_id')->unsinged()->nullable()->index();
$table->integer('mounter_procedure_id')->unsinged()->nullable()->index();
$table->integer('checked')->unsinged()->nullable();
$table->timestamps();
});
The following models:
public function mounterevaluation() {
return $this->belongsToMany(
'App\MounterEvaluation',
'mounter_procedures_checks',
'mounter_evaluation_id',
'mounter_procedure_id'
)->withPivot(['checked']);
}
public function mounterprocedures() {
return $this->belongsToMany(
'App\MounterProcedures',
'mounter_procedures_checks',
'mounter_evaluation_id',
'mounter_procedure_id'
)->withPivot(['checked']);
}
Here is my store function:
public function store(Request $request, $idProiect) {
$evaluation = MounterEvaluation::where('project_id', $idProiect)->first();
if (!$evaluation) {
$evaluation = new MounterEvaluation();
$evaluation->project_id = $idProiect;
}
$evaluation->mentions = $request->mentions;
$evaluation->save();
// $request->request->add(['checked' => 1]);
// $evaluation->mounterprocedures()->sync($request->procedures);
// $evaluation->mounterprocedures()->sync($request->all() + ['checked' => 1]);
$evaluation->mounterprocedures()->sync($request->procedures);
// $request->request->add(['checked' => 1]);
$evaluation->mounterprocedures()->request->add(['checked' => 1]);
return response()->json($evaluation, 201);
}
Every time a person checks some filed in the evaluation form, this check is to be stored in the pivot table.
Put it in the $request
array the simple way as you put values in any other array like so:
$request['new_key1'] = 'new value 1';
$request['new_key2'] = 'new value 2';
Here is how I know it works for sure, in your controller you have Request instance, passed as parameter and then you can add values normally like so:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
public function example(Request $request)
{
$request['new_key1'] = 'new value 1';
$request['new_key2'] = 'new value 2';
dd($request->input('new_key1'));//new value 1
}
In your case, if $request->procedures
is an array then you could do:
$request->procedures = $request->procedures->toArray();
$request->procedures['checked'] = 1;
$evaluation->mounterprocedures()->sync($request->procedures);
I hope it helps