0

I am trying to insert data in Laravel using DB

code:

web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ExamController;

Route::get('/', [ExamController::class, 'Index']);
Route::post('/exam-sub', [ExamController::class, 'Insertexam'])->name('sub')

welcome.blade.php

<!DOCTYPE html>   
<html>
<head>
    <link rel="stylesheet" type="text/css" href="{{ URL::asset('css/test.css') }}" />
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
    <title>Quiz app</title> 
</head>

<body bgcolor="green">
    <form action="{{ route('sub') }}" method="post">
        @csrf 
        <div class="container mb-3">
            @if(Session::has('exam_submitted'))
                <div class="alert alert-success">
                    {{ Session::get('exam_submitted') }}
                </div>
            @endif

            <div class="form-outline">
                <label class="form-label" for="form3Example1">First name</label>
                <input type="text" id="form3Example1" class="form-control" name="first_name"/>
                @error('first_name')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>

            <div class="form-outline">
                <label class="form-label" for="form3Example1">SEC NAME</label>
                <input type="text" id="form3Example1" class="form-control" name="sec_name"/>
                @error('sec_name')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>  

            <div class="form-outline">
                <label class="form-label" for="form3Example1">EMAIL</label>
                <input type="text" id="form3Example1" class="form-control" name="email"/>
                @error('email')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>  

            <div class="form-outline">
                <label class="form-label" for="form3Example1">PASSWORD</label>
                <input type="password" id="form3Example1" class="form-control" name="password"/>
                @error('password')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>

            <h1 class="color-grey">test</h1>
            <hr style="width: 30%; font-weight: 700; height:3px; background-color:black;">

            <div class="form-outline">
                <label class="form-label" for="form3Example1">24x90</label>
                <input type="text" id="form3Example1" class="form-control" name="question1"/>
                @error('question-1')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>

            <div class="form-outline">
                <label class="form-label" for="form3Example1">45x80</label>
                <input type="text" id="form3Example1" class="form-control" name="question2" />
                @error('question-2')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>

            <div class="form-outline">
                <label class="form-label" for="form3Example1">100x10</label>
                <input type="text" id="form3Example1" class="form-control" name="question3"/>
                @error('question-3')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>

            <div class="form-outline">
                <label class="form-label" for="form3Example1">10x10</label>
                <input type="text" id="form3Example1" class="form-control" name="question4"/>
                @error('question-4')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>

            <br>
            <button class="btn btn-outline-primary btn-block btn-lg">Submit test</button>
        </div>
    </form>
       
    <div class="btn"></div>

    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-SR1sx49pcuLnqZUnnPwx6FCym0wLsk5JZuNx2bPPENzswTNFaQU1RDvt3wT4gWFG" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-j0CNLUeiqtyaRmlzUHCPZ+Gy5fQu0dQ6eZ/xAww941Ai1SxSY+0EQqNXNE6DZiVc" crossorigin="anonymous"></script>

</body>
</html>

ExamController

 <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ExamController extends Controller
{
    public function Index() {
        return view('welcome');
    }

    public function Insertexam(Request $request) {
        $validatedata = $request->validate([
            'id' => mt_rand(1, 1000),
            'first_name' => 'required',
            'sec_name' => 'required',
            'email'=> 'required|email',
            'password' => 'required|min:8|max:24',
            'question-1' => 'required|int',
            'question-2' => 'required|int',
            'question-3' => 'required|int',
            'question-4' => 'required|int',
        ]);

        DB::table('exam')->insert([
            'Firstname' => $request->first_name,
            'Secondname' => $request->body,
            'email' => $request->email,
            'password' => $request->password,
            'question1' => $request->question1,
            'question2' => $request->question2,
            'question3' => $request->question3,
            'question4' => $request->question4,
        ]);

        return back()->with('exam_submitted','Exam submitted successfully contact you mr for the results good luck');
    }
}

The problem is when inserting data it shows this:
array_map(): Argument #2 ($array) must be of type array, int given

1 Answer 1

1

The problem is with this 'id' => mt_rand(1, 1000), try to remove it.

$validatedata = $request->validate([
               'id' => mt_rand(1, 1000),
                'first_name'=> 'required',
                'sec_name'=>'required',
                'email'=> 'required|email',
                'password'         => 'required|min:8|max:24',
                'question-1' => 'required|int',
                'question-2' => 'required|int',
                'question-3' => 'required|int',
                'question-4' => 'required|int',
            ]);
Sign up to request clarification or add additional context in comments.

3 Comments

thank You I forgot this is big mistake from me
explain the problem to him, your answer requires educational explanation
@CoderMoataz - The problem is that validate method accepts array with validation rules which will be applied to the input parameters from the request. mt_rand(1,1000) is not a validation rule. Please check: laravel.com/docs/8.x/validation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.