Laravel 8 Auth Model Different - php

I want to create a model for my project but i don't know what's the different between the original MVC style and laravel default auth model
there's a directory different too. The original MVC was in Model folder, while the defaulth auth is in controller folder. I don't know why make a model inside the Controller folder
This is the code that's work ( laravel default auth model )
source : https://www.5balloons.info/changing-authentication-table-laravel/
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class CustomUser extends Authenticatable
{
use Notifiable;
protected $table = 'customusers';
protected $fillable = [
'name','username','email','passcode','active'
];
protected $hidden = [
'passcode',
];
}
while this code is not work with error Class 'App\Models\Authenticatable' not found
i can create a work around to use the class, but i need to understand why
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class TestUser extends Authenticatable
{
use HasFactory, Notifiable;
protected $table = 'testuser';
protected $fillable = [
'username','password',
];
protected $hidden = [
'password',
];
/*
public funtion getAuthPassword()
{
return $this-> passcode;
}
*/
}

Related

Load multiple models from single controller

need help laravel. I have 2 model in different folder
app\User
app\model\Role
there is no problem when i used at UsersController -> call app\User, or RolesController -> call app\model\Role
but, when i used both models on UsersController , the app\model\Role didnt work
==================== UsersController ======================
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use app\User;
use app\model\Role as Role;
use DataTables;
class UserController extends Controller
{
public function index(Request $request){
$data['title_page'] = 'User';
$data['roles'] = Role::all(); // this line show error
return view('admin/user', $data);
}
}
======================== app\model\Role ===================
namespace App\model;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
protected $fillable = ['id','name'];
protected $tables = 'roles';
public function users(){
return $this->belongsTo('App\User','role');
}
}
======================== app\User =======================
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = "users";
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password','role', 'status'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function roles(){
return $this->hasOne('App\model\Role','id');
}
}
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Class 'app\model\Role' not found
Try with capital 'A' on 'App'. Some systems are case sensitive. So use App\Model\Role depending on your folder name for model. IE if your model folder is lower case, match it in the use statement.
Also, you don't need the as keyword here unless there are conflicts - you may be fine with just use App\Model\Role without the as Role.
One more item, make sure the Role class is actually in the model folder and that your namespace is at the top of the php file correctly referencing the App\Model namespace. So in your Role class make sure the caps match your use call -
<?php
namespace App\Model

FatalThrowableError in User.php line 7: Class 'App\Authenticatable' not found (laravel 5.3)

I have used this code
User.php file
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
protected $guarded = ['role'];
protected $hidden = [
'password', 'remember_token',
];
}
?>
Error: FatalThrowableError in User.php line 7: Class
'App\Authenticatable' not found
How can I solve it?
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $guarded = ['role'];
protected $hidden = [
'password', 'remember_token',
];
}
The class that Laravel uses is Illuminate\Foundation\Auth\User which they alias as Authenticatable.
At the top of your user model, you can import this base class which the default User model extends:
use Illuminate\Foundation\Auth\User as Authenticatable;
This base class implements the standard interfaces and uses the standard traits (including the one named Authenticatable) for you.

Passing second user model data to a controller in Laravel

So I have 2 User models, one is User and other is OrgUser for company users.
I'm trying to pass OrgUser to a OrgUserController and display single company user in show method. But I always get an empty object.
User Model
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password', 'verified', 'verification_token',
];
protected $hidden = [
'password', 'remember_token', 'verification_token',
];
OrgUser Model
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class OrgUser extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password', 'verified', 'verification_token', 'admin'
];
protected $hidden = [
'password', 'remember_token', 'verification_token',
];
OrgUserController
<?php
namespace App\Http\Controllers\OrgUser;
use App\Http\Resources\OrgUserResource;
use App\OrgUser;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;
class OrgUserController extends ApiController
{
public function show(OrgUser $orgUser)
{
// OrgUserResource::withoutWrapping();
//
// return new OrgUserResource($orgUser);
return $orgUser;
}
Routes
Route::resource('orgusers', 'OrgUser\OrgUserController', ['only' => ['index', 'show']]);
Is there something that I would need to add to OrgUser model so I could pass data to controller like that? Because it works if I pass $id.
So It seems the problem was with naming in show method. I need to pass $orguser and not $orgUser. Because in my route it was /orgusers/{orguser}.
The show function should be accepting the $id rather than the model. If you change your code to the following, it should work:
public function show(Request $request, $id)
{
$orgUser = OrgUser::find($id);
return $orgUser;
}

Laravel 5.5 use User model to relationship

I want to use the User model that already in the app folder. but it seem it only extends Authenticatable and can't extend by the model Class i want to use it as a link to the other class, like user has only one employee. what else can i do to recycle the user model that is already extended by Authenticatable?
thanks for the help :)
this is the user model that extends the authenticatable
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function employee()
{
return $this->belongsTo('App\Employee');
}
}
And this is the employee model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
// protected $table = "positions";
// public function position()
// {
// return $this->hasMany('App\Position');
// }
protected $table = "employees";
public function employee()
{
return $this->hasOne('App\User');
}
}
It already inherited Model class. If you follow Illuminate\Foundation\Auth\User you will finally find that it is inherited from a Model class. So, your User model has the same features that your other models have.

Laravel 5.3 + Sentinel: BadMethodCallException in Builder.php line 2450

I'm trying to build my first Laravel application by following a few guides on the internet and I'm feeling I'm missing something obvious. Here is the code.
Error
BadMethodCallException in Builder.php line 2450: Call to undefined
method Illuminate\Database\Query\Builder::addresses()
User-Model
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Sentinel;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'email', 'password',
];
protected $hidden = [
'password',
'remember_token'
];
public function addresses()
{
return $this->hasMany('App\CustomerAddress');
}
}
CustomerAddress-model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class CustomerAddress extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
CustomerAddress-controller
<?php
namespace App\Http\Controllers;
use App\CustomerAddress;
use Illuminate\Http\Request;
class CustomerAddressController extends Controller
{
public function create(Request $request)
{
$address = new CustomerAddress();
$address->address = $request['address'];
$request->user()->addresses()->save($address);
}
}
Error appears after this piece of code:
$request->user()->addresses()->save($address);
Any ideas? Thanks and cheers
In .config/cartalyst.sentinel.php, change 'model' => 'Cartalyst\Sentinel\Users\EloquentUser' to 'model' => 'App\User' to use your user model with the addresses relation defined
In ./app/User change User extends Authenticatable to User extends \Cartalyst\Sentinel\Users\EloquentUser to extend sentinel's user to your app's User model
Finally, your controller code should now be
`$address = new CustomerAddress();
$address->address = $request->input('address');
$request->user()->addresses()->save($address);`
and everything should be peachy

Categories