0
votes

I have problem to save name and email from user1 in table user1s that I have made .

When I enter them in textareas using html form in Laravel with route::post and function store it is not working. When I enter text and hit the button Register it outputs the following error:

MethodNotAllowedHttpException in RouteCollection.php line

You will see that I use the HTML form and that I have tried to add <input ....> into my form.


Here are my files:

route.php

<?php

Route::get('/','PageController@home');
Route::post('/','User1Controller@store');

Route::get('about','PageController@about');

welcome.blade.php

I'm not sure about the action. After putting user1 inf into table, it should be redirected to a "Thank you" page (I have a thankyou.blade.php ) , maybe that is the problem

<form  method="POST" action=""> 
                    <input name="_token" type="hidden" value="{{ csrf_token() }}"/>

                <ul class="list-group" >


                    <li  >
                    NAme
                    <div class="form-group" title="email" >
                        <textarea name="name" class="form-control" >

                        </textarea>
                    </div>
                    </li  >

                    <li>Email
                    <div class="form-group" > 
                        <textarea name="email"  class="form-control" >

                        </textarea>
                    </div>
                    </li>

                    <li  >
                        <div class="form-group" >
                        <button class="btn btn-primary">Register</button>
                        </div>
                    </li>

                </ul>
            </form>

migration for user1

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateNotesTable extends Migration
{

    public function up()
    {
        Schema::create('notes', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('card_id')->unsigned();
            $table->text('body');            
            $table->timestamps();
        });
    }


    public function down()
    {
        Schema::drop('notes');
    }
}

user1controller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User1;

class User1Controller extends Controller
{

    public function store(Request $request)
        {

            $user= new User1;
            $user->name = $request->name;
            $user->email =  $request->email;
            $user->save();

            return view('thankyou');

        }

}

pagecontroller.php

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

use App\User1;

class PageController extends Controller
{
    public function home()
    {
            $user1s= User1::all();
    return view('welcome',compact('user1s'));
    }

    public function about()
    {
            return view('pages.about'); 
    }

}
3
acction="/user1" ?lagbox
I'm not sure what should I put as action , I tough that this would mean that I wisit the page '/user1' after the metod store is finished ,but I'm confused.moonlight
I changed the action=" " .moonlight
<form method="POST" acction=""> what is acction, you mean action?lagbox
I corrected the typo.Thanks!moonlight

3 Answers

0
votes

Your form is basically a registration form. I would recommend using a more meaningful name for the end point. The post route can be something like,

Route::post('/register','User1Controller@store');

Now the form action can be,

action="/register"
0
votes

I corrected the typo.Thanks!

I have also changed this Route::post('/','User1Controller@store'); and action=" " .

It works ,the only thing right now that is not good is that I should redirect to a page "Thank you" not go to anther view on the exact same page.

Because It makes a mess in the database when I reload the home page. I'll try that and tell if it works.

Thank you people for the help! :)

0
votes

Things solved,this works. I will add the code that i have added so the other can find it!

Firs of all : I haven't figured why ,but action="dir1/dir3" for me didn't work!

Here are the added things!

routes.php

Route::get('thankyou','PageController@thankyou');


***PageController.php***


	public function thankyou()
	{
			return view('thankyou'); 
	}

*****User1Controller.php*****

    public function store(Request $request)
	    {
	    	
	    	$user= new User1;
	    	$user->name = $request->name;
	    	$user->email =  $request->email;
	    	$user->save();

	    	return redirect('/thankyou');
			
	    }