Search code examples
phplaravellaravel-5eloquentlaravel-5.3

upgrading to laravel 5.3, "Call to undefined method Illuminate\Database\Query\Builder" error


I have a Laravel 5.2 project that works great. I tried to upgrade it to Laravel 5.3 and it gave errors such as:

BadMethodCallException in Builder.php line 2448: Call to undefined method Illuminate\Database\Query\Builder::friends()

or any other methods in my User.php model have the same error. For example, when I comment friends() in HomeController.php I have:

BadMethodCallException in Builder.php line 2448: Call to undefined method Illuminate\Database\Query\Builder::getNameOrUsername()

This is my User model:

<?php

namespace Activentum\Models;

use Activentum\Models\Status;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract ;

class User extends Model implements AuthenticatableContract
{
    use Authenticatable;

    protected $table = 'users';

    protected $fillable = [
        'username',
        'email', 
        'password',
        'first_name',
        'last_name',
        'location',


    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];


    public  function getName()
    {
        if ($this->first_name && $this->last_name){
            return "{$this->first_name} {$this->last_name}";
        }

        if ($this->first_name) {
            return $this->first_name;
        }
        return null;
    }


    public function getNameOrUsername()
    {
        return $this->getName() ?:$this->username;
    }    
    public function getFirstnameOrUsername()
    {
        return $this->first_name ?: $this->username;
    }
    public function getAvatarUrl()
    {
        return "https://www.gravatar.com/avatar/
        {{md5($this->email)}}?d=mm&s=40";
    }



    public function statuses()
    {
        return $this->hasMany('Activentum\Models\Status', 'user_id');
    }
    public function likes()
    {
        return $this->hasMany('Activentum\Models\Like', 'user_id');
    }
    public function friendsOfMine()
    {
        return $this->belongsToMany('Activentum\Models\User',
         'friends', 'user_id','friend_id');    
    }

    public function friendOf()
    {
        return $this->belongsToMany('Activentum\Models\User',
         'friends', 'friend_id', 'user_id');    

    }

    public function friends()
    {
        return $this->friendsOfMine()->wherePivot('accepted', true)->get()->
            merge($this->friendOf()->wherePivot('accepted', true)->get());
    }

    public function friendRequests()
    {
        return $this->friendsOfMine()->wherePivot('accepted', false)->get();
    }    


    public function friendRequestsPending()
    {
        return $this->friendOf()->wherePivot('accepted', false)->get();
    }

    public function hasFriendRequestsPending(User  $user)
    {
        return (bool) $this->friendRequestsPending()->
        where('id', $user->id)->count();

    }

    public function hasFriendRequestsReceived(User  $user)
    {
        return (bool) $this->friendRequests()->where('id', $user->id)->count();
    }

    public function addFriend(User  $user)
    {
      $this->friendOf()->attach($user->id);
    }

    public function deleteFriend(User $user)
    {
        $this->friendOf()->detach($user->id);
        $this->friendsOfMine()->detach($user->id);  
    }

    public function acceptFriendRequest(User  $user)
    {
        $this->friendRequests()->where('id', $user->id)->first()
        ->pivot->update(['accepted'=> true, ]);
    }

    public function isFriendWith(User $user)
    {
        return (bool) $this->friends()->where('id', $user->id)->count();
    }
    public function hasLikedStatus(Status $status)
    {
        return (bool) $status->likes
        ->where('user_id', $this->id)->count();

    }
}

And my HomeController:

<?php
    namespace Activentum\Http\Controllers;

    use Auth;
    use Activentum\Models\Status;

    class HomeController extends  Controller 
     {

    public function index()
    {
        if (Auth::check()) {
            $statuses = Status::notReply()->where(function($query){
                return $query->where ('user_id', Auth::user()->id)
                ->orWhereIn('user_id',Auth::user()->friends()->pluck('id')
                    );
            })
            ->orderBy('created_at','desc')->
            paginate(14);

            return view ('timeline.index')
                ->with('statuses', $statuses);
        }

        return view('home');
    }

}

It seems that project can't see the model. My models are in the Models folder in app/Models.


Solution

  • You need to update your config/auth.php file to look at the correct User model. A fresh install will look at the default \App\User model located at app/User.php. You need to update the model key in your auth config to look at your custom \App\Models\User model located at app/Models/User.php.