Laravel 5.5: Dependency Injection on user model - laravel

I have a BaseRepository class with methods which will be valid for all Models. The only problem is, that the User model is derived from use Illuminate\Foundation\Auth\User. So it throws me a type error because the constructor requires an instance of Illuminate\Database\Eleoquent\Model. How can I solve the problem?
Here is my UserRepository.php:
namespace App\Repositories\User;
use App\Model\User; use App\Repositories\Base\BaseRepository;
class UserRepository extends BaseRepository {
public function __construct(User $user)
{
parent::__construct($user);
}
}
BaseRepository.php
namespace App\Repositories\Base;
use App\User;
use Illuminate\Database\Eleoquent\Model;
class BaseRepository {
public function __construct (Model $model) {
$this->model = $model;
}
public function all() {
return $this->model->orderBy('id','desc')->get();
}
}
This is the error: Type error:
Argument 1 passed to App\Repositories\Base\BaseRepository::__construct() must be an instance of Illuminate\Database\Eleoquent\Model, instance of App\User given, called in C:\wamp64\www\adblog\app\Repositories\User\UserRepository.ph‌​p

Customize your User model like this
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
}

Related

Why is it saying Cannot declare class App\Http\Controllers\Controller, because the name is already in use in?

[![This is the error][1]][1]
This is the Controller.php but i think this is a built in when creating a laravel project.
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

Laravel 5.5 Type error: Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance

In my LoginController under Auth, I have used the following codes:
namespace App\Http\Controllers\Auth;
use App\Model\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
use Hash;
use Auth;
use DB;
use App\Model\UserAdmin;
class LoginController extends Controller {
use AuthenticatesUsers;
public function __construct() {
$this->middleware('guest')->except('logout');
}
public function doLogin(Request $request) {
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'status' => '1',
);
if (Auth::guard('admin')->attempt($userdata)) {
return Redirect::intended('/administrator/dashboard')->with('successMessage', 'You have successfully logged in.');
}
}
}
And in UserAdmin (model) under app/Model is as follows:
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Config;
class UserAdmin extends Authenticatable {
protected $table = 'adminusers';
public $timestamps = false;
protected $fillable = ['firstName', 'lastName', 'email', 'company', 'website'];
public function __construct() {
parent::__construct(); // Don't forget this, you'll never know what's being done in the constructor of the parent class you extended
}
}
After submitting the login details, it shows me the error:
Type error: Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\Model\UserAdmin given, called in /var/www/html/XXXXXX/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php on line 379
I suppose that you required to add implements \Illuminate\Contracts\Auth\Authenticatable to your UserAdmin model class definition.
class UserAdmin extends Model implements
\Illuminate\Contracts\Auth\Authenticatable
You must use Authenticatable in User model
for example:
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
//your code
}
You must declared use AuthenticableTrait for Authenticatable interface.
For example :
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Database\Eloquent\Model;
class Company extends Model implements Authenticatable
{
use AuthenticableTrait;
Try and run 'composer dump-autoload' to check for "ambiguous User class resolution". It is likely you have two classes Defined as User Class.
use Illuminate\Foundation\Auth\AuthenticatesUsers;
Then in your model class, extends AuthenticatesUsers instead of Model.
You must extends Authenticatable class and implements JWTSubject in User model
For example :
class User extends Authenticatable implements JWTSubject {
Go to your Model and instead of extending Model, extend User
<?php
namespace App;
class Staff extends \Illuminate\Foundation\Auth\User
{
}

Laravel Assigning Middleware within Controller’s constructor, should the constructor include parent::__construct()?

Here is example for
Laravel: Assigning Middleware within Controller’s constructor
https://www.tutorialspoint.com/laravel/laravel_controllers.htm
class UserController extends Controller {
public function __construct(){
$this->middleware('auth');
}
}
My question is should it be
class UserController extends Controller {
public function __construct(){
$this->middleware('auth');
parent::__onstruct();
}
}
Due to the UserConstroller override the __construct. If it does not include parent::__construct(), the parent constructor will not be called. That could be a problem, correct?

Laravel 5.2 use Illuminate\Http\Request Not working in subfolder Controller

I have make an AdminController in App\Http\Controllers folder with an index method and the Request it is working fine, i type in url bar http://localhost/brosta/public/index and i take the path "index" to my browser it's ok!
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class AdminController extends Controller
{
public function index(Request $request)
{
$path = $request->path();
print_r($path);
return view('index');
}
But when i make the controller AdminController in a subfolder like App\Http\Controllers\Admin the requested path is not working. How can make it to work?
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
class AdminController extends Controller
{
public function index(Request $request)
{
$path = $request->path();
print_r($path);
return view('index');
}
}
Ok! Now i have a different problem with the Request! With this way is working
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index(Request $request)
{
print_r($request->path());
}
}
But with this way is not working!
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index()
{
$request = new Request;
print_r($request->path());
}
}
Try adding this line in your second controller.
use App\Http\Controllers\Controller;
Since you are in a different namespace you need to add the correct namespace for the Controller class.

Setting up Queue in Lumen Framework

I'm trying to set up queue in Lumen using the guide from lumen page:
http://lumen.laravel.com/docs/queues
<?php
namespace App\Jobs;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
class BlastEmail extends Job implements SelfHandling, ShouldQueue
{
public function sendEmail()
{
[...CODE TO SEND EMAIL...]
}
public function handle()
{
$this->sendEmail();
}
}
and in My Controller
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Jobs\BlastEmail;
use App\Models\Blast;
use App\Models\Subscriber;
use Illuminate\Http\Request;
use Validator;
class BlastsController extends BaseController
{
public function queue(Request $request)
{
$job = (new BlastEmail($email,$request->input('content'),$request->input('title')));
$this->dispatch($job);
}
}
Controller.php
<?php
namespace App\Http\Controllers;
use Laravel\Lumen\Routing\Controller as BaseController;
class Controller extends BaseController
{
//
}
BaseController.php
use Dingo\Api\Routing\Helpers;
use Illuminate\Routing\Controller;
use Cartalyst\Sentinel\Native\Facades\Sentinel;
class BaseController extends Controller {
function someFunctionThatOtherGuyWrote()
{
// Some code that other guy wrote
}
}
And I got
Undefined method App\Http\Controllers\BlastsController::dispatch
Do I miss something?
Looking at your code, your BlastsController extends App\Http\Controllers\BaseController and not App\Http\Controllers\Controller.
You should change it to extend Controlller class changing class BlastsController extends BaseController into class BlastsController extends Controller because this class will finally uses Laravel\Lumen\Routing\DispatchesJobs trait that contains dispatch method
EDIT
After update you didn't show full BaseController file but it seems you extends wrong class. You extend Illuminate\Routing\Controller and you should extend App\Http\Controllers\Controller

Resources