POST Method in route gives error 404 - Laravel - laravel

I want to generate a random password on button click but it leads to Error 404
The Form
<form style="position: relative; left: -15px" action="{{ route("dashboard.users.generate-app-password-store") }}" method="POST">
#csrf
<button type="submit" class="btn btn-primary float-right mt-2">
{{ trans("translation.generate-password") }}
</button>
</form>
The Controller Function
public function generateAppEmailPasswordStore(Request $request)
{
$user = User::findOrFail($request->uid);
$user->app_email_password_store = Hash::make(Str::random(8));
$user->app_email_password_store = $request->app_email_password_store;
$user->save();
Alert::flash(trans('translation.email-account-created'));
return redirect()->route('dashboard.users.profile', ['id' => $user->id]);
}
The route (prefix => dashboard is the main route group)
Route::group(['prefix' => 'user', 'middleware' => 'checkRole:admin'], function () {
Route::post('/generate-app-password-store', 'Dashboard\UsersController#generateAppEmailPasswordStore')->name('dashboard.users.generate-app-password-store');
});
The error

You are probably recieving 404 because of this line in the controller method.
$user = User::findOrFail($request->uid)
Check if you get the correct value for $request->uid or if there is a user with the given ID.
Otherwise it will throw a 404 error.

either change your dashboard.user.generate-app-password-store (instead of dashboard.users.generate-app-password-store) in your view
or
change your route prefix to users (instead of user)

The problem seems to be that you mismatch the " usage.
action="{{ route("dashboard.users.generate-app-password-store") }}"
should be
action="{{ route('dashboard.users.generate-app-password-store') }}"
The reasoning is that you close the action too early.
action="{{ route("

Related

why Resource Controllers not working in laravel 8?

route link
Delete
define route
Route::resource('users', UserController::class);
controller function
public function destroy($id)
{
$delete = DB::table('users')->delete($id);
if ($delete) {echo 'success';}
}
in the delete route, the method should be delete, you can write it in this way
<form action="{{ route('users.destroy', $user->id) }}" method="post">
#csrf
#method('delete')
<button type="submit">Delete</button>
</form>
first of all you need to find specific id then delete the same one
$user = User::findOrFail($id);
$user ->delete();

Route [tasks.complete] not defined. (View: /Users/pathparakh/Projects/task/resources/views/tasks/index.blade.php)

This is my TaskController where complete() function is defined to store done_at current time
public function complete($id)
{
$task = Task::create([
'done_at' => now(),
]);
return redirect()->route('tasks.index')->withSuccess('Done');
}
This is my web.php route
Route::post('/tasks/complete', 'TaskController#complete');
This is my index page where there is submit button to save current time
<form action="{{ route('tasks.complete', $task->id) }}" method="post">
#csrf
<input type="submit">
</form>
You missed name declaration of your route, like this:
Route::post('/tasks/complete', 'TaskController#complete')->name('tasks.complete');
Note: If you want to add new task use the first one, but if you want to update an existing task use the second one:
//---First:
TaskController:
public function complete() {
$task = Task::create([
'done_at' => now(),
]);
return redirect()->route('tasks.index')->withSuccess('Done');
}
web.php
Route::post('/tasks/complete', 'TaskController#complete')->name('tasks.complete');
Your view file(index page):
<form action="{{ route('tasks.complete') }}" method="post">
#csrf
<input type="submit">
</form>
//---Second:
Or if you need to pass the id parameter in your controller you should use the below code:
TaskController:
public function complete($id) {
$task = Task::findOrFail($id);
if ($task) {
$task->update([
'done_at' => now()
]);
return redirect()->route('tasks.index')->withSuccess('Done');
}
return redirect()->route('tasks.index')->withSuccess('Task No Found');
}
web.php
Route::post('/tasks/complete/{id}', 'TaskController#complete')->name('tasks.complete');
Your view file(index page):
<form action="{{ route('tasks.complete', ['id' => $task->id]) }}" method="post">
#csrf
<input type="submit">
</form>
As per your complete() function in the controller and as per the form action,
Your route should look like this:
Route::post('/tasks/complete/{id}', 'taskcontroller#complete')->name('tasks.complete');

Trying to get property 'username' of non-object after POST to external website and redirect back to my website in LARAVEL

Auth::user() has a POST request to an external website with the following:
postfile.blade.php
<form action="{{ url('https:www.external.com/api/') }}" method="POST" align="center">
#csrf
#method('POST')
<input type="hidden" name="Data1" value="{{ $Data1}}">
<input type="hidden" name="Data2" value="{{ $Data2}}">
<input type="hidden" name="Data3" value="{{ $Data3}}">
<input type="hidden" name="ReturnURLOK" value="{{ url('/success') }}">
<input type="hidden" name="ReturnURLError" value="{{ url('/fail') }}">
<button type="submit" value="POST TO API">PROCEED/button>
</form>
After which, https:www.external.com/api/ provides a POST request back to Auth::user() url wherein the data are saved on database.
Controller:
public function postfiles(Request $request)
{
$request->session()->put('user_id',Auth::user()->id);
return view('postfile');
}
public function parse(Request $request)
{
$files = File::create([
'Data4' => $request->input('Data4'),
'Data5' => $request->input('Data5'),
'Data6' => $request->input('Data6'),
]);
$data = array(
'Data4' => $request->input('Data4'),
'Data5' => $request->input('Data5'),
'Data6' => $request->input('Data6'),
);
Auth::loginUsingId($request->session()->get('user_id'));
return view('success')->with($data);
}
The data are being saved correctly but in the success.blade.php I'm getting an error when I try to display the data on the blade.
Error is
Trying to get property 'username' of non-object
success.blade.php
<div class="container">
<div>
<h3>CONGRATULATIONS {{ Auth::user()->username}}!</h3>
<h2>The following Data has been saved</h2>
{{ $Data4}}
{{ $Data5}}
{{ $Data6}}
</div>
</div>
Routes:
Route::group(['middleware' => ['auth', 'activated', 'currentUser']], function () {
Route::get('postfile', 'App\Http\Controllers\FileController#postfiles')->name('postfile');
Route::POST('success', 'App\Http\Controllers\FileController#parse')->name('parse');
});
It seems the user's session is somehow lost and Auth::user() becomes null after being redirected back during the POST request from the external website.
i dont know why your logged-in user is lost but:
one solution i can say is that you can save id of that user somewhere like coockie or session and after redirecting back from your api, you get that id and re-login that user

POST method not supported for route in Laravel 6

I am building a discussion form in Laravel 6. The route I used is a POST method and I checked it in route:list. I get the following error, why?
The POST method is not supported for this route. Supported methods:
GET, HEAD, PUT, PATCH, DELETE
View
<form action="{{ route('replies.store', $discussion->slug) }}" method="post">
#csrf
<input type="hidden" name="contents" id="contents">
<trix-editor input="contents"></trix-editor>
<button type="submit" class="btn btn-success btn-sm my-2">
Add Reply
</button>
</form>
Route
Route::resource('discussions/{discussion}/replies', 'RepliesController');
Controller
public function store(CreateReplyRequest $request, Discussion $discussion)
{
auth()->user()->replies()->create([
'contents' => $request->contents,
'discussion_id' => $discussion->id
]);
session()->flash('success', 'Reply Added.');
return redirect()->back();
}
You passed a disccussion object as parameter in order to store user_id within an array.
I think this is not a good practice to store data.
You might notice that your routes/web.php and your html action are fine and use post but you received:
"POST method not supported for route in Laravel 6". This is runtime error. This probably happens when your logic does not make sense for the compiler.
The steps below might help you to accomplish what you want:
1. Eloquent Model(App\Discussion)
protected $fillable = ['contents'];
public function user(){
return $this->belongsTo('App\User');
}
2. Eloquent Model(App\User)
public function discussions(){
return $this->hasMany('App\Discussion');
}
3. Controller
use App\Discussion;
public function store(Request $request){
//validate data
$this->validate($request, [
'contents' => 'required'
]);
//get mass assignable data from the request
$discussion = $request->all();
//store discussion data using discussion object.
Discussion::create($discussion);
session()->flash('success', 'Reply Added.');
return redirect()->back();
}
4. Route(routes/web.php)
Route::post('/replies/store', 'RepliesController#store')->name('replies.store');
5. View
<form action="{{ route('replies.store') }}" method="post">
#csrf
<input type="hidden" name="contents" id="contents">
<trix-editor input="contents"></trix-editor>
<button type="submit" class="btn btn-success btn-sm my-2">
Add Reply
</button>
</form>

post form action to controller in laravel

First of all I want to say that my problem is very similar to this question, only the answer is not working in my case.
I am trying to connect my form action to my UserController. it's an update avatar function. Here's what it look like.
in my profile/show.blade
<div class="col-md-12 justify-content-center">
<form action="{{ action('UsersController#update_avatar') }}" method="post" enctype="multipart/form-data">
#csrf
</form>
</div>
UsersController
public function avatar()
{
$user = Auth::User();
return view('dashboard.profile'.$user->id,compact('user',$user));
}
public function update_avatar(Request $request){
$request->validate([
'avatar' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$user = Auth::User();
$folder = 'avatars';
Storage::delete($folder.'/'.$user->avatar);
$avatarName = $user->id.'_avatar'.'.'.request()->avatar->getClientOriginalExtension();
$request->avatar->storeAs('avatars',$avatarName);
$user->avatar = $avatarName;
$user->save();
return back()
->with('success','You have successfully upload image.');
}
routes/web.php
Route::get('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#avatar');
Route::post('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#update_avatar');
here's the error I am receiving
Thank you so much in advance!
Change these routes:
Route::get('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#avatar');
Route::post('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#update_avatar');
To these:
Route::get('dashboard/profile/{{profile}}', 'UsersController#avatar')->name('user.avatar');
Route::post('dashboard/profile/{{profile}}', 'UsersController#update_avatar')->name('user.update_avatar');
And in your form use route instead of action
<form action="{{ route('user.update_avatar') }}" method="post" enctype="multipart/form-data">
#csrf
</form>
You can try do something like this:
Route::post('dashboard/profile','Dashboard\UsersController#update_avatar')->name(profile.update);
and use it in the form like this:
<form action="{{ route('profile.update') }}">

Resources