how to solve 404 error message in laravel 8 - laravel

i got error 404 while applying route model binding , i am confused where i went wrong, even though i checked route and controller are correct
this is my index coding
```
<h1 class="heading"> product <span>categories</span> </h1>
<div class="box-container">
#foreach($kategori as $k)
<div class="box">
<img src="image/cat-1.png" alt="">
<h3>{{$k->nama_kategori}}</h3>
<p>{{$k->deskripsi_kategori}}</p>
Lihat
</div>
#endforeach
</div>
```
my routes code
<?php
use App\Http\Controllers\HomepageController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('/', [HomepageController::class, 'index']);
Route::get('/{buku:slug}', [HomepageController::class, 'show']);
Route::get('/{kategori:slug_kategori}', [HomepageController::class, 'lihat']);
my controller code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Buku;
use App\Models\Kategori;
class HomepageController extends Controller
{
public function index() {
return view('homepage/index',[
"title" => "Homepage",
"books" => Buku::all(),
"kategori" => Kategori::all(),
]);
}
public function show(Buku $buku) {
return view('homepage/lihat', [
'title' => 'Detail Buku',
'book' => $buku
]);
}
public function lihat(Kategori $kategori) {
return view('homepage/kategori', [
'title' => $kategori->nama,
'kategoris' => $kategori,
]);
}
}
kategori migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('kategoris', function (Blueprint $table) {
$table->id();
$table->string('kode_kategori')->unique();
$table->string('nama_kategori')->unique();
$table->string('slug_kategori');
$table->text('deskripsi_kategori');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('kategoris');
}
};
and file views structure
file views structure
i got error 404 while applying route model binding , i am confused where i went wrong, even though i checked route and controller are correct

Try this artisan command
php artisan config:clear
php artisan route:clear

What's the route that shows a 404 page?, maybe it's because of the cache, try these commands:
for clear route cache: $ php artisan route:clear
for clear all cache app: $ php artisan optimize:clear

Related

larvael form submit to database giving error

i am new to laravel and i am trying to submit form into data base but i am getting error i dont know why
i have added the screen shot along with that controller
when i do dd($REQUEST->all()) i am getting the form data
<?php
namespace App\Http\Controllers;
use App\Inventory;
use Illuminate\Http\Request;
class InventoryController extends Controller
{
public function index(){
return view('invetory.index');
}
public function sales(){
return view('invetory.sale');
}
public function create(Request $REQUEST){
// dd($REQUEST->all());
inventories::Create($REQUEST->all());
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('/inventory', 'App\Http\Controllers\InventoryController#index')->name('map');
Route::get('/inventory/sales', 'App\Http\Controllers\InventoryController#sales');
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::post('/inventory', 'App\Http\Controllers\InventoryController#create')->name('invetory.create');
according to your code the controller should look like
<?php
namespace App\Http\Controllers;
use App\Models\Inventory;
use Illuminate\Http\Request;
class InventoryController extends Controller
{
public function index(){
return view('invetory.index');
}
public function sales(){
return view('invetory.sale');
}
public function create(Request $request){
Inventory::Create($request->all());
}
}
change the namesapce of inventory to use App\Models\Inventory;
and also , inside create function use :
Inventory::Create($request->all());

web.php seems to be okay but BindingResolutionException Target class does not exist. I am looking for a way to remove extra junk rubbish path

I use Laravel 6.2
web.php seems to be okay however, BindingResolutionException Target class [App\Http\Controllers\App\Stack\Http\Controllers\HomeController] does not exist.
I think "App\Http\Controllers" is reductant .
How should I remove this extra junk path?
error happens.
Where should fix it?
web.php
<?php
Route::get('/', function () {
return redirect('/home');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
HomeController.php
<?php
namespace App\Stack\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Stack\Http\Middleware\SetDefaultLayoutForUrls;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware(['auth', SetDefaultLayoutForUrls::class]);
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
the file is located at
{thisProject}/app/Http/Controllers
In web.php you say that the controller is at :
App\Stack\Http\Controllers\HomeController
but in your comment you say that the controller is at
{thisProject}/app/Http/Controllers
Try changing your homecontroller namespace to:
namespace App\Stack\Http\Controllers;
And your route to:
Route::get('/home', 'App\Stack\Http\Controllers\HomeController#index')->name('home');

How to debug this error 'The GET method is not supported for this route. Supported methods: POST.'?

I am trying to work with forms in laravel but I keep getting this error
Symfony \ Component \ HttpKernel \ Exception \
MethodNotAllowedHttpException The GET method is not supported for this
route. Supported methods: POST.
I have tried so many ways to solve it but ain't solving it
Here my model
create_posts_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('caption');
$table->string('image');
$table->timestamps();
$table->index('user_id');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
protected $fillable = ['caption', 'image'];
public function user(){
return $this->belongsTo(User::class);
}
}
Controller PostsController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function create(){
return view('posts.create');
}
public function store(Request $request){
$request->validate([
'caption' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg',
]);
Post::create($request->input());
dd($request->all());
}
}
Routes web.php
<?php
/*
|--------------------------------------------------------------------------
| 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!
j|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/p/create', 'PostsController#create');
Route::post('/p', 'PostsController#store')->name('p.store');
Route::get('/profile/{user}', 'ProfilesController#index')->name('profile.show');
Blade file:
<div class="container"> <form action="/p" enctype="multipart/form-data" method="post"> #csrf
Kindly Help me, I've been stack for 3 days because of that above error .
In the browser I have this
protected function methodNotAllowed(array $others, $method)
{
throw new MethodNotAllowedHttpException(
$others,
sprintf(
'The %s method is not supported for this route. Supported methods: %s.',
$method,
implode(', ', $others)
)
);
}
Edit: According to your comment, your <form> appears to be correct. Could you provide the Envoirement Details that you see on the Whoops! page? They can be found int the bottom right corner.
I assume that you have a html form from which you acces your PostsController. Your tag should look something like this: <form action="{{route('p.store')}}" method="post". You probably have method="get". If so, change the method to post, that should work.

Can't redirect users to custom URL after succesful login in Laravel

I am trying to redirect the users to a custom URL after a succesful login but it doesn't work. It keeps redirecting users to this dashboard page "/". I already deployed the website to the server so I can't clear the route cache with artisan.
Laravel version is 5.3.29
App\Http\Controllers\Auth\LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/my-profile';
protected $redirectPath = '/my-profile';
protected function redirectTo()
{
return '/my-profile';
}
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
App\Http\Middleware\RedirectIfAuthenticated.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check())
{
return redirect('/my-profile');
}
return $next($request);
}
}
I have read that there might be some problems with Laravel in this post. It says that there might be some problems with this file: /vendor/laravel/framework/src/Illuminate/Foundation/Auth/RedirectsUser.php, but this error was fixed in Laravel version 5.3.29, but I can see that it is fixed and 'returnTo' method should work.
/vendor/laravel/framework/src/Illuminate/Foundation/Auth/RedirectsUser.php
<?php
namespace Illuminate\Foundation\Auth;
use Illuminate\Support\Facades\Log;
trait RedirectsUsers
{
/**
* Get the post register / login redirect path.
*
* #return string
*/
public function redirectPath()
{
Log::info("RedirectsUsers");
if (method_exists($this, 'redirectTo')) {
return $this->redirectTo();
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/homeeeee';
}
}
And my routes file:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Laravel the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/
Auth::routes();
Route::get('/', 'HomeController#index');
Route::get('/home', 'HomeController#index');
Route::get('/confirm-your-email', 'Auth\ConfirmEmailController#confirm_email');
Route::get('/confirm-email/{register_token}', 'Auth\ConfirmEmailController#index');
Route::get('/my-profile', ['as' => 'my-profile' , 'uses' => 'MyProfileController#show', 'userAlert' => null]);
Route::post('/my-profile/edit-about', 'MyProfileController#editAbout');
Route::post('/my-profile/edit-profile-picture', 'MyProfileController#editProfilePicture');
Route::get('/search/company', 'SearchController#searchCompany');
Route::get('/my-profile/add-job', 'MyProfileController#addJobPage');
Route::get('/my-profile/add-job/{id}', 'MyProfileController#addCompanyJobPage');
Route::post('/my-profile/add-a-job', 'MyProfileController#addJob');
Route::post('/my-profile/delete-job', 'MyProfileController#deleteJob');
Route::get('/users/{id}', ['as' => 'users', 'uses' => 'UserController#show']);
Route::get('/rate/user/{id}', ['as' => 'rate', 'uses' => 'RateController#showUserRate']);
Route::post('/rate/rate-user/{id}', 'RateController#rateUser');
Route::get('/invite-user-rate/{id}', 'RateController#showInviteUserToRateYou');
Route::post('/invite-rate/user/{id}', 'RateController#inviteUserToRateYou');
Route::get('/company/{id}', ['as' => 'company', 'uses' => 'CompanyController#show']);
Route::get('/rate/company/{id}', 'RateController#showCompanyRate');
Route::post('/rate/rate-company/{id}', 'RateController#rateCompany');
Route::get('/search/{page}/results/', 'SearchController#showSearchCompanies');
Route::get('/search/{page}/people/results', 'SearchController#showSearchPeople');
Route::get('/leave-a-rating/', 'SearchController#showLeaveARating');
Route::get('/invite', ['as' => 'invite', 'uses' => 'OtherAuthentificatedController#showInvite']);
Route::post('/email-invite', 'OtherAuthentificatedController#emailInvite');
Route::get('/contact', 'OtherController#showContact');
Route::post('/send-contact-email', 'OtherController#sendContact');
Route::get('/tyfcu', 'OtherController#thankYouForContactingUs');
you can run this command on your server for clear chache
if you want
php artisan cache:clear
and for redirecting you can redirect by this method
return Redirect::route('/my-profile');
think this might be help you

Laravel 5 : php artisan route:list Illuminate\Container\BindingResolutionException]

When I use the command "php artisan route:list" I have this error
[Illuminate\Container\BindingResolutionException]
Unresolvable dependency resolving [Parameter #0 [ <required> $methods ]] in class Illuminate\Routing\Route
The command works nicely when I have
Route::get('/', 'WelcomeController#index');
Route::get('home', 'HomeController#index');
but as soon as i add other routes, I have this exception
I found the problem :
I'm doing IOC in the controller and I inject Illuminate\Routing\Route. As soon as i delete the injection its works.
I have BaseController which extends Controller.
The BaseController is
class BaseController extends Controller {
/**
* #var array Options used by the pages
*/
protected $options;
/**
* #param Route $route
*/
public function __construct(Route $route)
{
$this->options = Option::getAutoloaded();
// Load the options for the named route page
$this->options = array_merge($this->options, Option::getByPage($route->getAction()['as']));
$this->initListPlugins();
}
A controller is
class MediasController extends BaseController {
public function __construct(Route $route)
{
parent::__construct($route);
$this->options = array_merge($this->options, Option::getByPage('media'));
}
Now thre problem is how I fix it :)
Thanx for your help
When ever I see this error I think of typo's, so make sure you recheck everything for those.
Just make sure your Route is linked to the correct Controller#function:
Route::get('medias', ['uses' => 'MediasController#getMediaList', 'as' => 'medias.index'])
Would go to the MediasController in Http/Controllers:
class MediasController extends Controller {
public function getMediaList()
{
return view('medialist');
}
}
Also make sure your controller has a correct return to the view.
And if you're using the
{!! link_to_route('medias.index', 'Media list') !!}
make sure you have the "illuminate/html": "5.0.*#dev" installed through your composer.json aswell.

Resources