Facing problem while trying to insert data in database, LARAVEL - laravel

I am getting this error "Class 'App\Models\Student' not found".
Please check my codes:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;
class StudentController extends Controller
{
public function data(){
$stud = new Student;
$stud->name = 'Mona Lisa';
$stud->rollnumber = '001';
$stud->save();
}
}

Your error is most likely in the namespace declaration of the Student model.
check in your class file App\Models\Student.php that the namespace is correct.
namespace App\Models;
then try to run in the console:
composer dump-autoload

Change this line:
use App\Models\Student;
to:
use App\Student;

Your error suggests that class is not loaded into the controller. I am guessing from the issue that you have created a separate directory for models if you have then you need to update your composer file to include new class paths for model classes. locate autoload in you composer.json file and add an entery in classmap array as shown below
"autoload": {
"classmap": [
"database",
"app/models"
]
}
then on command line navigate to your project directory and run the following command:
composer dump-autoload
and if you didn't create new models directory then just run
composer dump-autoload

Make sure your namespace of student model
use App\Models\Student If your model is in App\Models directory
use App\Student if your model is in app directory
Finally run composer dump-autoload

check your namespace of student model
use App\Models\Student If your model is in use App\Models directory
use App\Student if your model is in app directory
then go to command line and run composer dump-autoload

Related

Service Provider not booting in Laravel 5.8

I've created a new service provider to observe a model (App\Providers\EloquentEventServiceProvider.php), like so:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Staff;
use App\Oberservers\StaffObserver;
class EloquentEventServiceProvider extends ServiceProvider
{
public function boot()
{
Staff::observe(StaffObserver::class);
}
}
I've also added it to the config file (config\app.php):
return [
...
'providers' => [
...
App\Providers\EloquentEventServiceProvider::class,
...
]
...
]
The observer methods aren't working though. If I move Staff::observe(StaffObserver::class); to the AppServiceProvider class, it works just fine. So clearly this is an issue with getting my service provider to boot. I've tried php artisan config:clear, php artisan clear-compiled, composer update and composer dump but none have work. Any help is greatly aprpeciated.
your Oberservers name is wrong, as mentioned in the laravel doc observers laravel doc it should be Observers which means all of your observers should be within App\Observers instead of App\Oberservers.
so here we have 2 solutions :
1- if you want to keep the namespace App\Oberservers, you should run these 2 commands below because autoloading of the files may not work properly because we created a new folder Oberservers:
# Autoloading of files
composer dump
# Configure the cache
php artisan config:cache
2- the second solution is to just rename your actual Oberservers folder to Observers, in that way the autoloading of files will work well.

Laravel 5.5 not found class with correct namespace

My ./app folder looks like:
+-- app
+-- Classes
+-- Events
+-- EventBase.php
+-- EventX.php
There's nothing secret with EventX file:
<?
namespace App\Classes\Events;
class EventX {
// ...
}
EventBase.php represents a Facade that inside it I just try to instantiate an EventX:
public function someMethod() {
new \App\Classes\Events\EventX;
// ...
}
After this line, Framework throw an exception telling that class was not found:
Symfony\Component\Debug\Exception\FatalThrowableError (E_ERROR)
Class 'App\Classes\Events\EventX' not found
Even that:
file_exists(__DIR__ . '\EventX.php'); // true
I already had this issue before when trying to create Facades and solved by moving class file from his current directory and after moving back (yeah, I don't why but it worked).
Something tells me that this is an issue of autoload process, so I tried these command (but still not working):
php artisan cache:clear
php artisan clear-compiled
php artisan config:clear
composer dump-autoload
What can I do in order to investigate the problem?
I think the problem is with the php tag <?
<?php
namespace App\Classes\Events;
class EventX {
// ...
}
PHP also allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).
Link
You need to add the new namespace to your composer.json to the key "psr-4"
"psr-4": {
"App\\": "app/",
"Classes\\": "app/classes/"
otherwise composer can't detect your new namespace
Another approach can be found here:
https://stackoverflow.com/a/28360186/6111545

Undefined class OneSignal

I use Laravel 5.2 and want to use berkayk/laravel-onesignal package, and I installed this package step by step according to Guide on github.
But when I want to use this package I get "Undefined class OneSignal", also I run this artisan command in the terminal:
php artisan config:clear
php artisan config:cache
I try this code in controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use OneSignal;
use App\Http\Requests;
class SignalController extends Controller
{
public function index()
{
OneSignal::sendNotificationToAll("Some Message");
}
}
I get this error
You should use full namespace for the facade:
\OneSignal::sendNotificationToAll("Some Message");
Or add this to the top of your class:
use OneSignal;
You should write use OneSignal top of the class underneath the namespace.
Hope this work for you!

How to specify a path in routing Laravel?

I have located contoller in directory dashboard, how to specify correct path in routing to this controller?
I have class by path:
Class App\Http\Controllers\Dashboard\PlaceController
But I get error that this class : does not exist
Laravel uses PSR-4 namespaces, so you need to make sure controller is in correct namespace:
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
class PlaceController extends....
If namespace is correct, try to run composer dumpauto command.
Here are a example :
Laravel 5.2 :
create a directory on >> App >> Http >> Controllers >> Dashboard
create a file >>App>>Http>>Controllers>>Dashboard>> PlaceController.php
PlaceController.php
<?php
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
class PlaceController extends Controller
{
// write your functions
}
command line
on command line : composer dump-autoload

Laravel 4.2 , Model (eloquent) my own class not found

I have this weird error ... I apparently cannot use any of my model class in my project..
Ad_category model
class Ad_category extends Eloquent {
protected $table = 'ad_category';
protected $fillable = array('*');
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
}
calling this
$ad_cat=Ad_category::find(1);
error
`Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Class 'Ad_category' not found `
PHP frameworks use a system called "autoloading" to automatically include or require in the correct class definition file when you want to use a class. Autoloading in Laravel 4.2 is in a bit of a transitional spot, which means there's multiple answers to your question.
By default, Laravel 4.2 will look for a class named Ad_category in one of the following four locations.
app/commands/Ad/category.php
app/controllers/Ad/category.php
app/models/Ad/category.php
app/database/seeds/Ad/category.php
That is, Laravel's autoloader will automatically convert Ad_category into the file path Ad/category.php, and then check each configured autoload path for that file. You can configure the base autoloader paths in
#File: app/start/global.php
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));
Laravel 4.2 also uses composer based autoloading. Specifically, is uses a very aggressive form of composer autoloading called classmap autoloading. If you look in your composer.json file, you'll see a section like this
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
When you manually run the command
$ composer dumpautoload
Composer will go through every folder in the above section and look for PHP class files. If it finds one, it adds it to the classmap in vendor/composer/autoload_classes.php. Composer also runs this command automatically during updates.
So, what this means is, if you've defined Ad_category in a different location than Laravel expects to find it, you may be able to get away with running
$ composer dumpautoload
and Laravel will use Composer's autoloader to find your class.

Resources