尝试向 CRUD 添加额外功能的各种错误

Various errors trying to add an extra function to CRUD

首先,我显然没有完全理解路由。这个问题与数据库无关,而是文件夹中图像的管理,但我使用 Resource 进程来创建 crud 元素。也许这不是文件管理的正确方法。

我有以下看法(非成品图,仅供测试):

@extends('layouts.app')
@section('content')
<!--/*{ { dd(get_defined_vars(), url()->current()) } }*/-->
@if(!Auth::guest())
    @if(Auth::user()->admin == 1)
        <h3>Manage Gallery</h3>
    @else
        <h3>Gallery </h3>   
    @endif
@else
    <h3>Gallery (Visitor)</h3>  
@endif

<style>
.flex-container {
           display: -webkit-flex;
           display: flex;
           -webkit-flex-direction: row;
           flex-direction: row;
           -webkit-align-items: center;
           align-items: center;
           justify-content: space-around;
           flex-wrap: wrap;
           min-width: 100px;
           max-width: 1100px;
        }
        .flex-item {
            display:flex;
        align-items: center;
        /*height: 100px;
        width: 200px;*/
        background-color: #f5f8fa;
        color: white;
        justify-content: space-between;
        margin: 3px;
        padding: 10px 0 0 10px;
        }
.flex-item img{
    flex-grow:0;
    flex-shrink:0;
}
        span {
            padding-left: 5px;
        }
</style>


@if(count($data['folders'])>0)

    <div class="flex-container">
    @foreach ($data['folders'] as $folder)

            <div class="flex-item">

                <form action="{{action('GalleryController@show', $folder)}}" method="get">
                    <input name="_method" type="hidden" value="show">
                        <button class="btn btn-lg px-5 btn-primary" style="margin:5px" type="submit" role="button" <img src="assets/folder_image.png" height="30">{{$folder}}</button>
                </form>

            <!-- --------------------------------------- -->
            <!-- Only show crud buttons if administrator -->
            <!-- --------------------------------------- -->

            @if(!Auth::guest())
                @if(Auth::user()->admin == 1)
                    <a href="{{action('GalleryController@edit',$folder)}}" class="btn btn-primary btn-sm">Delete 1 Picture</a>&nbsp;
                            <form action="{{action('GalleryController@destroy', $folder)}}" method="post">
                                {{csrf_field()}}
                                <input name="_method" type="hidden" value="DELETE">
                                <button class="btn btn-danger btn-sm" type="submit">Delete entire Folder</button>
                            </form>

                            <a href="{{action('GalleryController@add',$folder)}}" class="btn btn-primary btn-sm">Add piccies</a>&nbsp;
                    <form action="{{action('GalleryController@add')}}" method="post">
                                {{csrf_field()}}
                                <input name="folder" type="hidden" value='{{$folder}}'>
                                <button class="btn btn-info btn-sm" style="margin:5px" type="submit">Add new pictures</button>
                            </form>

                @endif
            @endif  


            </div>
    @endforeach
    @if(!Auth::guest())
        @if(Auth::user()->admin == 1)
        <div class="flex-item" style="width:1500px">        <!-- large width forces flexbox to new row -->
            <a href="{{action('GalleryController@create')}}" class="btn btn-primary btn-sm">Create</a>
        </div>  
        @endif
        @endif  

    </div>
@else
    @if(!Auth::guest())
        @if(Auth::user()->admin == 1)
            <br><br><br>
            <a href="{{action('GalleryController@create')}}" class="btn btn-primary btn-sm">Create</a>
        @endif
        @endif  
    <p>No Gallery Folders available.</p>
@endif




@endsection

我不记得为什么我对 'delete 1 picture' 使用 link 而对 'delete entire folder' 使用表格。我假设我在一些教程中找到了它并且只是使用了它。我想删除特定图像(使用控制器的 'edit' 组件可以正常工作)或删除整个文件夹(使用控制器的 'destroy' 组件也可以正常工作)。

因此下一步是能够将新文件夹或图像添加到特定文件夹。使用控制器的 'create' 组件添加新文件夹也能正常工作。

然后它在尝试添加新图像时崩溃了。使用如图所示的代码,我会收到 2 个不同的错误,具体取决于我是单击 'Add Piccies' 按钮还是 'Add new pictures' 按钮。

我的路线是(get和post我都试过了):

Route::post('/gallery/add/', 'GalleryController@add');
/* Dynamically create all routes for CRUD for gallery table */
Route::resource('gallery', 'GalleryController');

如果我使用 'add piccies' 按钮,我会得到 'http://hdkumdo.com/lsapp/public/gallery/add?2019_Masters' 的 URL,其中 ?2019 等是相关文件夹。 如果我使用 'add new picture' 按钮,我会收到错误消息“类型错误:函数 App\Http\Controllers\GalleryController::add() 的参数太少,0 已通过,正好是 1 预期”。

如果我更改从 post 获取的路由,我会收到错误消息 'Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 没有留言'.

呈现的视图如下所示: View of buttons

因为我已经使用了 RESOURCE 创建的基本 CRUD 元素,所以我向控制器添加了一个新函数,认为我可以直接调用它。代码片段为:

/**
     * Show the form for uploading pictures.
     *
     * @return \Illuminate\Http\Response
     */
    public function add($gallery)
    {
    $folder = $gallery;
    $title = 'Add Pictures';
    TheUser::get_Role();
    $userRole = session('userRole');
        Log::info('GalleryController.add called : user Role =>' . $userRole . ' Folder:' . $folder );
    $data = array(
            'userRole' => $userRole,
        );
    /*dd($data);*/
    return view('gallery.add')->with('title', $title)->with('data', $data);
    }

因此,欢迎任何 suggestions/advice 等。

查看上面的评论。这是需要注意的事情,因为我花了几天时间挠头,需要一点横向思考。