Items Management

All three types of users have access to a tags management table page.


Item Management is the most advanced example included in the PRO theme because every item has a picture, has a category and has multiple tags. The item management can be accessed by clicking Items Management from the Laravel Examples section of the sidebar or adding /laravel-items-management in the url. The authenticated user as an Admin or Creator is able to add, edit and delete items. For adding a new item you can press the + New Item button or add in the url /laravel-new-item. If you would like to edit or delete a tag you can click on the Action column. It is also possible to sort the fields or to search in the fields.

On the page for adding a new item you will find a form which allows you to add an image of the item, to fill the name, excerpt, description of the item, a dropdown to choose the category and a multiselect for the tags.

The App\Http\Controllers\ItemsController.php handles data validation when adding a new item and the item creation(see snippet below):

Copy

                    public function store(Request $request)
                    {
                        if(!empty($request->file('item_img'))) {
                            $uniqueFileName = uniqid().$request->file('item_img')->getClientOriginalName();
                            $request->file('item_img')->move(public_path('/assets/img/items/'),$uniqueFileName);
                        }
                        else{
                            $uniqueFileName = 'default.jpg';
                        }

                        $attributes = request()->validate([
                            'name' => ['required', 'max:50'],
                            'excerpt' => ['max:150'],
                            'category_id' => ['required'],
                            'tag_id' => ['required'],
                            'description' => ['max:200'],
                        ]);
                        $request->all();

                        $optionList = $request->input('option');
                        $tags = $request->input('tag_id');

                        $items = new Item();
                            $items->name = $attributes['name'];
                            $items->file = $uniqueFileName;
                            $items->excerpt = $attributes['excerpt'];
                            $items->description = $request->get('description');
                            $items->category_id = $attributes['category_id'];
                            $items->status = $request->get('status');
                            $items->show_homepage = $request->get('show_homepage');
                            $items->options = $request->get('option');
                            $items->date = $request->get('date');
                            $items->created_at = now();
                            $items->updated_at = now();
                            $items->options = json_encode($optionList);
                            $items->save();

                            $items->tags()->attach($tags);
                            return redirect('/laravel-items-management');
                    }
                    
                  

For deleting an item is necessary to remove the association between item and tags. The App\Http\Controllers\ItemsController.php handles the deletion of an item:

Copy

                    public function destroy($id)
                    {
                        DB::table('items_management')->where('id', $id)->delete();
                        return redirect('/laravel-items-management');
                    }