How to use Laravel revisionable with morphMaps? - php

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.

Related

Class 'App\Http\Controllers\rimdetails' not found?

I have created a laravel project with Rims and tyres. when clicking a Rim from the index page, i would like to link directly to a rim detail page, like a productdetails page based on the clicked product.
So far, it seems that my route and blade files are fine! but i have no idea how to make the controller function. i have tried with:
public function show($id)
{
$rimdetails = rimdetails::with('rimdetails')->findOrFail($id);
return View::make('rimdetails', compact($rimdetails));
}
and getting the error :
Class 'App\Http\Controllers\rimdetails' not found
try this one
for laravel 6
Route::get('rims','App\Http\Controllers\RimDetails#show');
for laravel 8
use App\Http\Controllers\RimDetails';
Route::get('rims',[RimDetails#show,'show']);
and also make sure you have exact name of file as you have in controller
You need to alias this rimdetails class if you want to reference it correctly. Currently you are in a declared namespace and referencing a class, rimdetails. PHP thinks you are referring to a class named rimdetails in the current namespace App\Http\Controllers so its looking for App\Http\Controllers\rimdetails as per the error.
Add this below the namespace declaration and before the class definition:
use App\rimdetails;
Assuming your model is in the app folder.

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

Method ::find not found in Laravel 5.2 / Eloquent

In Laravel 5.2 I made a folder app/Models.
This is one of my models: app/Models/Pin.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Pin extends Model {
protected $table = 'pins';
}
In my composer.json I added to "classmap": app/Models.
Then I runned composer dump-autoload
In my PinController, I have:
use App\Models\Pin as Pin;
When I do: Pin::all(); it returns a collection.
But when I try: Pin::find(1); I get:
Method find not found in class \App\Models\Pin
Is their a way I can get all methods from Eloquent?
I see nothing wrong with your approach. It should be working, and it works if I try it on a clean Laravel 5.2.5 install. Maybe some other dependencies are causing side effects for you. Have you tried to isolate the problem on a clean install?
You can find all Eloquent methods in the API docs, but keep in mind the Model class has a magic __call() method. This is why you won't see a find() method directly in the Model class; instead calls to Model::find() will be passed to the Builder::find()method.

custom method in laravel 4.1 model not working

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.

Referring to an Eloquent model from package

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

Categories