I installed a fresh Lumen in my docker container. Next I wanted to use eloquent so I activated it and it worked with some tests.
My problem now is if I want to get some Information about the model I've created, most of the times I get an error from the ClassLoader:
"include(/var/www/html/app/Models/Group.php): Failed to open stream: No such file or directory"
The file is obviously there, if I start the request again 1-2 / 10 times it worked. Finds the class and loads all things.
The Model itself has no special stuff in it, just an empty class.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Group extends Model
{
}
In my controller I'm using the model like this:
<?php
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Models\Group;
class TestController extends Controller
{
public function getGroups(Request $request): JsonResponse
{
return response()->json(['groups' => Group::all()]);
}
}
It looks like it has something to do with docker?
Hope someone can help.
Thanks in advance!
Related
I was writing my first laravel package, so I could use it and understand how packages work and learn how to write packages and etc.
But my project didn't recognize the package that I wrote.
Here is my package Github link: https://github.com/IIIphr/Shamsi_Calendar
And this is my main project: https://github.com/IIIphr/aimeos_shamsi
I use this command to add my package to the app: composer require iiiphr/shamsi_calendar
And it'll be added successfully (at least I guess). Then in the temp Controller, I wrote this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use iiiphr\shamsi_calendar;
class temp extends Controller
{
public function index(){
return shamsi_calendar::get_date();
}
}
And a route:
Route::get('/date','temp#index');
But in the http://localhost:8000/date, I will face this error:
Before, I have tried other ways and the result was anything but success.
Another thing, I have this error in visual studio code in the temp controller:
Undefined type 'iiiphr\shamsi_calendar'.intelephense(1009)
I will appreciate any kind of help :)
You import the library perfectly but you don't use the Calendar Controller that the package gives you. The package haves a controller called CalendarController, you have two ways, extends from this controller or create an instance of this controller, if you extend the controller from this:
<?php
namespace App\Http\Controllers;
use iiiphr\shamsi_calendar\CalendarController;
class temp extends CalendarController
{
public function index(){
return $this->get_date();
}
}
What do you think about it?
Hi there I am having trouble using a model within a class there error being shown is Error
Class 'App\Models\RegisteredUsers' not found.
I have made sure that the namespaces match what is being used but I repeatedly get the same error.
Model code
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class RegisteredUsers extends Model
{
//
}
Controller code
<?php
namespace App\Http\Controllers;
use App\Models\RegisteredUsers;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class RegisterUser extends Controller
{
$UserObj = new RegisteredUsers();
}
The directory
App/Http/Controllers/RegisterUser - controller
App/Http/Models/RegisteredUser - model
I created the model and the controller using the command line with PHP artisan.
I have tried the solutions from
laravel model class not found
as well as Model not found in Laravel
and a few laracast questions but i still get the error.
May need to rebuild the classmap
composer dump-autoload
With App\Models namespace in your RegsiteredUser model, the RegisteredUser.php model file must be in the app/Models/RegisteredUser.php directory. Try to move the Models folder outside the Http folder. And from now, you should never put the Models folder in the Http folder again.
Your error is App/Http/Models/RegisteredUser and use App\Models\RegisteredUsers;, did you place your Models in Http?. that is not correct, the Models should be in App root directory, so you are calling the RegisteredUser in the wrong place
I'm learning laravel but it isn't working out that well....
I've set my route in routes.php:
Route::get('/','WelcomeController#index');
Then I obviously have made a controller called "WelcomeController" and it looks like this:
<?php
class WelcomeController extends BaseController
{
public function index()
{
return view ('index');
}
}
?>
And then I've made a view called index with just some html text.
But when I go to localhost/public I receive the error:
FatalErrorException in WelcomeController.php line 3:
Class 'BaseController' not found
And when I say:
class WelcomeController extends Illuminate\Routing\Controller
It does not work!
What am I doing wrong.
You should try
use Illuminate\Routing\Controller as BaseController;
at the top of your controller file. That acts as an import
Two suggestions:
Run php composer dump-autoload to make sure the class mappings is fresh.
Add use Controller; in your use block. The modify your controller to extend it. Example:
class WelcomeController extends Controller {...
Controller is an interface in Laravel 4.*
In Laravel 5 use instead:
use App\Http\Controllers\Controller; according to the documentation here: http://laravel.com/docs/5.0/controllers
The Laravel documentation clearly describes how to change your routes if you nest your controllers in folders. It seems REALLY simple, and yet I'm still getting an error. Here's the error:
"Class App\Http\Controllers\Input\InputController does not exist"
^That path looks 100% correct to me. What gives?
File Structure:
-Controllers
--Auth
--Input
---InputController.php
Routes:
Route::get('input', 'Input\InputController#getInput');
InputController:
<?php namespace App\Http\Controllers;
use Illuminate\Http\Response;
class InputController extends Controller
{
public function getInput()
{
return response()->view('1_input.input_form');
}
}
Thanks for any help!
Change Controller namespace from
namespace App\Http\Controllers
to
namespace App\Http\Controllers\Input
namespace needs to be changed to the directory your controller is in 'App\Http\Input'
You need to pull in Controller with use App\Http\Controllers\Contoller so that you can extend it.
<?php
namespace App\Http\Controllers\Input;
use App\Http\Controllers\Controller; // need Controller to extend
use Illuminate\Http\Response;
class InputController extends Controller
{
public function getInput()
{
return response()->view('1_input.input_form');
}
}
you should try running a couple commands in your base dir from your terminal (shell/prompt):
composer dump-autoload
or if you don't have composer set as executable:
php composer dump-autoload
and then:
php artisan clear-compiled
This way your laravel would prepare everything again "from scratch" and should be able to find the missing controller class.
Basically laravel generates some additional files to boot up faster. If you define a new class it doesn't get included into that "compiled" file. This way your class should be "introduced" to the framework.
I'm in the process of switching from Laravel 4.2 to Laravel 5, not sure if that is relevant, but I am getting an error:
"Class 'library\observers\UserObserver' not found"
and I have no Idea what the problem is, as far as I can see ( through my frustration mind you ) is that everything is in its right place, name spaces, folders, class names etc.. and I've ran the artisan dump autoload command twice now. the class is an observer which modifies user input on save. here is my code:
UserObserverServiceProvider.php:
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use library\observers\UserObserver;
use App\Models\User;
class UserObserverServiceProvider extends ServiceProvider
{
public function boot()
{
User::observe( new UserObserver );
}
public function register(){}
}
UserObserver.php:
<?php namespace library\observers;
use library\Facades\Geo;
use Geocode;
use State;
use City;
class UserObserver{ code for user observer }
app.php configuration for service provider:
'App\providers\UserObserverServiceProvider',
All of these things were working together before the switch, what am I missing?
I left the App out of the namespace and path for use, works now, thanks!