custom method in laravel 4.1 model not working - php

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.

Related

where does laravel can function for user object define?

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

How to call createDeleteForm in CRUD controller for another entity

I have a basic CRUD controller using the "php bin/console generate:doctrine:crud AppBundle:Product" command. So I have the basic generated productController.php along with a Product.php entity that I made before, the generated CRUD product views, etc.
In ProductController.php there is a showAction method that has this line:
$deleteForm = $this->createDeleteForm($product);
I am trying to create a settings controller that embeds the product's showAction method but, as you can guess, trying to use
$this->createDeleteForm($product) yields
"Attempted to call an undefined method named "createDeleteForm" of class "AppBundle\Controller\SettingsController""
What's the best way to incorporate a settings controller and, if I'm on the right track, how can I generate the delete form for the product within the settings controller?
Thanks so much for any help you can offer!

laravel's model gets request info from controller

I have a problem with code using Laravel. I define an attribute in a model to get a list. It takes a lot of time. I use this model in a controller. Follow the code:
protected $appends=["consume_info"];
public function getConsumeInfoAttribute(){
//query a lot of information from mysql
}
I'm wondering if there is an attribute in Controller or Model to avoid a query with mysql in model.
Is there a setting to tell Laravel when to load this appended attribute or not?
Why not just remove the consume_info from $appends array. You will get the $model->consumer_info and that too only when you need this.

laravel 5.3 + Tymon\JWTAuth = How to create token with own authentication

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.

How to use Laravel revisionable with morphMaps?

I have a morphMap setup in my AppServiceProvider boot method that is firing when the Laravel revisionable package calls for the record so nothing is being returned. I am looking to figure out how to make revisionable work with morphMaps. Please note that revisionable is storing the full namespace when saving the record but when calling the record revisionable is using the value I put in for the morphMap.
For example revisionable saves App\Models\Organization\Organization but my morphMap translates that to just organization when calling for the revisionHistory().
I know this is an old question but I see Venturecraft/Revisionable developers are not giving answers to this problem.
The error ocurrs when the package try to instatiate a new model with the value of the property $this->revisionable_type in Venturecraft\Revisionable\Revision, because the class does't exist when using the key of $morphClass array instead of the real namespace of the model.
I figured out that the problem is solved when I override the value of this property with an Laravel Accessor like this:
use Illuminate\Database\Eloquent\Relations\Relation;
public function getRevisionableTypeAttribute($value)
{
$morphedModel = Relation::getMorphedModel($value);
return !empty($morphedModel) ? $morphedModel : abort(400, "The namespace {$value} hasn't been added to the Relation::morphMap() method.");
}
Now, I don't know how to add this Accessor to the Revision model without editing the one that comes in the vendor folder.
If you can give a try and share what you get will be helpful.

Categories