#Form doesn't send information to database

31 messages · Page 1 of 1 (latest)

wicked pier
#
<?php

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

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/home', [RegisterController::class, 'index'])->name('home');
Route::get('/home/create', [RegisterController::class, 'create'])->name('create');
Route::post('/home', [RegisterController::class, 'store'])->name('store');
#
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Register;

class RegisterController extends Controller
{
    public function index(){
        return view('index');
    }
    public function create(){
        return view('create');
    }
    public function store(Request $request) {
        $data = $request->validate([
            'naam' => 'required',
            'land' => 'required',
            'beschrijving' => 'required',
            'age' => 'required',
        ]);
        

        $newKaas = Register::create($data);

        return redirect(route('index'));

    }
}

#
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head> 
<body>
    <h1>Create product</h1>
    <form method="post" action="{{route('store')}}">
    @csrf
    @method('post')
        <div>
            <label for="name">Name</label>
            <input type="text" name="name" placeholder="name">
        </div>

        <div>
            <label for="land">Land</label>
            <input type="text" name="land" placeholder="land">
        </div>

        <div>
            <label for="beschrijving">Beschrijving</label>
            <input type="text" name="beschrijving" placeholder="beschrijving">
        </div>

        <div>
            <label for="nummer">Age</label>
            <input type="number" name="nummer" placeholder="nummer">
        </div>

        <div>
            <button type="submit">Create</button>
        </div>
    </form>
</body>
</html>
warm gazelle
#

Hello you have an input variable named incorrectly

#

It should be named 'naam'

wicked pier
#

Where exactly in the form?

warm gazelle
#

here: <input type="text" name="name" placeholder="name"> </div>

#

Hope that helped

wicked pier
wicked pier
warm gazelle
#

Yes that is correct, however you should change it to read name="naam" as shown in your validate logic

wicked pier
#

Still not working

#

        <div>
            <label for="naam">Name</label>
            <input type="text" name="naam" placeholder="naam">
        </div>

warm gazelle
#

Other auxillary checks, would be checking mass assignment and if the other fields have been created with nullable()

#

Additionally, this is also wrong <label for="nummer">Age</label> <input type="number" name="nummer" placeholder="nummer"> </div>

#

it should read name="age"

wicked pier
#

HOly

#

YEs

#

thank you ❤️

warm gazelle
#

No problem, you are welcome

wicked pier
#

ONly now i am running in to a problem it isn't going to the Table it should be in

warm gazelle
#

In that case please check your Register::create method.

wicked pier
#

$newKaas = RegisterController::create($data);

#

My controller is Called RegisterController so that's right

wicked pier
#
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Register extends Model
{
    use HasFactory;

    protected $fillable = [
        'naam',
        'land',
        'beschrijving',
        'age',
    ];
}

``` This is my modle
#

model

warm gazelle
#

What table is it going into?

wicked pier
#

kaas_register

warm gazelle
#

OK I'm not sure, assuming you've followed the tutorial on how to make models, migrations and using eloquent ORM correctly, it should work.

#

For the record, I don't use the ORM facility.

wicked pier
#

Hmm weird