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

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

Related

Laravel 8 Auth Model Different

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;
}
*/
}

BadMethodCallException Call to undefined method App\Models\Menu::menu()

I have 'Menus' table and this website is for a restaurant. When I add a new record through Laravel Backpack Crud, it gives me an error
BadMethodCallException Call to undefined method
App\Models\Menu::menu()
however, it adds the record to DB. When I want to update some record, it gives me this error but does not update the DB. Where can be the problem?
Here is my model:
<?php
namespace App\Models;
use Backpack\CRUD\CrudTrait;
use Illuminate\Database\Eloquent\Model;
class Menu extends Model
{
use CrudTrait;
protected $table = 'menus';
protected $fillable = ['image', 'name', 'description', 'price', 'category_id', 'popular'];
public function category()
{
return $this->belongsTo(Category::class);
}
Category.php:
<?php
namespace App\Models;
use Backpack\CRUD\CrudTrait;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use CrudTrait;
protected $table = 'categories';
public function menu()
{
return $this->hasMany(Menu::class);
}
MenuCrudController:
/*
|--------------------------------------------------------------------------
| CrudPanel Basic Information
|--------------------------------------------------------------------------
*/
$this->crud->setModel('App\Models\Menu');
$this->crud->setRoute(config('backpack.base.route_prefix') . '/menu');
$this->crud->setEntityNameStrings('menu', 'menus');

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

Class 'Illuminate\Foundation\Auth\User' not found JWT Auth Laravel

I have written code for registration and login using JWT authentication. In this code registration function works fine but login function doesn't works. Login function prompts an error as Class 'Illuminate\Foundation\Auth\User' not found
My user model is
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $table = 'users';
public $timestamps = false;
protected $primaryKey = 'user_name';
protected $fillable = ['user_name','password'];
}
My UserController is
class UsersController extends Controller
{
public function login()
{
$credentials = request()->only('user_name','password');
try{
$token = JWTAuth::attempt($credentials);
if($token){
return response()->json(['error'=>'invalid_credentials'],401);
}
}
catch(JWTException $e){
return response()->json(['error'=>'something went wrong'],500);
}
return response()->json(['token'=>$token],200);
}
public function register()
{
$user_name = request()->user_name;
$password = request()->password;
$user = User::create([
'user_name'=>$user_name,
'password'=>bcrypt($password)
]);
$token = JWTAuth::fromUser($user);
return response()->json(['token'=>$token],200);
}
}
The login function shows the error as
Class 'Illuminate\Foundation\Auth\User' not found
In your controller I guess you forgot to use your model "User" add it below the namespace declaration, Or it's a conflict with Illuminate\Foundation\Auth\User
use\App\User;
And you must run the following:
composer update
composer dump-autoload
The problem exists with my user model and I solved it
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{use Authenticatable, CanResetPassword;
//
protected $table = 'users';
public $timestamps = false;
protected $primaryKey = 'user_name';
protected $fillable = ['user_name','c_name','accessibility_level','password','role','contact_number','address'];
}

ErrorException in EloquentUserProvider.php line 114: Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials

I am trying to authenticate my user with the help of Helpers
For this purpose i have make Helper folder in app directory. Add the following lines of code to the composer.json
"files": [
"app/Helpers/UserHelper.php"
],
Make HelperServiceProvider.php in App\Provider directory, and use the following code in it.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
require_once($filename);
}
}
}
after this i have add alias in app.php as well as add provide like this
//this is an alias
'UserHelper' => App\Helpers\UserHelper::class,
//this is an provider
App\Providers\HelperServiceProvider::class,
My User model is
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $table='users';
protected $fillable =['username', 'password', 'firstname', 'lastname', 'email', 'phone', 'groupname', 'about', 'image'];
public static $login = [
'username' => 'required|',
'email' => 'required|',
'password' => 'required'
];
}
This my UserHelper
<?php namespace App\Helpers;
use Illuminate\Support\Facades\Auth;
class UserHelper {
public static function processLogin($inputs){
if(Auth::attempt($inputs)){
return TRUE;
} else {
return FALSE;
}
}
}
Here is my Login Function
<?php
namespace App\Http\Controllers;
use App\User;
use Input;
use Illuminate\Support\Facades\Validator as Validator;
use App\Helpers\UserHelper;
class LoginController extends Controller
{
public function login() {
$inputs = Input::except('_token');
$validator = Validator::make($inputs, User::$login);
if($validator->fails()){
print_r($validator->errors()->first());
} else {
$respones = \UserHelper::processLogin($inputs);
if($respones){
return 'loginView';
} else {
return 'not a user of our DB';
}
}
}
}
I have also updated my composer and after i login to application following error comes up , i am searching this for last 5 hour any help ?
Reards
In your code you are extending the class User extends Model but when you are using auth functionality in laravel you need to extend the auth rather than model..
Keep Illuminate\Foundation\Auth\User and extends the model like this...
class User extends Authenticatable{
//code here
}

Categories