i am working on a laravel project .almost i see in each controller these line format :
!auth()->user()->can('something (differ from each controller to other one)')
but in my php editor it says method {can} not found for this object. so i try to found method can and not found it .even i edit __Call magic method to see if when can method is calling does magic function run but i know it never run for can function.so how it is possible to link function to object when it is not defined in class and its all mother class.and where does can function locate?i search and i see laravel has some policy for authorize users but yet i don not know how can function link to user object without
define in classes and even magic method does not run .and how can i develope these type of function (for authorizing in laravel and change policy)
can method is a part of Authorizable trait in Illuminate\Foundation\Auth\Access
Look at your User model, you will found that model is extended from Illuminate\Foundation\Auth\User as Authenticatable. and Authenticatable uses the above trait.
it's part of the core of laravel, you can read the documentation from official docs
Related
Im building a Restful api, except that I DONT want to use Laravel's User authentication system (instead I use my own).
So im on stuck here:
$this->jwt_token = JWTAuth::attempt(['email'=>$email, 'pass'=>$pass])
which gives me the following error:
Type error: Argument 1 passed to
Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an
instance of Illuminate\Contracts\Auth\Authenticatable, instance of
App\User given, called in
**\vendor\laravel\framework\src\Illuminate\Auth\SessionGuard.php on line 387
So how do I solve this? any idea?
Thanks
It looks like your User model isn't extending the correct Eloquent class.
Pull up your user model, and make sure it extends Illuminate\Foundation\Auth\User.
In the default Laravel install, this is imported via use Illuminate\Foundation\Auth\User as Authenticatable, then the model extends Authenticatable.
If you don't want to use the User model for authenticating, you can change which model should be used in config/jwt.php, but whatever you choose will need to extend the Auth\User class as above.
I'm using Laravel 5 framework.
I extended the Illuminate\Http\Request Class and added some functions. So I changed bootstrap to boot with my Custom Http Request Class. They work well.
But when it come to integrated test. by extending their TestCase, they use the Request class as below
use Illuminate\Http\Request;
$request = Request::create();
Is there a way for me to override their class from using Illuminate\Http\Request to use MyApp\Http\Request at my own class? I don't want to change their code.
No.
Their code calls it directly, so there's unfortunately nothing you can do.
I am trying to use custom method in User model with class name User in laravel4.1. i changed the $table attribute to my table name and added a custom method names 'public function abc' in user model. Then in my user controller i tried like this :-
$u= new User;
$u->abc();
but its not working and giving following error :-
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::abc()
and i dnt know why this happening everything seems fine,help me out in this guys.
UPDATE :SOLVED ,Done Nothing
I DO NOT KNOW WHAT IS THE PROBLEM WITH LARAVEL
$u= new user;
$u->abc();
i just changed User to user and its started working and i dnt even know why ,anyone know reason??
Every method on a Model get passed on to a new QueryBuilder
User::where()
User::find()
User::{relationship}()
If you instantiate a model like this
$user = User::find()->method();
it will work. Don't try to make your Eloquent models too fat.
Just create a Repository to make your Controllers as thin as possible and your Eloquent just as intelligent as it can be by using the tools given by Eloquent (relationships, hidden attributes, accessors & mutators, $this->appends, ...)
Everything else belongs in your Repositories.
Try running
composer dumpautoload
If this doesn't work, be sure that there is only one class called User in your whole project. There may be a package, migration or something with the same name. Try putting your custom User class in a namespace.
I am trying to use an Eloquent model inside the Sentry package (this could've been any package).
I've refered to it using use App\Models\User; in the top, and User:: when I need to call it.
However, I am getting this error: Class 'App\Models\User' not found
What am I doing wrong?
The current code is here: http://paste.laravel.com/H19
I assume thath your models are in the App part of the application.
In laravel, the App namespace is the top level namespace (the default one).
So to call your models, you have to call \User (because \ refer to the top level namespace)
Alternatively you can do:
use \User as User
And then you can call it User instead of \User
class Api extends CI_Controller {
public function index()
{
show_error("You are not authorized to access this page", 401);
}
I have an api class class Api extends CI_Controller and an another
class myproject extends , now if want to use the functions of the api class in myproject class. how can i do . do i have to create an object of api class or just extend the myproject class with parent::api class . please help me as i m not good at oops.
details - i have made a class "class myproject extends ci_controller" which has different functions for user registration and login application. it all works fine using a single controller. But now want to use an api file.which has functions for login. how can i call those functions in api file from "class myproject"
You can create an helper, like the form one and use your method.
Then you can load it:
$this->load->helper('api');
First of all this looks more like a software design problem rather then a real code issue.
CodeIgniter is based on the MVC concept, and the Controller is only meant to build up the page.
You should probably make a library for all the API functionality, and have the controller calling that library and converting the data to JSON or whatever you want it to be converted to.
For myself i apply a simple rule:
Never write/call a Controller method that doesn't generate any form of output.
I recommend you to stick to the CodeIgniter way of coding, and not try to avoid them.
You should just need to extend the API class, as in
class Myproject extends Api { ... }
but there a few other issues, such as include_once("Api.php"), for example, in the Myproject.php file so that the subclass can "see" the superclass declaration. This thread on the codeigniter forums discusses these issues (sorry for the google webcache version but the forums are down for maintenance at the moment).
Firstly, you can't call a function of a controller in a controller unless and until you are inheriting it.
It's better that you create API class as library, instead of a controller.
If you have some restriction to have it as a controller only then use these APIs through curl calls.
But the best way is to have them in helper, if you are accessing them from your own server only.