I'm Trying to solve this error but not getting any proper solution for this as i'm beginner in the laravel, Please help with the code..
error--> 1
Thank You
This is my Web.php file
Route::get('/', function() {
return view('welcome');
});
Route::get('user',function(){
return view('user');
});
Route::get('user/register',['uses'=> 'usercontroller#create']);
Route::post('/user',['uses'=> 'usercontroller#store']);
Register.blade.php
<form method="POST" method="/user">
{{ csrf_field() }}
name<input type="text" name="name">
email<input type="email" name="email">
pass<input type="password" name="password">
<input type="submit" value="register">
</form>
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class usercontroller extends Controller
{
public function create()
{
return view('register');
}
public function store(Request $request)
{
User::create($request->all());
return 'Sucess';
return $request->all();
}
}
You have a mistake in your form tag (the second method should be action):
Change
<form method="POST" method="/user">
to
<form method="POST" action="/user">
Related
I was trying to send an array through a route to another view, but when i used the function get_defined_vars(), i realized that i was sending a string with the information. Is it possible to do that?
this form from my view should send the array to my route
<form action="/trans" method="POST">
#csrf
<div class="input-group">
<input type="hidden" class="form-control" name="r" value="{{$cooperado}}">
<button type="submit" class="btn btn-primary">
<span>+</span>
</button>
</span>
</div>
</form>
then this route should send the array to the other view
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with(['j'=>$j]);
});
this is the controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Movimentacoes;
class MovimentacoesController extends Controller
{
public function create()
{
//
return view('movs.create');
}
}
routes.php
Route::post('/trans', 'MovimentacoesController#create');
controller
use Illuminate\Http\Request;
use App\Movimentacoes;
class MovimentacoesController extends Controller
{
public function create(Request $request)
{
$j = $request->request->get('r');
return view('movs.create')->with(['j' => $j]);
}
}
Code like this In the form tag:
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
submit this form
then the Input::get('r') will be Array!
I hope it helps you.
I'm using Xampp and Laravel and my app is throwing the following error while I'm trying to make a post route:
ERROR
Declaration of App\Http\Controllers\HandleClient::validate() should be compatible with App\Http\Controllers\Controller::validate(Illuminate\Http\Request $request, array $rules, array $messages = Array, array $customAttributes = Array)
Form
<form action="{{route('handle')}}" method="POST">
<label for="cn">Customer Name</label>
<input type="text" name="cn" placeholder="Customer Name" />
<input type="submit" value="Add Request"/>
<input type="hidden" value="{{Session::token()}}" name="_token" />
</form>
Controller HandleClient.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HandleClient extends Controller
{
public function validate(Request $request){
return view('finish',$request);
}
}
web.php routes file:
<?php
Route::get('/', function () {
return view('welcome');
})->name('home');
Route::post('/Request_handled',[
'uses' => 'HandleClient#validate',
'as' => 'handle'
]);
By default, the base Controller class usesValidatesRequests which provides a validate function to the Controller class. Naming your function validate overrides this function.
Rename your function from validate to something else and update your route, then you shouldn't have a conflict anymore.
I am trying to add a record to a database utilizing a resource controller, however, I'm getting MethodNotAllowedHttpException error. I'm using a pivot table. I have gone through several similar questions like a link! but none seem to answer me. This is my code:
Routes.php
Route::group(['prefix' => 'user'], function () {
Route::resource('categories', 'User\CategoriesController');
});
CategoriesController.php
<?php
namespace App\Http\Controllers\User;
use Session;
use Sentinel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Sections;
use App\Models\Categories;
use App\Models\Users;
use App\Models\CategorieUser;
class CategoriesController extends Controller
{
public function __construct()
{
$this->middleware('sentinel.auth');
}
public function index()
{
$categories = Categories::all();
return view('user.categories.index', ['categories' => $categories]);
}
public function create()
{
$sections = sections::all();
return view('user.categories.create', ['sections' => $sections]);
}
public function store(Request $request)
{
// records in table categories
$categories = new Categories();
$categories->name = $request->name;
$categories->sections_id = $request->sections_id;
$categories->save();
// records in pivot table users_categories
$user = Sentinel::getUser()->id;
$users_categories = new CategorieUser();
$users_categories->user_id = $user;
$users_categories->categorie_id = $categories->id;
$users_categories->save();
return redirect()->route('categories.index');
}
}
This is the form:
<form action="store" method="POST">
<div class="form-group">
<label for="section">Choose section:</label>
<select class="form-control" name="sections_id">
#foreach($sections as $section)
<option value="{{ $section->id }}">{{ $section->name }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="name">Category name:</label>
<input type="text" class="form-control" name="name" required>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-default">Submit</button>
</form>
This is model Categories.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Categories extends Model
{
public function sections()
{
return $this->belongsTo('App\Models\Sections');
}
public function users()
{
return $this->belongsToMany('App\Models\Users', 'categorie_user');
}
}
And this is model Users.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Users extends Model
{
public function categories()
{
return $this->belongsToMany('App\Models\Categories', 'categorie_user');
}
}
When I add this route under routes as above
Route::group(['prefix' => 'user'], function () {
Route::resource('categories', 'User\CategoriesController');
Route::post('categories/store', ['uses' => 'User\CategoriesController#store']);
});
then everything works like a charm. I'm newbie in Laravel but I think that everything must work with out that route because I use restfull controller. Any sugestions I will appriciated. Thank you.
Finally, I find the answer. According to RESTfull resource controller the URL for form action must be the main route, not /store or /create. So in form action I wrote:
<form action="/user/categories" method="POST">
And that worked for me. Anyway, thanks for help. I hope this will help someone.
change the form action to "/user/categories" like
<form action="/user/categories" method="POST">
Im trying to POST data to my controller but I'm getting a
MethodNotAllowedHttpException in RouteCollection.php line 219:
error message, here are my files.
my route file
<?php
Route::get('/', function () {
return view('welcome');
});
// Authentication routes
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
// Registration routes
Route::get('register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::controllers(['password' => 'Auth\PasswordController',]);
Route::get('/home', 'HomeController#index');
// Using A Route Closure
Route::get('profile', ['middleware' => 'auth', function() {
// Only authenticated users may enter...
Route::auth();
}]);
// practicing using forms for sending data to the DB & populating form fields with DB data
Route::get('profile', 'ProfileController#index');
Route::post('profile/update', 'ProfileController#updateProfile');
profile.blade.php
<form method="POST" action="/profile/update/">
<div class="form-group hidden">
<input type="hidden" name="id" value="<?php echo $users[0]->id;?>">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
</div>
<div class="form-group">
<label for="email"><b>Name:</b></label>
<input type="text" name="name" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<label for="email"><b>Email:</b></label>
<input type="text" name="email" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default"> Submit </button>
</div>
</form>
& my ProfileController.php
<?php
namespace App\Http\Controllers;
use Auth;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ProfileController extends Controller
{
/**
* Update user profile & make backend push to DB
**/
public function index()
{
if(Auth::check()) {
// connecting to the DB and accessing
$users = User::all();
//var_dump($users);
return view('profile', compact('users'));
}
return view('auth/login');
}
public function updateProfile(Requests $request) {
return $request->all();
}
}
not sure what the issue is. Thanks for all the help everyone
A couple of issues that we managed to address here:
Over-use of HTTP Verbs
At your view, you have: <form method="POST" but also <input name="_method" type="hidden" value="PATCH"> which may conflict between a POST and a PATCH. Since your routes.php only declares POST, let's remove the patch definition.
Routing Mistake
Still at your view, your action points to action="/profile/update/" while your route is defined as Route::post('profile/update'), notice the extra / at the end in your form. That slash should not be there.
Controllers Request
You have a here: use App\Http\Requests; is probably incorrect because that's a folder within Laravel, not a class. Let's remove that and keep use Illuminate\Http\Request; for now. In the near future, you'll be learning how to create your own Form Requests and you'll probably want a UpdateProfileRequest.php.
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');
}
}
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"
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! :)
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');
}