Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yanikkumar's avatar

Need Help Laravel Package Folder Is Renamed Different?

PS: I accidently clicked best answer instead of reply. This is Still Open.

I don't know if I'll explain this correctly or not but I install the package laravel overtrue/follow and using this

use Overtrue\LaravelFollow\Followable;

class User extends Authenticatable

{
    use Followable;

but the problem is folder created in vendor after publishing is

overtrue/laravel-follow/src

and includes the methods. Now the problem is if I rename the folder back to laravelfollow then the method is easily accessible but then error comes in that laravel-follow not found and if let it be like that then the followable is not working.

Need Help!

0 likes
34 replies
martinbean's avatar
Level 80

@yanikkumar You should not be touching any file or folder in your vendor directory!

You don’t need to mess around with folder names to be able to import a class or a trait from a package.

yanikkumar's avatar

@martinbean Can you help me out with this. I'm having trouble accessing the methods while making a ajax call.

PS: I accidently clicked best answer instead of reply. LOL

1 like
tykus's avatar

You are confusing the package's directory structure with the PHP namespace it exposes.

What errors/exceptions are you seeing?

martinbean's avatar

I don’t really understand? You’re adding a trait to a model. You don’t access models directly from AJAX; you make requests to a controller.

yanikkumar's avatar

@tykus I'm not confusing the structure. Basically the follower trait isn't accessible because it is unable to locate the folder and the followable. So to use followable the

use Overtrue\LaravelFollow\Followable;

must be located correctly and that's confusing because after vendor publish the folder created is laravel-follow

martinbean's avatar

@yanikkumar Again, I don’t think you understand the difference between folders classes and traits are stored in, and autoloading.

yanikkumar's avatar

Yes while making requests it gives internal server error in the console because the method in the controller that is associated to the model is nit accessible.

Route:

Route::post('/follow', 'UserController@follow')->name('user.follow');

User model:

use Overtrue\LaravelFollow\Followable;

class User extends Authenticatable
{
    use Followable;
}

UserList Controller:

public function follow(Request $request)
{
    $user = User::find($request->user_id);
    $response = Auth::user()->Followable::toggleFollow($user);

    return response()->json(['success' => $response]);
}

}

AJax Call:

$.ajax({
            type: 'POST',
            url: '/follow',
            data: { user_id: user_id },
            success: function(data) {
                console.log(data.success);
            }
        })
yanikkumar's avatar

There is no error showing except the console error 500 " internal server error "

tykus's avatar

@yanikkumar what is this (it's not valid PHP)????

Auth::user()->Followable::toggleFollow($user)

Have you read the documentation for the package??

Auth::user()->toggleFollow($user)

When a trait is used by a class, its methods are mixed in with the class' own method; so it is as if the User class (in this case) has all of the Followable method itself.

martinbean's avatar

If you have a 500 error, then that means an exception was thrown and it will be in your application’s error log (storage/logs/laravel.log by default).

yanikkumar's avatar

I accidentally wrote that and after correcting that still the method is not accessible. And yes I'm reading the docs

tykus's avatar

Ok, it is useless to continue trying to help when you do not provide the correct or complete information. We have asked for the exception/error details; we have show obvious errors in your code snippets and we have told you that the directory structure is not relevant. What more can we do?

2 likes
tykus's avatar

Looks like logging is misconfigured. What is the LOG_CHANNEL environment variable; and the logging config looking like?

EDIT you removed the stack trace?

Snapey's avatar

after vendor publish the folder created is laravel-follow

php artisan vendor publish does not change the folder names of anything in the vendor folder. It allows the package to move files into your application space so that you can reconfigure some of its behaviour. For instance, it will create a file in the config folder for package settings.

and PLEASE learn how to format code for this forum. three backticks ``` on their own line before and after code.

yanikkumar's avatar

@snapey I followed this documentation laravel-follow and after

php artisan vendor:publish --provider="Overtrue\LaravelFollow\FollowServiceProvider" --tag=migrations

the folder generated in the vendor was laravel-follow and I can't access the traits in user model. there after I can't use

toggleFollow($user);

saying it is undefined

tykus's avatar

Publishing migration files will in no way affect the functionality of a Trait in a Model regardless the directory name. You still are focussed on the wrong part of your application. Publishing is essentially copying files (or stubs) from the package sub-directory in the vendor directory; nothing more.

Did you run the migrations?

yanikkumar's avatar

@tykus Yes I know publishing is essentially copying files in vendor. I was just explaining what I did and I followed the steps as mentioned in the docs and now I'm stuck at undefined method toggleFollow($user) that's it.

tykus's avatar

Can you show you your User model (properly formatted)?

yanikkumar's avatar

@martinbean What you asked earlier

[2021-03-12 14:16:56] laravel.EMERGENCY: Unable to create configured logger. Using emergency logger. {"exception":"[object] (InvalidArgumentException(code: 0): Log [] is not defined. at C:\Users\belie\Projects\BMNEWS\vendor\laravel\framework\src\Illuminate\Log\LogManager.php:168)
[stacktrace]
#0 C:\Users\belie\Projects\BMNEWS\vendor\laravel\framework\src\Illuminate\Log\LogManager.php(102): Illuminate\Log\LogManager->resolve(NULL)
#1 C:\Users\belie\Projects\BMNEWS\vendor\laravel\framework\src\Illuminate\Log\LogManager.php(90): Illuminate\Log\LogManager->get(NULL)
#2 C:\Users\belie\Projects\BMNEWS\vendor\laravel\framework\src\Illuminate\Log\LogManager.php(500): Illuminate\Log\LogManager->driver()
#3 C:\Users\belie\Projects\BMNEWS\vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php(118): Illuminate\Log\LogManager->error('Undefined const...', Array)
#4 C:\Users\belie\Projects\BMNEWS\app\Exceptions\Handler.php(37): Illuminate\Foundation\Exceptions\Handler->report(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#5 C:\Users\belie\Projects\BMNEWS\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(378): App\Exceptions\Handler->report(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#6 C:\Users\belie\Projects\BMNEWS\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(143): Illuminate\Foundation\Console\Kernel->reportException(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#7 Command line code(1): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#8 {main}
"} 
[2021-03-12 14:16:56] laravel.ERROR: Undefined constant 'Overtrue\Laravel' {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Undefined constant 'Overtrue\Laravel' at C:\Users\belie\Projects\BMNEWS\config\app.php:168)
[stacktrace]
#0 C:\Users\belie\Projects\BMNEWS\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\LoadConfiguration.php(72): require()
#1 C:\Users\belie\Projects\BMNEWS\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\LoadConfiguration.php(39): Illuminate\Foundation\Bootstrap\LoadConfiguration->loadConfigurationFiles(Object(Illuminate\Foundation\Application), Object(Illuminate\Config\Repository))
#2 C:\Users\belie\Projects\BMNEWS\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(210): Illuminate\Foundation\Bootstrap\LoadConfiguration->bootstrap(Object(Illuminate\Foundation\Application))
#3 C:\Users\belie\Projects\BMNEWS\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(322): Illuminate\Foundation\Application->bootstrapWith(Array)
#4 C:\Users\belie\Projects\BMNEWS\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(131): Illuminate\Foundation\Console\Kernel->bootstrap()
#5 Command line code(1): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#6 {main}
"} 

@tykus This is user model

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Post;
use App\Role;
use App\Photo;
use Overtrue\LaravelFollow\Followable;

class User extends Authenticatable
{
    use Notifiable, Followable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'role_id', 'photo_id', 'get_email', 'username', 'about', 'url', 'website', 'facebook', 'instagram', 'twitter', 'youtube'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    // Relation between role and user
    public function role()
    {
        return $this->belongsTo(Role::class);
    }

    public function photo()
    {
        return $this->belongsTo(Photo::class);
    }

    public function post()
    {
        return $this->hasMany(Post::class);
    }
}

Any other thing I can share?

tykus's avatar

Looks like logging is misconfigured. What is the LOG_CHANNEL environment variable; and the logging config looking like?

What I mentioned earlier about that log.

This is user model

Nothing appears out of place here; what does your code look like wherever you are using the trait methods?

yanikkumar's avatar
Auth::user()->toggleFollow($user)

When a trait is used by a class, its methods are mixed in with the class own method so it is as if the User class (in this case) has all of the Followable method itself.

You mentioned this earlier and I fixed that but it says toggleFollow($user) method is undefined I don't know why, I'll try to figure out.

Snapey's avatar

You have Undefined constant 'Overtrue\Laravel' in your config/app.php file. What did you do to that file?

yanikkumar's avatar

I did nothing it is still there. I guess I've to redo the process again and see where I'm having problem.

Snapey's avatar

but you must have edited that file for it to contain anything related to overtrue

yanikkumar's avatar

@snapey Hey, there I checked everything, and it all same. I'm having one problem. I guess the trait method is not accessible in the controller as expected.

Overtrue Followable is in User Model and I'm accessing toggleFollow($user) it in UserController which includes User model

but still say toggleFollow() is undefined that's the final problem.

Edit: when I'm using

User::toggleFollow($user) 

Then the method is accessible but that's not how we should use. But when using

Auth::user()->toggleFollow($user) 

Then method is undefined.

Snapey's avatar

are you logged in or is Auth::user() null ?

yanikkumar's avatar

Yes Sir, Any way this is working like this anyhow, but I'm not sure why. I tried to figure that out and came up with this solution.

    public function follow(Request $request)
    {
        $authUser = Auth::user();
        $user = User::findOrFail($request->user_id);

        if ($authUser !== null && $authUser instanceof User) {
            $response = $authUser->toggleFollow($user);
        }

        return response()->json(['success' => $response]);
    }

using if ($authUser !== null && $authUser instanceof User) { } This condition let me access the toggleFollower() method.

But now another problem here. the response()->json(['success' => $response]);

returning undefined while console log.

Snapey's avatar

because if your if condition is false then $response is unset

so this just proves you did not call the toggleFollow function

Again pointing to your auth user not being logged in

yanikkumar's avatar

@snapey I can only understand that my response is getting null. in the networks tab I don't know why I'm logged in

Is is because of any middleware issues?

Snapey's avatar

using very basic diagnostics, you can print the current user in your controller and check it is set.

 dd(Auth::user());

Please or to participate in this conversation.