Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
56 views
in Technique[技术] by (71.8m points)

php - Unable to pass values to database using laravel

im a little new to laravel and would really appreciate some help with this. Im trying to make a registration form to pass user info from a website to mysql database.

This is the form Im using.

<form action="{{ route('register') }}" method="post">
                @csrf
                <div class="mb-4">
                    <label class="text-sm font-medium text-gray-500">LCard Number:</label>
                    <input type="number" name="cardno" id="cardno" placeholder="LCard Number" class="bg-gray-100 border-2 w-full p-4 rounded-lg" value="{{ old('cardno') }}">

                    @error('cardno')
                        <div class="text-red-500 mt-2 text-sm">
                            {{ $message }}
                        </div>
                    @enderror
                </div>

                <div class="mb-4">
                    <label class="text-sm font-medium text-gray-500">Amount Spend:</label>
                    <input type="number" name="amount" id="amount" oninput="showInput();" placeholder="Enter Amount" class="bg-gray-100 border-2 w-full p-4 rounded-lg" value="{{ old('amount') }}">

                    @error('amount')
                        <div class="text-red-500 mt-2 text-sm">
                            {{ $message }}
                        </div>
                    @enderror
                </div>

                <div class="mb-4">
                    <label class="text-sm font-medium text-gray-500">Points Earned:</label>
                    <input name="points" id="display" disabled=yes class="bg-gray-100 border-2 w-full p-4 rounded-lg showInput">
                </div>

                <div class="mb-4">
                    <label class="text-sm font-medium text-gray-500">Transaction Date:</label>
                    <input type="date" name="date" id="date" placeholder="Date of Transaction" class="bg-gray-100 border-2 w-full p-4 rounded-lg" value="">

                    @error('date')
                        <div class="text-red-500 mt-2 text-sm">
                            {{ $message }}
                        </div>
                    @enderror
                </div>

                <div>
                    <button type="submit" class="bg-blue-500 text-white px-4 py-3 rounded font-medium w-full">Save</button>
                </div>
            </form>

Here is my controller class.

class RegisterController extends Controller
{   
    public function index()
    {
        return view('auth.register');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
        'cardno' => 'required|max:10',
        'amount' => 'required|max:8',
        'points' => 'required',
        'date' => 'required',
    ]);

    User::create([
        'cardno'=>$request->cardno,
        'amount'=>$request->amount,
        'points'=>$request->points,
        'date'=>$request->date,
    ]);
}
}

This is the route I put in web.php

Route::get('/register', [RegisterController::class, 'index'])->name('register');
Route::post('/register', [RegisterController::class, 'store']);

After I enter values into the form and press submit nothing happens. And help would be very much apreciated, thank you.

question from:https://stackoverflow.com/questions/65930697/unable-to-pass-values-to-database-using-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

there will be validation error

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

put this before you form

also you should put return statement in controller

public function store(Request $request)
{
    $this->validate($request, [
    'cardno' => 'required|max:10',
    'amount' => 'required|max:8',
    'points' => 'required',
    'date' => 'required',
]);

User::create([
    'cardno'=>$request->cardno,
    'amount'=>$request->amount,
    'points'=>$request->points,
    'date'=>$request->date,
    ]);

    return redirect()->back()->with('success','Created.');
}

otherwise only database insert operation will done but user won't get any output


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...