【问题标题】:upgrading to laravel 5.3, "Call to undefined method Illuminate\Database\Query\Builder" error [duplicate]升级到 laravel 5.3,“调用未定义的方法 Illuminate\Database\Query\Builder”错误 [重复]
【发布时间】:2017-04-07 02:10:14
【问题描述】:

我有一个运行良好的 Laravel 5.2 项目。我尝试将其升级到 Laravel 5.3,但它给出了以下错误:

Builder.php 第 2448 行中的 BadMethodCallException:调用未定义的方法 Illuminate\Database\Query\Builder::friends()

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

Builder.php 第 2448 行中的 BadMethodCallException:调用未定义的方法 Illuminate\Database\Query\Builder::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 的 Models 文件夹中。

【问题讨论】:

    标签: php laravel laravel-5 eloquent laravel-5.3


    【解决方案1】:

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

    【讨论】:

    • 我更新了 config/auth.php config/services.php/database/factories/ModelFactory.php 并将 Activentum\User 更改为 Activeum\Models\User,现在可以使用了。
    猜你喜欢
    • 2017-11-23
    • 2017-05-06
    • 2017-02-14
    • 2017-03-31
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    相关资源
    最近更新 更多