post form action to controller in laravel - 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') }}">

Related

Laravel 9 how to pass parameter in url from one controller to another controller?

I was facing this problem of missing parameter when trying to pass a parameter from one controller to another controller. The parameter is $id whereby the data is originally from post method in details blade.php into function postCreateStepOne However, I want to pass the data into a new view and I return
redirect()->route('details.tenant.step.two')->with( ['id' => $id]
);}
And this is where error occur. However, it works fine if I skip it into a new route and directly return into a view with the compact parameter. For Example,
return view('document.details-step-two', compact('id', 'property'));
However, I would prefer a new url as I was doing multistep form using Laravel.
Error
web.php
Route::get('/document/details/viewing/{id}', 'ViewDetails')->name('details.tenant');
Route::post('/document/details/viewing/{id}', 'postCreateStepOne')->name('post.step-one');
Route::get('/document/details/viewing/step-2/{id}', 'ViewDetailsStep2')->name('details.tenant.step.two');
TenanatController.php
public function viewDetails($id){
$view = Properties::findOrFail($id);
return view('document.details', compact('view'));
}
public function ViewDetailsStep2(Request $request, $id){
$view = Properties::findOrFail($id);
$property = $request->session()->get('property');
return view('document.details-step-two', compact('view', 'property'));
}
public function postCreateStepOne($id, Request $request)
{
$validatedData = $request->validate([
'property-name' => 'required',
]);
if(empty($request->session()->get('property'))){
$property = new Tenancy();
$property->fill($validatedData);
$request->session()->put('property', $property);
}else{
$property = $request->session()->get('property');
$property->fill($validatedData);
$request->session()->put('property', $property);
}
return redirect()->route('details.tenant.step.two')->with( ['id' => $id] );
}
details.blade.php
<form action="{{ route('post.step-one', $view->id) }}" method="POST">
#csrf
<div class="card-body">
<div class="form-group">
<label for="title">Property Name:</label>
<input type="text" value="" class="form-control" id="property-name" name="property-name">
</div>
</div>
<div class="card-footer text-right">
<button type="submit" class="btn btn-primary">Next</button>
</div>
</form>
When you use with on a redirect the parameter is passed through the session. If you want to redirect to a route with a given route parameter you should pass that parameter in the route function itself like e.g.
return redirect()->route('details.tenant.step.two', ['id' => $id]);

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();

404 not found on laravel post request

I'm making a post request in my form. The csrf token and the method are there.
<div class="flex">
<form action="{{ route('profile.store.follower', $user) }}" method="post">
#csrf
<input value="follow" type="hidden" name="follow" value="{{$user->id}}" >
<button type="submit" class="bg-green-700">Follow</button>
</form>
</div>
The route:
Route::get('/usersprofile/{user:name}/index', [ProfileController::class, 'index'])->name('profile.index');
Route::post('/usersprofile/{user:name}/index', [ProfileController::class, 'store'])->name('profile.store.follower');
The controller:
public function store(User $user, Request $request) {
dd($user);
$user = user::findOrFail($request->follow);
Auth::user()->following->attach($user->id);
return back();
}
The following function in the user model:
public function following() {
return $this->belongsToMany(User::class, 'followers', 'user_id', 'following_id');
}
For some reason, it's not going through. I've tried php artisan route:cache and config:cache but the error persists. I've checked the route list and the route exists.
Your hidden input has two value attributes, that's probably why it doesn't find a user and fails.
Remove the hidden input and the line $user = user::findOrFail($request->follow); - the user you want to follow already exists in $user via your route. Then use Auth::user()->following->attach($user); to follow.
<div class="flex">
<form action="{{ route('profile.store.follower', $user) }}" method="post">
#csrf
<button type="submit" class="bg-green-700">Follow</button>
</form>
</div>
public function store(User $user, Request $request)
{
Auth::user()->following->attach($user);
return back();
}

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');

Form action not working in laravel

I want to insert data in database. i found a problem. I have 2 blade.php page. 1. dashboard.blade.php and another is class.blade.php.
When i use in my form action dashboard.blade.php its working. but when i used in action class.blade.php its not working. i can't found this problem. Here is my form action :
<form class="form-horizontal" role="form" method="POST" action="{{ url('/dashboard') }}">
{!! csrf_field() !!}
when i changed it
<form class="form-horizontal" role="form" method="POST" action="{{ url('/class') }}">
{!! csrf_field() !!}
then its not working.
Here is my controller
public function showclass(Request $request)
{
$randomnumber = rand(50001,1000000);
$classrooms = new Classrooms();
$classrooms->class_name = $request['class_name'];
$classrooms->subject_name = $request['subject_name'];
$classrooms->section = $request['section'];
$classrooms->class_code = $randomnumber;
$classrooms -> user_id = Auth::user()->id;
$classrooms -> save();
Flash::success('Your status has been posted');
//return redirect(route('dashboard'));
return view('dashboard', array('classroom' => Auth::user()) );
}
There is an error in your Route. You have used showdata method instead of showclass. Just change it like below:
Route::post('/class', [
'uses' => 'classroom#showclass',
'as' => 'classrooms']);
Note: Make sure to specify specific method while defining the routes.

Resources