Class with model could not be found in Lumen - laravel

In my web browser I am getting this error:
FatalErrorException in UserController.php line 18:
Class 'App\Report'
not found
It seems that UserControler.php page cannot find report.php page.
Here is my folder structure:
lumen
-api
--report.php // file where model Report is
---Http
----Controller
------UserController.php // file where i get error
-bootstrap
- ...
Header of UserController.php
namespace App\Http\Controllers;
use Illuminate\Database\Schema\Blueprint;
use App\Report; // it seems here is problem
use App\User; // with user model is same problem
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
class UserController extends Controller {
...
Header of report.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Report extends Model {
...
I am using Apache, Ubuntu, PHP 5.5 with Lumen framework

First of all model file name should be same as your class name here
lumen
-api
--report.php // This should be Report.php
---Http
----Controller
------UserController.php // file where i get error
-bootstrap
- ...
Also your Report.php Model will be under app directory same directory where your User Model resides

Related

Laravel 8 subfolder Controller

I created folder in Controller. But Laravel is giving
Target class [App\Http\Controllers\SAIDE\SaideMerchantController] does not exist.
error
-->Controller
<?php
namespace App\Http\Controllers\SAIDE;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
class SaideMerchantController extends Controller
{
public function home()
{
return view('backoffice.saide.maindashboard');
}
Route::get('/maindashboard','SAIDE\SaideMerchantController#home');
}
-->end controller
How to use controller in Folder ?
We don't see a complete route file, but try the change the get method to
Rouge::get('/mainboard', [App\Http\Controllers\SAID\SaidMerchantController::class, 'home']);
Check out Laravel documentation for version 8 routing
https://laravel.com/docs/8.x/routing#basic-routing

error vendor\laravel\framework\src\Illuminate\Container\Container.php:833 in laravel

please help me, I'm learning and using Laravel 8.x . I don't know what is my error in this case.
...
vendor\laravel\framework\src\Illuminate\Container\Container.php:833
...
this is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function index(){
echo'admin';
}
}
and this is my web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/admin','AdminController#index');
Since Laravel 8 default namespace App\Http\Controllers is not registered by default in RouteServiceProvider
So you need to use FQCN for the controller in the routes file other wise you will get the container binding resolution exception as you are getting
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AdminController;
Route::get('/admin', [AdminController::class, 'index']);
This happens from Laravel 8, since the App\Http\Controllers namespace isn't registered by default. You can register it in the corresponding service provider:
# app/Providers/RouteserviceProvider.php
// Uncomment this line
protected $namespace ='App\\Http\\Controllers';

Location of Controller in Laravel

I created a new Laravel controller named "StoriesController" using the CLI:
php artisan make:controller StoriesController
When I open app/Http/Controllers/StoriesController.php, I see:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class StoriesController extends Controller
{
//
}
My question is about the line:
class StoriesController extends Controller
Where is the "Controller" script located? Which directory?
{your-project]/app/http/controllers/{StoriesController}

Laravel : Class controller does not exist

I have created a simple controller and define a function. But when i run this it returns an error that controller does not exist.
In my web.php assign a route.
<?php
Route::get('/', function () { return view('front.welcome'); });
Route::get('plan','PlanController#PlanActivity')->name('plan');
On otherside in my controller my code:
<?php
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller as BaseController;
use Illuminate\Http\Request;
class PlanController extends Controller {
public function PlanActivity()
{
dd("hello");
//return view('admin.index');
}
}
This controller created on App\Http\Controllers\Front - on front folder
Error :
ReflectionException (-1)
Class App\Http\Controllers\PlanController does not exist
Add Front part to:
Route::get('plan', 'Front\PlanController#PlanActivity')->name('plan');
Also, change the top of the controller to:
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
And run composer du.
From the docs:
By default, the RouteServiceProvider includes your route files within a namespace group, allowing you to register controller routes without specifying the full App\Http\Controllers namespace prefix. So, you only need to specify the portion of the namespace that comes after the base App\Http\Controllers namespace.
First when defining route, make sure to use the correct path for the controller. the correct is:
Route::get('plan','Front/PlanController#PlanActivity')->name('plan');
Second you have imported Controller Class as BaseController. so you should extends BaseController not Controller:
class PlanController extends BaseController {
public function PlanActivity()
{
dd("hello");
//return view('admin.index');
}
}

Class 'App\' not found on laravel 5.2

I want to add data to the database after a successful validation,but i get this error.'
FatalThrowableError in AboutController.php line 51:
Class 'App\About' not found.
My Controller
<?php
namespace App\Http\Controllers;
use App\About;
use Illuminate\Http\Request;
use App\Http\Requests;
class AboutController extends Controller
{
public function store(Request $request)
{
//
$about = $request->about;
$validation = \Validator::make($about, About::$rules);
if($validation->passes())
{
About::create($about);
return route('about/admin')->compact(about);
}
}
my Model
<?php
namespace App\Http\Controllers;
use Illuminate\Database\Eloquent\Model;
class About extends Model
{
//
protected $guarded = array('id');
protected $fillable = array('about');
public static $rules = array('about' => 'required|5');
}
location of controllers and Model:
App\Http\Controllers\AboutController
App\About
I have tried to run
php artisan cache:clear
php artisan clear-compiled
composer dump-autoload
I'm stuck can anyone tell me what is causing this?
As #webNeat said you should change the namespace that you are using in your Model.
Your Model About
<?php
namespace App\Http\Controllers;
use Illuminate\Database\Eloquent\Model;
class About extends Model
{
Controller
<?php
namespace App\Http\Controllers;
use App\About; // You have declared App\Http\Controllers in your Model
Model About Fixed
<?php
namespace App; // change to this namespace
use Illuminate\Database\Eloquent\Model;
class About extends Model
{
If you're a bit lost with Laravel or namespaces I strongly recommend you to use php artisan with each of its commands, and see and study what they do by reading all the code generated. For this case with:
php artisan make:model About
You will get a fresh new About model prepared for receive all your code with the correct namespace.
changing the namespace of your model to App should fix the issue.
<?php
namespace App; // <- here
use Illuminate\Database\Eloquent\Model;
class About extends Model
{

Resources