Laravel-auditing: BadMethodCallException in Macroable.php line 74: Method with does not exist

Created on 25 Oct 2016  路  13Comments  路  Source: owen-it/laravel-auditing

Using $model->audits->with('user')->last() won't work. It should be something like $model->audits()->with('user')->get()->last(), shouldn't it?

Most helpful comment

I just tried to explain the difference between calling an "eager loading method" vs. an "eager loading property". Its query builder object vs. eloquent collection. Just for your information from a technical point of view. Thanks for clarifying @paulghz

All 13 comments

Hi @bart , you're right

We can use eager loading to reduce this operation to just 2 queries. When querying, you may specify which relationships should be eager loaded using the with method:

$model->audits->with('user')->get()->last();

Reference

But it should be $model->audits() shouldn't it? Because if you don't call audits as a method you will get a collection in return.

Hi @bart , only $model->audits.

When I use $model->audits->with('user')->... it won't work. And for me it's clear because you will get a collection instead of a query builder object. Just for your information!

That's right.

This is not laravel-auditing behavior, all that why you're making is eloquent,

One To Many

A "one-to-many" relationship is used to define relationships where a single model owns any amount of other models. For example, a blog post may have an infinite number of comments. Like all other Eloquent relationships, one-to-many relationships are defined by placing a function on your Eloquent model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
        return $this->hasMany('App\Comment');
    }
}

Remember, Eloquent will automatically determine the proper foreign key column on the Comment model. By convention, Eloquent will take the "snake case" name of the owning model and suffix it with _id. So, for this example, Eloquent will assume the foreign key on the Comment model is post_id.

Once the relationship has been defined, we can access the collection of comments by accessing the comments property. Remember, since Eloquent provides "dynamic properties", we can access relationship functions as if they were defined as properties on the model:

$comments = App\Post::find(1)->comments;

foreach ($comments as $comment) {
    //
}

Of course, since all relationships also serve as query builders, you can add further constraints to which comments are retrieved by calling the comments method and continuing to chain conditions onto the query:

$comments = App\Post::find(1)->comments()->where('title', 'foo')->first();

Like the hasOne method, you may also override the foreign and local keys by passing additional arguments to the hasMany method:

return $this->hasMany('App\Comment', 'foreign_key');

return $this->hasMany('App\Comment', 'foreign_key', 'local_key');

Exactly. You called the where() method on the query builder object in your example, instead of the collection itself. And that's exactly what I liked to describe. However, the package is working great, thanks a lot!

@bart , I'm a little confused here. I'm sorry, I may not understand you very well. Could you show me the example please.

The above example works for me. When calling $model->audits->with('user') it doesn't because I try to eager load a collection instead of a query builder object. And that won't work. This is the point here.

I think the problem is in your documentation on this page : Displaying the Audits
To eager load the user, you said to write :
$team->audits->with('user');
But that creates an error : "BadMethodCallException in Macroable.php line 74: Method with does not exist."

The syntax given by @bart works :
$team->audits()->with('user')->get();

It's not a bug, but you should update your documentation with this syntax.

@bart , I'm sorry not to have understood before.

@achillesp , You're right, if anyone can send me a pull request I'll be very grateful.

@anteriovieira I've created a Pull Request with the change.

Perfect, thank you very much @paulghz

I just tried to explain the difference between calling an "eager loading method" vs. an "eager loading property". Its query builder object vs. eloquent collection. Just for your information from a technical point of view. Thanks for clarifying @paulghz

Was this page helpful?
0 / 5 - 0 ratings