Resolve Laravel library and model name crash - laravel

I have a library and a model with the same class name. How do I instantiate the model in my library class?
/application/libraries/property/request.php
<?php namespace Property;
class Request {
...
/application/models/request.php
<?php
class Request extends Eloquent {
...
In need to instantiate the model in Request library. new \Models\Request() will result in "Class 'Models\Request' not found".
Is there anyway to resolve this issue without changing class name?

Did you try new Request(), or new \Request()? I'm not sure but your Request model isn't defined in the Models namespace, so that could be it …

You need to namespace your Request class so other files know where in the namespace it is, otherwise there's no way to know if it's in \, \Models, \Property, or whatnot.
You should really have this:
/application/libraries/property/request.php
<?php namespace App\Libraries\Property;
class Request {
...
/application/models/request.php
<?php namespace App\Models;
class Request extends Eloquent {
...
Then in your request library: use App\Models\Request as RequestModel; (not sure if you can keep it the same name).

Related

Error to Retrieve data from database in Laravel

I have a database name lrvesikhon and there is a table named 'person'. I created a model named 'Person' and also create a controller named 'PersonsController'. I also config the .env file. But some error occurs. The error:
My code -
PersonController -
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PersonsCotroller extends Controller
{
public function index(){
$person_list = Person::all();
dd($person_list);
}
}
Web -
<?php
Route::get('/', 'PersonsCotroller#index');
Person Model -
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
protected $table = "person";
}
Write bellow code after use App\Http\Request;
use App\Models\Person;
If your model Person is not in Models directory change to use App\your-directory\Person;
Your haven't hooked up the model class.
Add in your controller after namespace App\Http\Controllers
use App\Models\Person;

Laravel model existed but return Class 'App\Entities\OrignalTripModel' not found

Model included in my Controller as
use App\Entities\OrignalTripModel;
But it returns
Class 'App\Entities\OrignalTripModel' not found
i am using it into controller as
OrignalTripModel::create($inputs);
Kindly include it in your controller like this:
use App\Entities\OrignalTripModel;
And please check namespace for the model OriginalTripModel to see if it has namespace App\Entities; and that is extends model like this class OriginalTripModel extends Model

Dependency resolving with App::make not working when bind is done by a provider in Laravel

I'm using Laravel 5.2. I tried to resolve a dependency in Laravel out of the IOCContainer as follows.(with App::make method)
App/FooController.php:-
<?php
namespace App\Http\Controllers;
use App\Bind\FooInterface;
use Illuminate\Support\Facades\App;
class FooController extends Controller
{
public function outOfContainer(){
dd(App::make('\App\bind\FooInterface')); // Focus: program dies here!!
}
}
Bindings for the FooInterface done in the AppServiceProvider as follows
App/Providers/AppServiceProvider.php:-
<?php
namespace App\Providers;
use App\Bind\Foo;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('\App\Bind\FooInterface', function() {
return new Foo();
});
}
}
Structure of the Foo class as follows.
App/Bind/Foo.php:-
<?php
namespace App\Bind;
class Foo implements FooInterface {
}
Structure of the FooInterface interface as follows:-
<?php
namespace App\Bind;
interface FooInterface {
}
Then I created a route as follows.
Route::get('/outofcontainer', 'FooController#outOfContainer');
But when I navigate to this route it throws an exception with the error text:
BindingResolutionException in Container.php line 748:
Target [App\bind\FooInterface] is not instantiable.
What is going wrong with this?
How to use App:make() with the AppServiceProvider?
In your service provider, you're binding the string '\App\Bind\FooInterface'. In your controller, you're attempting to make the string '\App\bind\FooInterface'. These strings are not the same, as they have differing cases (Bind vs bind). Since the strings are not the same, Laravel cannot find the binding in the container.
Correct the casing in your make statement and it should work:
dd(App::make('\App\Bind\FooInterface'));

Method [validate] does not exist

Method validate does not exit. I tried to validate my form values as shown in the figure. But it give me this error. Please tell me . I am new to laravel.
[
You aren't extending the correct Controller.
class UserController extends Controller {
...
}
Controller then extends BaseController.
If you open up Controller which resides in the same namespace as your other controllers, you will see it uses the trait ValidatesRequests which is what provides the validate method.
You can also remove the line use Illuminate\Routing\Controller as BaseController;. There should be no reason to import that.

Laravel5 extends in Controller in Subfolder

I have folder structure app/Http/Controllers/School inside which I have a SchoolController:
namespace School;
class SchoolController extends BaseSchoolController {.....
and BaseSchoolController:
namespace School
class BaseSchoolController extends \BaseController {....
(BaseController is in parent Controllers folder)
class BaseController extends Controller {...
This gives an error:
FatalErrorException in SchoolController.php line 5: Class 'School\BaseSchoolController' not found
Any ideas, thanks?
Seems happy with this structure though:
class SchoolController extends \Controller {
From what I can see right away, the namespaces are wrong.
Laravel 5 uses PSR-4, which means that each namespace must match the folder structure (including the vendor) of a class file.
So, for app/Http/Controllers/School/SchoolController.php, the namespace should be set to:
<?php namespace App\Http\Controllers\School;
not just
<?php namespace School;
Also, if the app name isn't the default one (App), change it accordingly on the namespace.
As an example, if you ran:
php artisan app:name ACME
the namespace should then be:
<?php namespace ACME\Http\Controllers\School;
Check other classes like app/Http/Controllers/Auth/AuthController.php, to have an idea how it should be done.

Resources