Can Someone please help me on laravel postcontroller? I am using Laravel 8 but I am seeing below error though my Postcontroller is existing on app>Http>Controllers>PostController.php. Below is my code and I appreciate yoenter image description hereur help in advance.
Routes
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostsController;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route:: get('/p/create','PostsController#create');
Route::get('/profiles/{users}',[App\Http\Controllers\HomeController::class, 'index'])->name('home');
PostController
use Illuminate\Http\Request;
class PostsController extends Controller {
public function create() {
return view ('posts/create');
}
}
Insead of :
Route:: get('/p/create','PostsController#create');
use :
Route:: get('/p/create',[PostsController::class , 'create']);
You can read more about laravel 8 routes here:
https://laravel.com/docs/8.x/routing#the-default-route-files
Related
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());
The routing process has changed in Laravel version 8. I did as in the internet but it gives an error. Where am I doing wrong?
Route file
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Backend\BackendHomeController;
Route::get('/', function () {
return view('welcome');
});
Route::get('/admin', [BackendHomeController::class, 'index'])->name("index");
Controller file
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public static function index()
{
return view("backend.index");
}
}
BackendHomeController also like this. Everything seems to be correct, but am I doing something wrong with using?
Error says
target class does not exist error
Class is referenced as BackendHomeController but your file is named HomeController. These should align for autoloading to be working.
I was trying to make a group routing with a prefix in Laravel 8. But when I tested it in http://localhost/mysite/admin/test/, it always throws error 404.
Here is the code in web.php:
Route::prefix('/admin', function() {
Route::get('/test', [Admin\LoginController::class, 'index']);
});
I created a controller in app/Http/Controller/Admin/ as the controller is inside Admin folder.
Here is the code in LoginController:
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class LoginController extends Controller
{
public function __construct()
{
//
}
public function index()
{
echo "Please login";
}
}
Can anybody show me what I am doing wrong to get it working?
You have to group the routes as stated in the documentation like:
Route::prefix('admin')->group(function () {
Route::get('/test', function () {
// Matches The "/admin/users" URL
});
});
In your case it would be:
use App\Http\Controllers\Admin\LoginController;
Route::prefix('admin')->group(function () {
Route::get('/test', [LoginController::class, 'index']);
});
I think it should be 'admin' not '/admin'.
That slash makes it:
http://localhost/mysite//admin/test
=>
http://localhost/admin/test
You can check all your routes using: php artisan route:list
Try this
use App\Http\Controllers\Admin\LoginController;
Route::prefix('admin')->group(function () {
Route::get('test', ['LoginController::class, index'])->name('test'); });
The error I am getting is: Target class [PostController] does not exist but it does.
Route web.php
Route::get('/post', 'PostController#index');
Route::post('/post', 'PostController#store');
Route::get('/', function () {
return view('create');
});
PostController.php
namespace App\Http\Controllers;
use App\Post;
use Redirect,Response;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
return view('create');
}
public function store(Request $request)
{
$data = json_encode($request);
Post::create($data);
return back()->withSuccess('Data successfully store in json format');
}
}
This error comes in Laravel new version because there is no namespace prefix being applied to your route groups that your routes are loaded into. In the old version of Laravel, the RouteServiceProvider contained a $namespace property which would automatically be prefixed onto the controller route.
To solve this, you either can go to RouteServiceProvider and uncomment the line:
protected $namespace = 'App\\Http\\Controllers';
Or you can use closure-based syntax:
use App\Http\Controllers\PageController;
Route::get('/page', [PageController::class, 'index']);
Another way would be to use the fully qualified class names for your Controllers:
Route::get('/page', 'App\Http\Controllers\PageController#index');
use this line on the top of the (web.php) maybe your problem will resolve
use App\Http\Controllers\PostController;
just trying to get to grips with the basics of Laravel. I was getting syntax errors in my areasController file. Once they were resolved I started recieving this error: ReflectionException in Route.php line 280:
Class App\Http\Controllers\areasController does not exist. So it seems like Laravel can find the file to know that when there are errors in it, but not the rest of the time. Any help appreciated, this is my first framework so I'm pretty stumped.
routes.php:
Route::get('/', function () {
return view('welcome');
});
Route::get('locations', function() {
return view('locations');
});
Route::get('areas', ' areasController#areas');
areasController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use DB;
class areas extends Controller
{
//
public function areas() {
$areas = DB::table('areas')->all();
return view('areas');
}
}
Any help would be appriciated.
In your routes.php file, you ask to use the method areas from areasController but in your controller file, you define class areas extends Controller
It should be class areasController extends Controller then it should work