Laravel NotFoundHttpException although route exits - laravel

I added a new route as:
Route::post('friendSend/{uid}','FriendController#sendFriendRequest')->name('friends.add');
and called it as a hyperlink to submit form:
<a href="{{route('friends.add',$user->uid)}}"
onclick="event.preventDefault();
document.getElementById('addfriend-form).submit();">
<i class="glyphicon glyphicon-facetime-video" style="color:#F44336;"></i> Add Friend
</a>
<form action="{{route('friends.add',$user->uid)}}" method="post" id="addfriend-form">
{{ csrf_field() }}
</form>
However when I click on the said link, I get redirected to /friendSend with the said error.
the route is visible in:
php artisan route:list
which makes sense since I called it via it's name 'friends.add'. It doesn't even go the controller.
I've already tried the following:
Laravel NotFoundHttpException although route exists
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Friend;
use Auth;
class FriendController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return redirect()->route('home');
}
public function sendFriendRequest($id)
{
echo "hello world";
}
}
Update:
Manually entering the url as /friendSend/2 (or any number for that matter) works.

You are missing a ' in the onclick javascript.
<a href="{{route('friends.add',$user->uid)}}"
onclick="event.preventDefault();
document.getElementById('addfriend-form').submit();">
<i class="glyphicon glyphicon-facetime-video" style="color:#F44336;"></i> Add Friend
</a>
<form action="{{route('friends.add',$user->uid)}}" method="post" id="addfriend-form">
{{ csrf_field() }}
</form>
If this doesn't work, what is the href on the generated page? Do you have multiple of these forms on a single page?

I thing you are doing wrong on set the url in form submit.You are adding route like
href="{{route('friends.add',$user->uid)}}"
Just modified it as
href="{{route('friends.add',['uid' => $user->uid])}}"
You can refer Laravel Named Routes

Related

Laravel Breeze logout redirect on 127.0.0.1 on shared host

I have domain for example aaa.com. And I deploy Laravel on my webhost succesfully. Login, pages, all things etc. works fine. but whenever I logout it redirects me to 127.0.0.1 not aaa.com. Of course, I have to point out that I am using Laravel Breeze
and here is what I wrote:
my logout form inside any page.
<form method="POST" action="{{ route('logout') }}">
#csrf
<button type="submit" class="underline text-sm text-gray-600 hover:text-gray-900">
{{ __('Log Out') }}
</button>
</form>
My web.php include require __DIR__.'/auth.php'; . Does not contain any logout redirects.
and inside auth.php
Route::post('logout', [AuthenticatedSessionController::class, 'destroy'])
->name('logout');
and finally AuthenticatedSessionController.php
public function destroy(Request $request)
{
Auth::guard('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/');
}
I don't understand why I am being redirected to 127.0.0.1 instead of aaa.com?
Edit:
and forgot to mention my .env file include
APP_URL=https://aaa.com
In .env file, Change APP_URL
APP_URL=http://aaa.com
Thank you to everyone who replied. It fixed itself the next day. It must be something left in the web host's cache, or your browser's. It's not a Laravel thing.

The #edit Route is not working... showing 404 not found

the #edit route is not creating. everything is ok but showing 404 not found
the problem is on the last route. I ran the php artisan route:list code but not showing any route named /profile/{user}/edit
the web.php code ->>>>>
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProfilesController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/p/{post}','App\Http\Controllers\PostsController#show');
Route::get('/p/create','App\Http\Controllers\PostsController#create');
Route::post('/p','App\Http\Controllers\PostsController#store');
Route::get('/profile/{user}', [App\Http\Controllers\ProfilesController::class, 'index'])->name('profile.show');
Route::get('/profile/{user}/edit','ProfilesController#edit')->name('profile.edit');
the profiles controller code ->>>>>
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function edit(User $user)
{
return view('profiles.edit', compact('user'));
}
public function index(User $user)
{
return view('profiles.index', compact('user'));
}
}
the index.blade.php code --->>>>>>>>>>>>>>>
#extends('layouts.app')
#section('content')
<div class="container">
<div><h1>here is a big design</h1></div>
<br>
<div class="d-flex justify-content-between align-items-baseline">
<h1>{{ $user->username}}</h1>
Add New Post
</div>
Edit Profile
<div class="pr-5"><strong>{{$user->posts->count()}}</strong> posts</div>
<div class="pr-5"><strong></strong> followers</div>
<div class="pr-5"><strong></strong> following</div>
<br>
<div><h1>{{ $user->profile->title}}</h1></div>
<br>
<div><h1>{{ $user->profile->description}}</h1></div>
<br>
<div><h1>{{ $user->profile->url ?? 'N/A'}}</h1></div>
<h1>Posts</h1>
<hr>
<div class="row pt-5">
#foreach($user->posts as $post)
<div class="col-4 pb-4">
<a href="/p/{{$post->id}}">
<img src="/storage/{{$post->image}}" class="w-100">
</a>
</div>
#endforeach
</div>
</div>
#endsection
Change your route to
Route::get('/profile/{user}/edit',[ProfilesController::class, 'edit'])->name('profile.edit');
Your way 'ProfilesController#edit' doesn't take use App\Http\Controllers\ProfilesController; into account.
Offtopic suggestion: since you already named your routes I suggest you use the named routes in your blade files instead of hardcoding them:
Edit Profile
instead of
Edit Profile
This way, should you decide to change a URL some time later, you only need to change it in your web.php file once, not all of your hardcoded occurences

multiple variables in routes. A route isn't leading to the controller defined blade

I have "show" and "edit" two routes. It's showing two different url. But "edit" route is using the 'show' blade, which is the SAME as the "show" route. How to lead the "edit" route to the 'edit' blade?
here is the web.php:
Route::get('/{user}/{course}', 'CoursesController#show')->name('course.show');
Route::get('/{user}/edit_{course}', 'CoursesController#edit')->name('course.edit');
here is the controller:
public function edit(Course $course) {
return view('courses.edit', compact('course'));
}
public function show(Course $course) {
return view('courses.show', compact('course'));
}
here is the index blade:
<a class="btn btn-xs btn-primary" href="{{ route("course.show", [auth()->user()->username, $course->title. $course->id] ) }}"> VIEW </a>
<a class="btn btn-xs btn-info" href="{{ route("course.edit", [auth()->user()->username, $course->title. $course->id]) }}"> EDIT </a>
Change the order or your routes in your routes files web.php
Route::get('/{user}/edit_{course}', 'CoursesController#edit')->name('course.edit'); //This one goes first.
Route::get('/{user}/{course}', 'CoursesController#show')->name('course.show');
Your show route is working as a wildcard if you invert the order as shown the edit route will catch it first when the second variable begins with edit_.

Form not showing due to routing problem in action parameter

I am working on an Excel import module as part of a CRM for my company. I want to import an excel sheet. I use the Maatwebsite Excel package, version 3.1. I want to show the form and then upload a sheet. However I can't even get to that point. I have already determined the issue is within the form route, just not sure what it is exactly that I am missing.
Routes that I use to display the page (index works fine)
Form used to get the Excel sheet imported
Navigation bar link in the menu
DataController (from which I am trying to call the import method)
If you know what may be wrong please do tell, this is really frustrating!
Route code:
Route::get('importeren', 'Datacontroller#index');
Route::post('import', 'Datacontroller#import');
<div class="container-fluid">
<form action="import" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="import_file">
<br>
<input type="submit" value="Import">
</form>
</div>
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Imports\DataImport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class DataController extends Controller
{
public function index(){
return view('importeren');
}
public function import(Request $request){
Excel::import(new DataImport(), $request->file('import_file'));
return redirect()->route('/home');
}
}
Don't use route() method because you're not defining any routes name, use something like:
form action="/import" method="POST" enctype="multipart/formdata">
you should use route() only with route name, use this instead :
return redirect('/home');
You can give a name to your route:
Route::post('import', 'Datacontroller#import')->name('import');
and leave the form action as it is with the route() helper:
<div class="container-fluid">
<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="import_file">
<br>
<input type="submit" value="Import">
</form>
</div>
You could also use the url() helper and leave the route with no name, but I highly recommend the option of giving your route a name.
<form action="{{ url('import') }}" method="POST" enctype="multipart/form-data">
Note that in the controller return you are also using a redirect to a named route, so I suggest that you give that route a name and use that name in the redirect. For example:
Route::get('home', 'SomeController#someMethod')->name('home');
and
return redirect()->route('home');

Laravel 5.5 - Passing form's Data to Controller then Passing Data from Controller to the same View

I give up ! 2days i'm looking for solution for my problem.
All I need is pass FORM Data to Controller, then just show it on the same View.
Easy, right ? But i can not find any solution and Laravel's Manual does not explain that clearly...
So this is my View with the form :
form.blade.php
<form action="{{ action('FormController#ReceiveDataForm') }}" method="post">
<input type="text" name="name" placeholder="Enter your name">
<input type="text" name="surname" placeholder="Enter your surname">
<input type="submit" value="Send">
</form>
<div class="ShowDataHere">
{{-- Here i want to show this Data from FORM above, but with using Controller. --}}
</div >
My Controller receiving data:
FormController.php
namespace App\Http\Controllers;
use Input;
use Illuminate\Http\Request;
class FormController extends Controller
public function Form()
{
return view('test.form');
}
public function ReceiveDataForm()
{
Input::post('name');
Input::post('surname');
}
And my question is how to pass this Data to the same View and show it on
user's screen ?
Please note that Data must be basically pushed to the Controller, just then passing to the View via Routing.
All solution i found in Internet does not work for me, what guys am i doing wrong ?
If you do not know proper answer, please direct me where to find it or similar.
Thank You !
You can do
public function ReceiveDataForm(Request $request)
{
//Input::post('name');
//Input::post('surname');
return view('test.form');
}
Import the Request class by adding use Illuminate\Http\Request; in your controller.
In your view
<div class="ShowDataHere">
#if(!empty(request()->all()))
{{ request()->name }}
#endif
</div >
In your web.php file, make sure you have
Route::post('/test', 'FormController#ReceiveDataForm');

Resources