Bootstrap Item management
Item management is the most advanced example included in the Pro theme, because every item has a picture, belongs to a category and has multiple tags. To access this example click the “Laravel Examples/Item Management” link in the left sidebar or add /laravel-examples/item-management
to the URL. Here you can manage the items. A list of items will appear once you start adding them (to access the add page click “Add item”). On the add page, besides the Name and Description fields (which are present in most of the CRUD examples) you can see a category dropdown, which contains the categories you added, a file input and a tag multi select. If you did not add any categories or tags, please go to the corresponding sections (category management, tag management) and add some.
The store used for roles form is found in app/Http/Controllers/ItemController
.
You can find the functions for form functionality in resources/views/laravel-examples/item
folder.
Item Controller
public function store(Request $request)
{
$request->validate([
'name' => 'required|min:3|max:255',
'category_id' => 'required|exists:categories,id',
'photo' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'description' => 'required|min:3|max:255',
'tag_id' => 'required',
'status' => 'required',
'options' => 'required|array',
'date' => 'required|date',
], [
'name.required' => 'Name is required',
'category_id.required' => 'Category is required',
'status.required' => 'Status is required',
'tag_id.required' => 'Tag is required',
'description.required' => 'Description is required',
'options.required' => 'Options are required',
'date.required' => 'Date is required',
'description.min' => 'Description must be at least 3 characters',
]);
$picture = $request->file('photo');
$photoPath = $picture ? '/storage/' . $picture->store('item', 'public') : '';
$tags_id = $request->input('tag_id');
$item = Item::create([
'name' => $request->input('name'),
'category_id' => $request->input('category_id'),
'photo' => $photoPath,
'description' => $request->input('description'),
'status' => $request->input('status'),
'options' => json_encode($request->input('options')),
'date' => $request->input('date'),
]);
sort($tags_id);
$item->tags()->sync($tags_id, false);
return redirect(route('item-management'))->with('success', 'New item added successfully.');
}