404 not found on laravel post request - laravel

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

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

Laravel - The GET method is not supported for this route. Supported methods: POST

I am a newbie. I'm having problems with Laravel doing the Login function. After login will redirect to URL /admin/dashboard but I get an error
The GET method is not supported for this route. Supported methods:
POST.
in web.php
Route::get('/login', [AdminController::class, 'index']);
Route::get('/logout', [AdminController::class, 'logout']);
Route::get('/dashboard', [AdminController::class, 'show_dashboard']);
Route::post('/admin/dashboard', [AdminController::class, 'dashboard']);
in controller
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use Session;
use Illuminate\Support\Facades\Redirect;
session_start();
class AdminController extends Controller
{
public function index() {
return view('admin.login');
}
public function show_dashboard() {
return view('admin.dashboard.index');
}
public function dashboard(Request $request) {
$email = $request->email;
$password = md5($request->password);
$result = DB::table('tbl_admin')->where('email', $email)->where('password', $password)->first();
if ($result) {
Session::put('id', $result->id);
Session::put('name', $result->name);
//return view('admin.dashboard.index');
return redirect('/admin/dashboard');
}
else {
Session::put('message', 'Email or password is incorrect.');
return redirect('/login');
}
}
public function logout() {
Session::put('id', null);
Session::put('name', null);
return Redirect::to('/login');
}
}
and in login.blade.php
<div class="log-w3">
<div class="w3layouts-main">
<h2>Sign In Now</h2>
<form action="{{ url('/admin/dashboard')}}" method="POST">
{{ csrf_field() }}
<input type="text" class="ggg" name="email" placeholder="E-MAIL" required="">
<input type="password" class="ggg" name="password" placeholder="PASSWORD" required="">
<?php
$message = Session::get('message');
if($message) {
echo $message;
Session::put('message', null);
}
?>
<span><input type="checkbox" />Remember Me</span>
<h6>Forgot Password?</h6>
<div class="clearfix"></div>
<input type="submit" value="Sign In" name="submit">
</form>
<p>Don't Have an Account ?Create an account</p>
</div>
and version laravel
"php": "^7.3|^8.0"
Can someone help me? Thanks.
You can change the redirect in the dashboard method to go to the GET route that returns the dashboard index instead of trying to redirect to a POST route. Change:
redirect('/admin/dashboard')
to:
redirect('dashboard')
Side notes:
This dashboard method on the Controller could probably be named something like login. Then show_dashboard could be dashboard.
You can remove the session_start() from the file. Laravel doesn't use PHP's sessions.

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') }}">

Laravel error: Call to a member function store() on null

When I tried to send any file I get this message
It's form in home.blade.php
<form action="{{ URL::to('/upload') }}" enctype="multipart/form-data" method="post">
<input type="file" name="something" >
{{ csrf_field() }}
<button type="submit" name="button">Upload</button>
</form>
It's function inside a Controller
public function store(Request $request)
{
$path = $request->file('something')->store('upload');
echo $path;
}
Try to change controller code like this and see the result:
public function store(Request $request) {
$path = $request->something;
echo $path; }

Resources