在Laravel 6中编辑旋转表复选框数据,可以通过以下步骤实现:
下面是一个示例代码,演示如何在Laravel 6中编辑旋转表复选框数据:
php artisan make:migration create_items_table --create=items
public function up()
{
Schema::create('items', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('options')->nullable();
$table->timestamps();
});
}
php artisan migrate
php artisan make:model Item
namespace App;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
protected $fillable = ['name', 'options'];
public function getOptionsAttribute($value)
{
return explode(',', $value);
}
public function setOptionsAttribute($value)
{
$this->attributes['options'] = implode(',', $value);
}
}
php artisan make:controller ItemController
namespace App\Http\Controllers;
use App\Item;
use Illuminate\Http\Request;
class ItemController extends Controller
{
public function edit($id)
{
$item = Item::findOrFail($id);
return view('items.edit', compact('item'));
}
public function update(Request $request, $id)
{
$item = Item::findOrFail($id);
$item->update($request->all());
return redirect()->route('items.index');
}
}
resources/views/items/edit.blade.php
<form action="{{ route('items.update', $item->id) }}" method="POST">
@csrf
@method('PUT')
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" id="name" value="{{ $item->name }}" class="form-control">
</div>
<div class="form-group">
<label for="options">Options</label>
@foreach(['Option 1', 'Option 2', 'Option 3'] as $option)
<div class="form-check">
<input type="checkbox" name="options[]" id="{{ $option }}" value="{{ $option }}" class="form-check-input" {{ in_array($option, $item->options) ? 'checked' : '' }}>
<label for="{{ $option }}" class="form-check-label">{{ $option }}</label>
</div>
@endforeach
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
Route::resource('items', 'ItemController');
这样,当访问/items/{id}/edit
时,将展示编辑页面,用户可以修改旋转表复选框数据并保存。
领取专属 10元无门槛券
手把手带您无忧上云