首页 > 解决方案 > 是否可以在不填写 mysql 某些属性的情况下上传表单?

问题描述

我只是想知道是否可以在不向 MySQL 中填充某些属性的情况下上传表单?我想上传他们可以填写的用户表单以发送他们的申请,但是在管理员方面,他们发送的表单将有另一列是备注,管理员可以拒绝申请然后填写他们被拒绝的备注存储在数据库中。管理员可以编辑它。我试图将值设置为null,但是,当我想对被拒绝的候选人发表评论时,它不会保存在数据库中,它仍然显示为null

存储数据的功能

public function update(Request $request, $id)
{
    $request->validate([
    
        'fullname' => 'required',
        'email' => 'required',
        'contact' => 'required',
        'address' => 'required',
        'attachment' => 'required',
        'remarks' => ['nullable', 'string', 'min:5', 'max:500'],
    ]);


    $candidate = new MainForm();
    $candidate->fullname = $request->fullname;
    $candidate->email = $request->email;
    $candidate->contact = $request->contact;
    $candidate->address  = $request->address ;
    $candidate->attachment = $request->attachment;
    $candidate->remarks  = $request->remarks ;
   
   
        if
        ($file = $request->hasFile('attachment')) {
            $file = $request->file('attachment');
 
            $filename = time() . '.' .$file->getClientOriginalName();
            $destinationPath = public_path('/uploads/documents/');
 
            $file->move($destinationPath, $filename);
            $candidate->attachment = $filename;
        }

        $candidate->save();
        
        return back();
}

下面是我的数据库代码

  $table->id();
        $table->string('fullname');
        $table->string('email');
        $table->longText('contact');
        $table->string('address');
        $table->string('attachment');
        $table->string('remarks')->default(NULL);
        $table->boolean('status')->default(0);

下面是我的刀片文件,我可以显示候选人的详细信息,一旦我打开备注按钮,它会给我一个模态,我可以在其中放我的备注。

                        <div class="flex justify-center items-center">
                        <a href="javascript:;" data-toggle="modal" data-target="#remarks-modal-preview" class="flex items-center mr-3"><i  class="w-4 h-4 mr-1"></i> Remarks</a>

                        <div class="modal" id="remarks-modal-preview">

                            <div class="modal__content">
                                <div class="intro-y flex items-center p-5">
                                    <h2 class="text-lg font-medium mr-auto">
                                        Remarks
                                    </h2>
                                </div>
                                <form method="post" action="{{ route('mainform') }}">
                                @csrf
                                <div class="intro-y box p-5">
                                   
                                    <div class="mt-3">
                                        <label>Name</label>
                                        <div class="mt-2">
                                            <input type="text" placeholder="name" name="fullname"
                                                class="input w-full border col-span-4 form-control" value="{{ $candidate->fullname}}" readonly >
                                        </div>
                                    </div>

                                    <div class="mt-3">
                                        <label>Email</label>
                                        <div class="mt-2">
                                            <input type="text" placeholder="email" name="email"
                                                class="input w-full border col-span-4 form-control" value="{{ $candidate->email}}" readonly >
                                        </div>
                                    </div>

                                    <div class="mt-3">
                                        <label>Phone Number</label>
                                        <div class="mt-2">
                                            <input type="text" placeholder="contact" name="contact"
                                                class="input w-full border col-span-4 form-control" value="{{ $candidate->contact}}" readonly >
                                        </div>
                                    </div>
                                    <div class="mt-3">
                                        <label>Address</label>
                                        <div class="mt-2">
                                            <input type="text" placeholder="address" name="address"
                                                class="input w-full border col-span-4 form-control" value="{{ $candidate->address}}" readonly >
                                        </div>
                                    </div>
                               
                                    <div class="mt-3">
                                        <label>Remarks</label>
                                        <input type="text" placeholder="remarks" name="remarks"
                                     class="input w-full border col-span-4 form-control tail-select" value="{{ $candidate->remarks}}">
                                            </div>
                              
                                    
                                    <div class="text-right mt-5">
                                        <button type="button" data-dismiss="modal"
                                            class="button w-24 border dark:border-dark-5 text-gray-700 dark:text-gray-300 mr-1">Cancel</button>
                                        <button type="submit" class="button w-24 bg-theme-1 text-white">Save</button>
                                        
            
                                    </div>
                                </div>
                                </form>
                        </div>

                    </div>

标签: phpmysqllaravel

解决方案


编辑:您的代码有很多问题,但主要问题是您的网络表单({{ route('mainform') }})中的路由没有指向您的路由文件(Route::post('/edit{id}', 'GeneralRejectedController@update')->name('generalrejected.update');)中的路由。


在您的 Laravel 迁移中,将列标记为可为空的正确方法是使用该nullable()方法。

代替:

 $table->string('remarks')->default(NULL);

你应该使用:

 $table->string('remarks')->nullable();

Laravel 文档:可空列修饰符


在您的控制器表单验证中,不要忘记指定该列可以为空:

$validated = $request->validate([
    ...
    'remark' => 'nullable'|'string'|'min:5'|'max:500',
    ....
]);

Laravel 文档:验证可选字段。


这就是它的全部。如果您仍然无法保存“备注”字段,则您可能在其他地方遇到了问题。您可以使用 . 检查您的控制器中的请求内容dd($request->all())


查看您的控制器 update() 方法。有很多问题是因为你没有遵循 Laravel 最佳实践。

  1. 调用你的模型没有意义MainForm,如果我理解你的应用程序,它应该被调用Candidate

  2. 在保存到数据库之前,您应该始终验证用户输入,不要保存请求值,保存经过验证的值。

  3. 使用 Laravel Eloquent 创建对象。而不是使用(永远不要这样做)创建对象,new Object()您应该使用依赖注入并执行类似$candidate->create($validated).

  4. 您应该使用 CRUD 命名路由。如果你的 Controller 方法是update你的路由应该是candidates.update. 打电话给你的路线没有意义mainform

这是您的控制器的外观(未经测试,但它是一个起点):

    public function update(Request $request, Candidate $candidate) // Update your route to pass the Candidate object instead of the ID.
    {
        // dd($request->all()); // Use this to debug your request

        $validated = $request->validate([
            'fullname' => 'required',
            'email' => 'required',
            'contact' => 'required',
            'address' => 'required',
            'attachment' => 'required',
            'remarks' => ['nullable', 'string', 'min:5', 'max:500'],
        ]);

        $candidate->update($validated);

        // You should validate your file before storing to your drive. This could be a vulnerability.
        if ($file = $request->hasFile('attachment')) {
            $file = $request->file('attachment');

            $filename = time() . '.' . $file->getClientOriginalName();
            $destinationPath = public_path('/uploads/documents/');

            $file->move($destinationPath, $filename);
            $candidate->attachment = $filename;
        }

        return back();
    }


推荐阅读