I have been asked to make a configuration menu in which to display sections in the menu of my web page. So I have set to work and this has been achieved.
Well, the problem is that I need to perform the update option and since it is a multiplecheckbox I have doubts about how to do it. I attach my code files in case someone could throw me a cable.
Route:
use App\Http\Controllers\ConfigController;
Route::resource('configs', ConfigController::class);
Migration table:
public function up()
{
Schema::create('configs', function (Blueprint $table) {
$table->id();
$table->string('category');
$table->timestamps();
});
}
Model (Config.php)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Config extends Model
{
use HasFactory;
protected $fillable = ['category'];
public function setCategoryAttribute($value)
{
$this->attributes['category'] = json_encode($value);
}
public function getCategoryAttribute($value)
{
return $this->attributes['category'] = json_decode($value);
}
}
Controller (ConfigController.php):
<?php
namespace App\Http\Controllers;
use App\Models\Config;
use Illuminate\Http\Request;
class ConfigController extends Controller
{
//index , create,store
public function edit($id)
{
$config = Config::find($id);
return view('configs.config',compact('id'));
}
public function update(Request $request, $id)
{
//
}
}
As you can see here I need an update that I don't know very well how to do it.
Edit (config.blade.php) I enclose the form part:
<form method="post" action="{{ route('configs.update',$configs->id) }}" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label><strong>Category :</strong></label><br>
<label><input type="checkbox" name="category[]" value="contacto"> Contacto</label>
<label><input type="checkbox" name="category[]" value="tiempo"> El Tiempo</label>
<label><input type="checkbox" name="category[]" value="galeria"> Galeria</label>
<label><input type="checkbox" name="category[]" value="mercadillo"> Mercadillo</label>
<label><input type="checkbox" name="category[]" value="noticias"> Noticias</label>
<label><input type="checkbox" name="category[]" value="webcams"> Webcams</label>
</div>
<div class="form-group text-center">
<button type="submit" class="btn btn-success btn-sm">Save</button>
</div>
</form>
If any kind person could give me a cable I would greatly appreciate it since I don't know how to complete this.
Edition:
I have tried this in driver update:
public function update(Request $request, $id)
{
$conf = Config::find($id);
$conf->category = $request->get('category');
$conf->update();
return redirect('/configs')->with('success', 'menu web actualizado correctamente.');
}
and this route in the action of the form to edit:
<form action="{{ route('configs.update',$config->id) }}" method="POST">
But it gives the following error:
ErrorException
Undefined variable: config (View: C:\xampp\htdocs\ruta\resources\views\configs\config.blade.php)
Someone could help me? I've been with this for days and I don't really know how to do it, I've searched on google, on youtube, I don't know what else to do, I can't find anything about this.
If more code is required I attach it without problem.
What happens is you are not passing the parameters correctly or executing the correct method
first you must pass the
config
or use directlyid
In addition, because we are modifying, it is very likely that there will be data, and we load these with
{{in_array($config->category,"contacto") ? "checked":""}}
is executed
save
to save, assuming the modelConfig
has a$casts = ["category" => "array" ]
From what I see, your only problems would be the following:
In your edit, you're passing it a variable called
id
, noconfig
. So you should change that:What you need, as I understood from your comment, is to join both arrays into one, so you can use
array_merge()
:You can choose to do the
array_merge
here or in the mutator you have.The only thing this will do is add whatever you select to the view. Obviously, to delete, you will need other logic, but it all boils down to handling arrays.