0

I'm making a web app with a form that inserts data into a database. I'm making this web app with Laravel. Now I made the form and the code for the insertion but it is not working, it is giving a not found error. Hopefully, someone can give me some advice on how to fix this problem.

This is my View:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Spelers Toevoegen</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="main.js"></script>
</head>
<body>
 <form id="form" action="/add" method="post">

 @csrf

   <div class="form-group">
     <h1>Speler toevoegen</h1>
     <label>Naam:<input name="naam" type="text" value="" class="form-control"></label><br>
     <label>Aantal:<input name="aantal" type="text" value="" class="form-control"></label><br>
            <label>Positie:<input name="positie" type="text" value="" class="form-control"></label><br>
            <label>club:<input name="club" type="text" value="{{$naam}}" class="form-control"></label><br>
            <button type="submit" class="btn btn-primary"><a id="btn_link">Voeg spelers toe</a></button>
        </div>
 </form>

This is my controller function:

public function Add(Request $request)
{
    $speler_naam = $request->input('naam');
    $aantal = $request->input('aantal');
    $positie = $request->input('positie');
    $club = $request->input('club');

    $data = array('speler_naam'=>$speler_naam, "doelpunten"=>$aantal, "positie"=>$positie, "club_naam"=>$club);

    SpelerSelectie::insert($data);
}

This is my route:

Route::post('/add', 'VoetbalController@Add');
2
  • A "not found error" but what error/ can't be found? Commented Jan 5, 2019 at 22:58
  • It is trying to go to the page /add. this is the exact error message it gives me. The requested URL /add was not found on this server. Commented Jan 5, 2019 at 23:03

1 Answer 1

1

First you should name your method in lower case not Uppercase

So your route should look like this:

Route::post('/add', 'VoetbalController@add');

And you save it like this to your database:

public function add(Request $request) {
    $data = SpelerSelectie::create($request->all());

    if($data) {
        return redirect()->route('add.index')->with('message', 'Success.');
    }

    return redirect()->route('add.index')->with('message', 'error.');
}

Further you should import the model in your Controller at the Top of the file:

use App\SpelerSelectie;

Further you need to add the field which you are inserting / updating to your model in your model's fillable array:

protected $fillable = ['field1']; // you need to add your field name (tablenames)

I assume that you created a proper migration, if not you have to create a proper migration first.

Sign up to request clarification or add additional context in comments.

6 Comments

I am a bit confused with what you mean with the fillable array, where do I put this code? Also, I have not done a proper migration yet, is this necessary? I think the problem is the /add I put as the action in my form, could this be the problem?
@stefandeboer well if you did not create a migration you post likely dont have the proper table in your local database. I hope you did not create in manually which is bad practice in this case. Best would be if you check out the migration docs or watch a video about migration. A migration simply helps you to create proper database tables, and the $fillable array in your model tells the model which data shall actually be stored/updated, so if you need to store the name add the name to the $fillable array and so on.
But shouldn't it also work without a migration? I looked at the table on my WAMP server and everything seems to be fine.
If the tables exists its fine, most people create migrations to setup the database but if you setup the database manually it should still work. If you say that your database looks correct then your mistake is elsewhere.
I think the problem is the way I fill in my action attribute in my form because the error says /add cant be found on the server. Why is the action attribute not working?
|

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.