Class autoloading in Laravel 4.1 - laravel

I am trying to use Laravel and have been following the official Laravel Eloquent documentation and multiple tutorials at credible sources tuts plus
I created a model inside app/models called Stack with a table in the database called stacks with a primary key column called id, as corresponding to Laravels defaults.
<?php (Stack.php)
class Stack extends Eloquent
{
}
$stacks = Stack::all();
However when I run this model I get the following error message.
Fatal error: Class 'Eloquent' not found in C:\www\laravelproject\app\models\Stack.php on line 4
Including the official documentation and the reputable tutorials, I have also watched 2 youtube tutorials and it seems like there is no additional autoloading/including/requiring required to be declared in any new defined model's, so I am assuming something else here maybe wrong.
Do I have to manually find all classes I must autoload? If so, why is this not written in the official documentation?
I downloaded the latest laravel.phar file directly from laravel and used a .bat file to call it. (Not via composer)
Some things I have checked/tried to fix the problem.
Eloquent directory does exist at vendor\laravel\framework\src\Illuminate\Database\Eloquent
Eloquent alias set in app/config/app.php. Default 'Eloquent' => 'Illuminate\Database\Eloquent\Model'
Directly extending class like \Illuminate\Database\Eloquent\Model, error message the same but with \Illuminate\Database\Eloquent\Model instead of just Eloquent
Tried to directly extend through all variations by navigating down the entire Laravel directory structure \vendor\laravel\framework\src\Illuminate\Database\Eloquent, then \laravel\framework\src\Illuminate\Database\Eloquent etc... etc...
Bit the bullet and decided to try the second official method, I installed composer and ran the command composer create-project laravel/laravel --prefer-dist, the command screen alerted me it was downloading files which was then all successful at 100%, then alerted me that a generated application key was set successfully. I then navigate to the new directory model/User.php and receive the exact same error message as when I did it with the previuos method(laravel.phar direct download).
Thanks in advance.

Make sure you are accessing the application from the correct 'entrance'.
Thus, accessing it from app/public/index.php.
The app/public/index.php file loads the autoloader.
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylorotwell#gmail.com>
*/
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require __DIR__.'/../bootstrap/autoload.php';

It's possible your is namespacing. Try adding the backslash before the class being extended.
class Stack extends \Eloquent
{
}

Make sure your are setting the Eloquent alias in the app config. (app/config/app.php)
Alternatively use the class directly. I believe it's: Illuminate\Database\Eloquent\Model
class Stack extends \Illuminate\Database\Eloquent\Model {}

Related

Laravel create Model and Migration dynamically with dynamic table columns

Working on a task where we need to create Model and Migration files programmatically. Also in the Model there will be some predefined functions. These functions will be same for all models. The functions are just relations with other Models. I have searched for a few options and found that Laravel has Stubs and we can create custom stubs which are great but don't think there is any option to pass params to the stub file when generating it.
In this case, we want to create a model and migration with dynamic columns. May be there is no easy way to do it but in case anyone has done it already, can you please provide me a hint of how you did it.
Trying this laravel package
https://github.com/laravel-shift/blueprint
.It can generate models, migrations, controllers from Yaml file. May be we can create a yaml file dynamically and then publish it.
Thanks
you can write your own command.
for example i wrote a command for generate repository pattern in my projects
php artisan make:repository repoName
You can publish stubs
php artisan stub:publish
Create your custom stub
<?php
// stubs/controller.custom.stub
namespace {{ namespace }};
use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;
/**
* Hello from the custom controller stub
*/
class {{ class }}
{
//
}
And call it
php artisan make:controller --type=custom MyController
Watch --type option

Laravel RouteServiceProvider.php file

I'm relatively new to Laravel; using 8.26.1
just after creating a project, I noticed that I have RouteServiceProvider.php exists in two locations:
app\providers\ and \vendor\laravel\framework\src\Illuminate\
can someone explain which one should I use to make my changes or guide me to a document where I can read about how and which file is used by Laravel ?
You would be using the one in your application which is everything that isn't in vendor as that is all packages and you should not touch any of that. The App\Providers\RouteServiceProvider (app/Providers/RouteServiceProvider.php) is the provider that is loaded by your configuration in config/app.php. This extends the base one from Laravel to give it the features it needs.

PhpStorm autocomplete with \Nwidart\Modules

The problem would be that I use PhpStorm IDE. The Laravel project is managed by nwidart/laravel-modules.
However, PhpStorm does not handle basic Laravel functions. For example: findorfail(), for own models:
Method 'findorfail' not found in \Modules\Companies\Models\Companies_adresses
I did the following to enable autocomplete:
install https://github.com/barryvdh/laravel-ide-helper#automatic-phpdoc-generation-for-laravel-facades
Enable PhpStorm plugins:
https://plugins.jetbrains.com/plugin/13441-laravel-idea
https://plugins.jetbrains.com/plugin/7532-laravel
It is very confusing because it does not give tips correctly. It does not list database columns, etc... PHPDoc still came to mind, but I don't know how to get started!
You have no ideas? I feel completely lost :(
I set up the Laravel IDE Helper but I had to add the following into all my models...or you could add to a new Model class that you then extend to all of your models. Either way, this will get PHPStorm to read the facade methods like that correctly:
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* Class Employee
*
* #mixin Builder
*/
class Employee extends Model
{
// blah blah blah
}
Make sure the PHPDoc bumps up against your model. The key bit is the #mixin line which gives PHP Storm a connection to all the facade methods for your model. You will still need the IDE Helper as well. But with this PHPStorm will suggest as you type and allow you to control-click to view the method itself from elsewhere in your project.
You also can generate advanced ide helper code with Laravel Idea plugin. main menu > Code > Laravel Idea code generation > Generate Eloquent Helper code.

I can't seem to access my Auth inside a vendor package

I had a Laravel app that I created, then I added another Laravel package for Oauth2 for a CRM.
This created a vendor and migration for DB. I tested it and it worked, I authenticated my CRM.
Now I would like to have that record be tied to the user_id in the Auth scaffold that I created with composer make:auth
However, when I try to get the current user from inside of those vendor files, it is not pulling in that information and is giving me errors.
I think this may be a namespace issue, but maybe it could be a guard or middleware. I am not sure. I am pretty laravel
I have tried a handful of other solutions, but I wasn't sure if those were tied to my specific issue
<?php
namespace Djaxho\LaravelInfusionsoftOauth2\Http\Controllers;
//namespace App\Http\Controllers\Auth;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Djaxho\LaravelInfusionsoftOauth2\Infusionsoft;
use Illuminate\Support\ServiceProvider;
//use Illuminate\Support\Facades\Auth;
class AuthorizeInfusionsoftApiController extends BaseController
'''
print Auth::user()->id;
I see that it is in its own namespace, is this what is the issue, laravel is not looking outside?
I get this error:
Class 'Djaxho\LaravelInfusionsoftOauth2\Http\Controllers\Auth' not
found
Their page says it pretty straightforward on what to do.
https://packagist.org/packages/djaxho/laravel-infusionsoft-oauth2
Installation (These instructions are for an 'alerady set-up' laravel
project with a functioning database set up) Add the service provider
for laravel by adding the following line to your 'providers' array in
the file congig/app.php
Djaxho\LaravelInfusionsoftOauth2\LaravelInfusionsoftOauth2ServiceProvider::class,
And add
use Djaxho\LaravelInfusionsoftOauth2\Infusionsoft;
to your model where you are calling the auth from.
That being said, I dont know if this package handles auth seperately or doesnt. In that case you should use the laravel facade,
use Illuminate\Support\Facades\Auth
You need to give you routes in your package web middleware and it will works fine
.

laravel id-helpeClass 'App\Models\Eloquent' not found

i'm just started to use laravel ide-helper in phpstorm and wanted to document my models.
I've run the command php artisan ide-helper:models but comes up with the error:
'App\Models\Eloquent' not found
After reading it looks as if the error lies in what my models extend - my model class looks like this:
<?php namespace App\Models;
use \Illuminate\Database\Eloquent\Model;
class Article extends Model { }
This is the standard set in the Laravel docs and created by the generators.
Is there a way to resolve this issue so I can generate the docs with the helper or do I leave as is.
Bit confused
Thanks

Resources