extends Model == extends Eloquent? - laravel

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.

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

"User Model" functions in Laravel 5 don't work

I migrate from laravel 4 to laravel 5. I modified User Model to make it compatible to laravel 5 User Model. The User model in Laravel 4 is like that:
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
// some model relation functions..
...
public function getLocationId()
{
return $this->location_id;
}
}
I modified it and the new User model is like that now:
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model as Eloquent;
class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
// some model relation functions (some changes for namespace)
...
public function getLocationId()
{
return $this->location_id;
}
}
However, getLocationId method is now problematic. The following line results in an error.
$auth_location_id = Auth::user()->getLocationId();
This is the error:
BadMethodCallException in Builder.php line 2508:
Call to undefined method Illuminate\Database\Query\Builder::getLocationId()
How can I make the method compatible to laravel 5? Thanks..
Update:
$auth_location_id = Auth::user()->getLocationId; works but returns empty!!
The line below works properly.
$auth_user_id = Auth::user()->getAuthIdentifier();
By my understanding, and short experience with Laravel, all I do is just to use:
use Illuminate\Foundation\Auth\User as Authenticatable;
Then have my Model as:
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
public function getLocationId()
{
return $this->location_id;
}
}
So I may be safe to assume, at least because it works for me that all those contracts has be extended in the facade called when ....\User is used.
PS: this is just an illustration of how I currently use it in Laravel 5.2.*.
Hope this can help or lead you to the solution :)

Class App\User contains 6 abstract methods and must therefore be declared abstract

I saw this error for the first time and don't really know what to do about it.
When I tried to register a new user on my website and when I clicked on submit button it shows:
FatalErrorException in User.php line 11:
Class App\User contains 6 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Contracts\Auth\Authenticatable::getAuthIdentifierName, Illuminate\Contracts\Auth\Authenticatable::getAuthIdentifier, Illuminate\Contracts\Auth\Authenticatable::getAuthPassword, ...)
User Model:
<?php
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
protected $table = 'users';
protected $primaryKey = 'id';
}
What is it trying to say, I don't understand. Please can someone help me with this?
You are implementing Illuminate\Contracts\Auth\Authenticatable. This is interface and requires your User class to have some required methods:
public function getAuthIdentifierName();
public function getAuthIdentifier();
public function getAuthPassword();
public function getRememberToken();
public function setRememberToken($value);
public function getRememberTokenName();
If you are trying to make default User model, you should use Illuminate\Foundation\Auth\User as Authenticatable and extend it instead of Model class. There is no need to implement Authenticatable interfase.
You need to either extend Illuminate\Foundation\Auth\User instead of Illuminate\Database\Eloquent\Model, or use Illuminate\Auth\Authenticatable trait in your class
UPDATE
You need to extend Illuminate\Foundation\Auth\User like this
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
}
UPDATE 2
Also make sure you don't have native Laravel's App\User model in you app folder, named User.php

Laravel 4 Versionable Package error: Class 'MyApp\Models\Eloquent' not found

I'm trying to use the Versionable Package in my Laravel 4 app based on the new, trait based implementation like this
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
namespace MyApp\Models;
class User extends Eloquent implements UserInterface, RemindableInterface {
use Venturecraft\Revisionable\Revisionable;
...
When i use this code above, I get this error
Class 'MyApp\Models\Eloquent' not found
I have also tried changing the namespace to app\models but it still shows the not found error.
Please could someone help me out here? I followed all the instructions on the Github page but as I'm new to Laravel, I may be missing something.
You could use either of the following solutions:
Change your class declaration to:
class User extends \Eloquent implements \UserInterface, \RemindableInterface
Or, simply remove the line:
namespace MyApp\Models;
I would recommend taking the second solution (removing the namespace) for now, until you are comfortable with namespaces in PHP.
Additionally, you should be using RevisionableTrait, not Revisionable, e.g.,
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use Venturecraft\Revisionable\RevisionableTrait;
You must write fully qualified name for Eloquent
class User extends \Eloquent implements UserInterface, RemindableInterface

Resources