升级到5.3 laravel,“调用未定义的方法照亮\\ \\数据库查询\\生成器”的错误[复制](

2019-09-28 11:28发布

这个问题已经在这里有一个答案:

  • 更新到Laravel 5.3后播种时调用未定义的方法照亮\数据库\查询\生成器::列表() 5个回答

我有一个Laravel 5.2项目的伟大工程。 我试着将它升级到Laravel 5.3,它给了错误,例如:

BadMethodCallException在Builder.php线2448:调用未定义的方法照亮\数据库\查询\生成器::朋友()

或者在我的任何其他方法User.php模型具有相同的错误。 例如,当我评论friends()HomeController.php我有:

BadMethodCallException在Builder.php线2448:调用未定义的方法照亮\数据库\查询\生成器:: getNameOrUsername()

这是我的User模型:

<?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();

    }
}

而我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');
    }

}

看来,项目不能看到模型。 我的模型在模型文件夹中app/Models

Answer 1:

你需要更新你的config/auth.php文件来看看正确的User模型。 全新安装将着眼于默认\App\User位于模型app/User.php 。 您需要更新model中的身份验证配置关键要看您的自定义\App\Models\User模型位于app/Models/User.php



文章来源: upgrading to laravel 5.3, “Call to undefined method Illuminate\\Database\\Query\\Builder” error [duplicate]