图像字段是必需的,(PHP/LARAVEL)

xjreopfe  于 2023-03-07  发布在  PHP
关注(0)|答案(1)|浏览(165)

我不明白为什么形式和发生了什么事后,在后台不得到的形象,我的地方在形式。
错误:enter image description here
验证码:表单视图addaccount.blade.php:

<!-- start content -->
    <div class="container">
        <div class="card w-100 text-center shadowBlue">
          <div class="card-header">
          New Account Forum
          </div>
          <div class="card-body bg-dark text-white">
            @if (count($errors))
                <div class="alert alert-danger">
                  <ul>
                    @foreach ($errors->all() as $error)
                      <li>
                        {{$error}}
                      </li>
                    @endforeach
                  </ul>
                </div>
            @endif
            @if (Session::has('status'))
            <div class="alert alert-success">
              {{Session::get('status')}}
            </div>
            @endif
            <table class="table">
              <tbody>
                <tr>
                  <form action="{{url('/admin/saveaccount')}}" method="POST" enctype="multipart/form-data">
                    {{ csrf_field() }}
                    <th>Name</th>
                    <td><input type="text" name="name" class="form-control input-sm" required></td>
                    <th>CNIC</th>
                    <td><input type="number" name="cnic" class="form-control input-sm" required></td>
                </tr>
                <tr>
                    <th>Account Number</th>
                    <td><input type="" name="accountnumber" readonly value="{{time()}}" class="form-control input-sm" required></td>
                    <th>Account Type</th>
                    <td>
                      <select class="form-control input-sm" name="accounttype" required>
                        <option value="current" selected>Current</option>
                        <option value="saving" selected>Saving</option>
                      </select>
                    </td>
                </tr>
                <tr>
                    <th>City</th>
                    <td><input type="text" name="city" class="form-control input-sm" required></td>
                    <th>Address</th>
                    <td><input type="text" name="address" class="form-control input-sm" required></td>
                </tr>
                <tr>
                    <th>Email</th>
                    <td><input type="email" name="email" class="form-control input-sm" required></td>
                    <th>Password</th>
                    <td><input type="password" name="password" class="form-control input-sm" required></td>
                </tr>
                <tr>
                    <th>Deposit</th>
                    <td><input type="number" name="balance" min="500" class="form-control input-sm" required></td>
                    <th>Source of income</th>
                    <td><input type="text" name="source" class="form-control input-sm" required></td>
                </tr>
                <tr>
                    <th>Contact Number</th>
                    <td><input type="number" name="phone"  class="form-control input-sm" required></td>
                    <th>Photo</th>
                    <td>
                      <input type="file" name="photo"  class="form-control input-sm" required>
                    </td>
                </tr>
                <tr>
                  <td colspan="4">
                    <button type="submit" name="saveAccount" class="btn btn-primary btn-sm">Save Account</button>
                    <button type="Reset" class="btn btn-secondary btn-sm">Reset</button>
                  </form>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
          <div class="card-footer text-muted">
            MCB Bank  
          </div>
        </div>
      </div>
      <!-- end content -->
@endsection

web.php:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LoginController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\ClientController;
use App\Http\Controllers\CashierController;

// Admin Controller pages
Route::get('/admin/home', [AdminController::class, 'home']);
Route::get('/admin/accounts', [AdminController::class, 'accounts']);
Route::get('/admin/addaccount', [AdminController::class, 'addaccount']);
Route::get('/admin/feedback', [AdminController::class, 'feedback']);
Route::get('/admin/clientdetails', [AdminController::class, 'clientdetails']);
Route::get('/admin/notice', [AdminController::class, 'notice']);
Route::post('/admin/addcashier', [AdminController::class, 'addcashier']);
Route::post('/admin/updatecashier', [AdminController::class, 'updatecashier']);
Route::get('/admin/deletecashier/{id}', [AdminController::class, 'deletecashier']);
Route::post('/admin/saveaccount', [AdminController::class, 'saveaccount']);

AdminController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Models\Cashier;
use App\Models\Account;
use App\Models\Statement;

class AdminController extends Controller
{
    //

    public function home(){
        return view('admin.home');
    }

    public function accounts(){
        $cashiers = Cashier::All();
        return view('admin.accounts')->with('cashiers', $cashiers);
    }

    public function addaccount(){
        return view('admin.addaccount');
    }

    public function feedback(){
        return view('admin.feedback');
    }

    public function clientdetails(){
        return view('admin.clientdetails');
    }

    public function notice(){
        return view('admin.notice');
    }

    public function addcashier(Request $request){
        $cashier = new Cashier();
        $cashier->email = $request->input('email');
        $cashier->password = $request->input('password');
        $cashier->accounttype = "Cashier";

        $cashier->save();
        return back()->with('status', 'Le compte a été créé avec succès');
    }

    public function updatecashier(Request $request){
        $cashier = Cashier::find($request->input('id'));
        $cashier->email = $request->input('email');
        $cashier->password = $request->input('password');

        $cashier->update();
        return back()->with('status', 'Le compte cassier a été modifié avec succès');
    }

    public function deletecashier($id){
        $cashier = Cashier::find($id);
        $cashier->delete();
        return back()->with('status', 'Le compte cassier a été supprimé avec succès');
    }

    public function saveaccount(Request $request){
        $this->validate($request, [
            'image' => 'required|image|max:1999'
        ]);
        

        $fileNameWithExt = $request->file('image')->getClientOriginalName();
        $filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
        $ext = $request->file('image')->getClientOriginalExtension();
        $filenameToStore = $filename.'_'.time().'.'.$ext;

        $account = new Account();
        $account->name = $request->input('name');
        $account->accountnumber = $request->input('accountnumber');
        $account->city = $request->input('city');
        $account->email = $request->input('email');
        $account->balance = $request->input('balance');
        $account->phone = $request->input('phone');
        $account->cnic = $request->input('cnic');
        $account->accounttype = $request->input('accounttype');
        $account->address = $request->input('address');
        $account->password = $request->input('password');
        $account->source = $request->input('source');
        $account->image = $filenameToStore;
        $account->branchname = 'Equity BCDC';
        $account->branchcode = 11111332;

        $statement = new Statement();
        $statement->name = $request->input('name');
        $statement->source = $request->input('acountnumber');
        $statement->destination = $request->input('acountnumber');
        $statement->amount = $request->input('balance');
        $statement->status = 0;

        $account->save();
        $statement->save();

        // Upload image
        $path = $request->file('image')->storeAs('public/account_images', $filenameToStore);
        return back()->with('status', 'le compte client a été crée avec succès');
    }
}

我在我的表单上添加了图像,但我认为图像永远不会被抛出表单,因为我一添加所需的一切都停止工作,我开始得到错误“图像字段是必需的”

li9yvcax

li9yvcax1#

我认为错误就在这里:

<input type="file" name="photo"  class="form-control input-sm" required>

name属性为photo

在控制器中,使用以下命令验证请求:

public function saveaccount(Request $request){
    $this->validate($request, [
        'image' => 'required|image|max:1999'
    ]);

您需要使用与您输入的名称相同的名称验证请求(照片)
您可以尝试使用以下内容进行验证:

$this->validate($request->all(), [
        'photo' => 'required|image|max:1999'
    ]);

相关问题