Laravel issue with route and HTML form action - php

I'm having some trouble with my form, on submit I get the error 'Whoops, looks like something went wrong.'.
I'm using Laravel 4.2, my routes look like this:
Route::get('/', function()
{
return View::make('index');
});
Route::post('/', array('as' => 'login', 'uses' => 'HomeController#login'));
And my form looks like this:
<form action="{{ action('HomeController#login') }}" method="post">
<input class="signUpField-index" id="signUpEmail-index" type="text" placeholder="Email Address (required, but never shown) *" name="email" />
<input class="signUpField-index" id="signUpPassword-index" type="password" placeholder="Password *" name="password" />
<input id="signUpSubmit-index" type="submit" value="Sign Up" />
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
</form>
And my controller looks like this:
<?php
class HomeController extends BaseController {
public function showIndex()
{
return View::make('index');
}
public function login() {
//return var_dump(_POST);
return View::make('index');
}
}
I think it might be the action that is incorrect but I am not too sure, I've tried to look at other examples and tutorials like here: Adding form action in html in laravel, but they have not helped.
Thanks in advance.

Its not action,its url.
Use:
<form url="your action" method="post">
</form>
But if you want to stay on a same page,use Ajax for submit.

Related

404 NOT FOUND EDIT AND UPDATE LARAVEL 6

while passing variable in URL to edit and update a form it's returning only 404 not found , the tutorials did not help me , so this is my code :
controller : rendezv.php
public function editer ($id) {
$rdv= rendezvous::findOrFail('id');
return view ('/edit', ['modifier'=>$rdv]);
}
public function update(Request $request ,$id)
{
$this->validate($request, [
'email' => 'required' ,
'tel' => 'required'
]);
//modifier rendez vous
$editer=rendezvous::findOrFail('id');
$editer->Email = $request->input('email');
$editer->tel = $request->input('tel');
$editer-> save();
return redirect('/index');
}
and this this edit.blade.php
<form action="/update/{{$modifier->id}}" method="post" role="form" data-aos="fade-up">
#csrf
<input type="hidden" name="_method" value="PATCH" />
<input placeholder="{{$modifier->Email}}" type="email" class="form-control" name="email" id="email" data-msg="Please enter your name " />
<input placeholder="{{$modifier->Numéro_de_téléphone}} " type="text" class="form-control" name="tel" id="subject" data-rule="minlen:8" data-msg="Please enter at least 8 numbers" /> </i>
<div id="buttons">
<button type="submit" class="btn btn-primary"> modifier </button>
</div>
</form>
and finally route :
Route::get('/rendezvous_{ID}', 'doctor#rdv');
Route::post('/rdv','rendezv#rdv');
Route::post('/bienvenu','doctor#authentification')->name('aziz');
Route::get('/edit/{id}','rendezv#editer');
need yr help guys , and thank you
Please add the route for update
Route::patch('/update/{id}','rendezv#update');
You get 404 for both edit and update for findOrFail() method. You are passing string 'id' instead of $id.
In editer method please replace
$rdv= rendezvous::findOrFail('id');
with
$rdv= rendezvous::findOrFail($id);
In update method please replace
$editer=rendezvous::findOrFail('id');
With
$editer=rendezvous::findOrFail($id);
Furthermore, findOrFail() method will return 404 if no data is found with the given $id
You route /update/{{$modifier->id}} doesn't exist, you need to declare it in you router file:
Route::post('/update/{id}','rendezv#update');
Take a look at the Resource Controllers
What you are looking for is a Route::post('/edit/{id}','rendezv#update'); or put or patch
You are missing a post route:
Route::post('/edit/{id}','rendezv#update');

Can't figure out why my form is not updating the values in the database

I have a project that is going to have 8 different forms all updating the same 'user' table in my database. I have the user authentication working and it makes a user in the table on my localhost mysql database. However when I start updating the table I keep getting errors such as email is not unique or http errors or ReflectionException in RouteDependencyResolverTrait.php line 57: Internal error: Failed to retrieve the default value.
I have tried everything, my create works but it makes a new row and doesn't update the existing row which the user is signed in on.
I'm only new to Laravel 5.4 and finished going through all the Laracasts, so I'm absolutely stumped at what to do.
Does anyone have any thoughts or know how to fix it or restructure it better? Please let me know if I have missed anything out. I have been trying to get this working for 2 days.
Basics.php
<?php
namespace App;
class Basics extends Model
{
public $table = "users";
protected $fillable = [
'family_name',
'given_names'
];
}
BasicsController.php
class BasicsController extends Controller
{
public function index()
{
$user = \Auth::user();
return view('/details/basics', compact('user'));
}
public function update(Request $request, $id)
{
$basics = Basics::find($id);
$basics->family_name = $request->input('family_name');
$basics->given_names = $request->input('given_names');
$basics->save();
return redirect("/details/basics");
}
}
basics.blade.php
#extends ('layouts/app')
#section ('content')
{{--Do #includes for all form components with the components file--}}
#include ('layouts/header')
<main class="main">
<form action="/details/basics" method="POST">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<fieldset>
<label>Family name</label>
<input type="text" name="family_name" placeholder="Family name" value="{{ old('family_name') }}" />
</fieldset>
<fieldset>
<label>Given names</label>
<input type="text" name="given_names" placeholder="Given names" value="{{ old('given_names') }}" />
</fieldset>
<button type="submit" value="Save" name="save" class="button button-primary button-wide">Save</button>
</form>
</main>
#endsection
web.php
Route::get('/', function () {
return view('welcome');
});
// Authentication Routes
Auth::routes();
Route::get('/logout', 'Auth\LogoutController#destroy');
Route::get('/home', 'DashboardController#index');
Route::get('/dashboard', function () {
$user = Auth::user();
return view('dashboard', compact('user'));
});
// Eligibility Assessments
Route::get('/assessment/student', 'AssessmentController#index');
Route::post('/assessment/results', 'AssessmentController#store');
// Details
Route::get('/details/basics', 'BasicsController#index');
Route::patch('/details/basics', 'BasicsController#update');
You need to add a rule in your post request which will exclude the email field when updating the model. This is why you're getting the email is not unique error. Although you're not posting the email field but still the save method is doing that. Try using post instead of patch. Just for debugging purposes in your Route.php

Laravel 5.1 Trying to post data to controller but geting MethodNotAllowedHttpException error

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.

Laravel form post to controller

I am new to Laravel and I'm having trouble with posting data to a controller. I couldn't find the corresponding documentation. I want to something similar in Laravel that I do in C# MVC.
<form action="/someurl" method="post">
<input type="text" name="someName" />
<input type="submit">
</form>
Controller
[HttpPost]
public ActionResult SomeUrl(string someName)
{
...
}
You should use route.
your .html
<form action="{{url('someurl')}}" method="post">
<input type="text" name="someName" />
<input type="submit">
</form>
in routes.php
Route::post('someurl', 'YourController#someMethod');
and finally in YourController.php
public function someMethod(Request $request)
{
dd($request->all()); //to check all the datas dumped from the form
//if your want to get single element,someName in this case
$someName = $request->someName;
}
This works best
<form action="{{url('someurl')}}" method="post">
#csrf
<input type="text" name="someName" />
<input type="submit">
</form>
in web.php
Route::post('someurl', 'YourController#someMethod');
and in your Controller
public function someMethod(Request $request)
{
dd($request->all()); //to check all the datas dumped from the form
//if your want to get single element,someName in this case
$someName = $request->someName;
}

How to get data transfer from the form into database Laravel 5

I'm trying to transfer data from the form to overwrite the content database .But get the error.
It is my route,method and form.
Route::get('edit/{id}','JokeController#edit');
public function edit(JokeModerateRequest $request,$id) {
$joke = Joke::findOrFail($id);
return view('jokes.edit', [ 'joke' => $joke ]);
}
<form action="/update" method="post">
<input type="text" value="{{ $joke -> content}}" name="body">
<input type="submit" value="Save">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
But when I try use next route and method
Route::post('update/{id}','JokeController#update');
function update(JokeModerateRequest $request,$id)
$joke = Joke::findOrFail($id);
$joke->content = $request -> get('content');
$joke->save();
return back();
I have next error
Sorry, the page you are looking for could not be found.
1/1
NotFoundHttpException in RouteCollection.php line 145:
The problem lies here:
<form action="/update" method="post">
This will redirect to /update route (which you haven't defined) instead of /update/id. Those two are different routes.
To fix this, change action of the form to:
<form action="/update/{{ $joke->id }}" method="post">

Categories