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

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

Related

Laravel 5.5 connect your class in the controller

There is a file "MyClasses.php" in the folder "App":
namespace App;
use Illuminate\Database\Eloquent\Model;
class Model1 extends Model {}
class Model2 extends Model {}
How to connect it in the controller using use?
I suggest you to read about PSR-4 standards:
https://www.php-fig.org/psr/psr-4/
MyClasses.php is not a valid name for a model in this case, because a) none of the classes defined inside of it are called MyClasses and b) there are numerous class definitions in this file.
// App/Model1.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Model1 extends Model {
protected $table = 'some_table';
}
// Controller
use App\Http\Controllers\Controller;
use App\Model1;
class SomeController extends Controller
{
public function index()
{
$record = Model1::where('some_field', 1)->get();
}
}
EDIT: to clarify.
Both models should be in their own files, called Model1.php and Model2.php under the App folder. Also, you model names * should * correspond to the table names they are accessing. So if for example Model1 is going to be bound to table user_confirmations you should rename your file and class to UserConfirmations - this would be considered best practice.

Laravel: pluck method on Model instance

I am just learning about Laravel, so excuse me for any newbie questions. In a tutorial I have seen people using pluck directly on Model classes, such as App\MyModel::pluck('id').
My question is where the class gets this method from. It extends the Model class but neither there nor in any trait is it defined. I can only see it defined for collections. I have looked at the source intensely but cannot see it.
Thanks
Zibellon
All of Laravel models they do extend a base class named Model. Example when you define a model the class will look like
class Channel extends Model
{
//stuff
}
Class channel extends base class Model, if you go to Model base class you will find it under Illuminate\Database\Eloquent\Model
That is the abstract base Model class which through some magic methods it recalls the method defined on QueryBuilder under 'Illuminate\Database\Query\Builder' there method pluck is defined

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.

extends Model == extends Eloquent?

All the examples of Eloquent Models in Laravel 4 extends Eloquent, but when you generate a Model in Laravel 5 it says extends Model, are they the same?
Laravel 4
<?php
class User extends Eloquent {
//Code
}
Laravel 5
<?php
class User extends Model {
//Code
}
The Laravel 5 docs says:
Defining An Eloquent Model
class User extends Model {}
Yes they are the same. Laravel 4 uses Class Aliasing to map Illuminate\Database\Eloquent\Model to Eloquent. You can see in the app/config/app.php file:
'Eloquent' => 'Illuminate\Database\Eloquent\Model',
Laravel 5 uses namespacing instead. So at the top of the model class you will see this line:
use Illuminate\Database\Eloquent\Model;
used...
use Illuminate\Database\Eloquent\Model;
to extend the model
I am using the _ide_helper.php file
from barryvdh's laravel-ide-helper
The subclass there is also called Eloquent and extends to Model.
So if I extend my own model class to Eloquent, the IDE knows all the functions, like MyModelClass::find. Maybe there's another way to do it, but that really works for me.

Resolve Laravel library and model name crash

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).

Resources