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

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.

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.

Facing problem while trying to insert data in database, 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

How to create custom helper file in laravel 5.6?

When I create an autoload helper file with composer following error ocures:
Fatal error: composerRequireebefe31fc60fbe2897fba8c156ec310c(): Failed
opening required
'D:\xampp\htdocs\common_module\vendor\composer/../../app/Http/helpers.php'
(include_path='D:\xampp\php\PEAR') in
D:\xampp\htdocs\common_module\vendor\composer\autoload_real.php on
line 66
Following are steps to create a Custom Helper In Laravel 5.5
Step : 1 Create app/helpers.php file
First, create one helper class file in app/helpers.php path and in this file we are write our any custom helper logic into the function.
Step : 2 Add app/helpers.php file in composer.json file
Now, we are add our app/helpers.php file in composer.json file for a autoload section.
"autoload": {
"classmap": [
...
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/helpers.php" //Add This Line
]
},
After done this then once we are run following command.
composer dump-autoload
for more visit following link
Visit How To Create Custom Helper In Laravel 5.5
I hope this will help you...
In older version of Laravel I do the following:
app folder create a Helpers folder
Create a class YourHelper class file
in that class file set namespace to App\Helpers
Name the class file
in config/app.php in aliases create an alias (so you can call the helper in your view also) 'YourHelper'=>'App\Helpers\YourHelper::class'
If you are new to Laravel or PHP, let’s walk through how you might go about creating your own helper functions that automatically get loaded by Laravel.
Creating a Helpers file in a Laravel App
you can organize the location of your helper file(s) however you want, however, here are a few suggested locations:
app/helpers.php
app/Http/helpers.php
I prefer to keep mine in app/helpers.php in the root of the application namespace.
Autoloading
PHP functions cannot be autoloaded. However, we have a much better solution through Composer than using require or require_once.
So composer has a files key (which is an array of file paths) that you can define inside of autoload:
"autoload": {
"files": [
"app/helpers.php"
],
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
Once you add a new path to the files array, you need to dump the autoloader:
composer dump-autoload
(Use this command after changing composer.json.)
Now on every request the helpers.php file will be loaded automatically because Laravel requires Composer’s autoloader in public/index.php:
require __DIR__.'/../vendor/autoload.php';
Defining Functions
Defining functions in your helpers class is the easy part, although, there are a few caveats. All of the Laravel helper files are wrapped in a check to avoid function definition collisions:
if (! function_exists('env')) {
function env($key, $default = null) {
// ...
}
}
I prefer to use function_exists checks in my application helpers, but if you are defining helpers within the context of your application, you could forgo the function_exists check.

How to use external php classes in laravel framework?

I'm switching from Codeigniter to Laravel. In CI its easy to user external libraries or classes, just put it in application/libraries folder then load it from your controller by:
$this->load->library('libraryName')
Now, I wanna do the same thing in Laravel. I used this scenario :
Create a folder inside my app folder called libraries.
Change the composer.json (which located in the root) as the following:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/libraries" // this was added
]
},
And from CMD run the command composer dump-autoload.
My class look like this:
class Messages{
function errorMessage(){//....}
function successMessage(){//....}
}
Class stored in file called Messages.php which located in app\libraries folder.
I still have the same error:
Class 'Messages' not found
What is the problem and what is the solution?
Create a new folder: app/your-folder-name and put your class in that folder
Open the globals.php file in app/start
You should see this:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
));
Add your folder with your class to this array
app_path().'/your-folder-name'
More info can be found here: Where do I put Laravel 4 helper functions that can display flash messages?
check your file with autoloading classes /vendor/composer/autoload_classmap.php
your class should in there.
if it is - check do you actually using composer autoloading on your request?

Laravel 4: loading an old library: how?

I have an old library (phpquery) that I'd like to include in my project. I've put it inside vendor, but it doesn't work, as it's not PSR-0 compliant. I don't want it to load for every request, so I didn't put the require inside bootstrap autoload.php.
I don't even know, how I can get the root of the app. Running path() gives me a URL, not what I'm after.
So how can I do that?
You can create a libraries directory just like in laravel 3 and include it in your class loader. You can do this via composer or laravel.
//composer.json
"autoload": {
"classmap": [
"app/commands",
"app/libraries",
"app/database/migrations",
"app/tests/TestCase.php",
]
},
//app/starts/global.php
ClassLoader::register(new ClassLoader(array(
app_path().'/libraries',
)));
Autoloading via Laravel does not require you to run "composer dumpautoload" every time a class is created or removed.
UPDATE - L4 Beta 4
ClassLoader::addDirectories(array(
app_path().'/libraries',
));

Resources