【问题标题】:How can i solve this error using "Laravel Follow"如何使用“Laravel Follow”解决此错误
【发布时间】:2021-12-11 19:59:29
【问题描述】:

所以我正在尝试为我的 imageshare 制作一个类似的系统。 为此,我尝试使用 overtrue 中的“Laravel Follow”,但这给我带来了问题。

每次我尝试使用他在 GitHub 页面中所说的功能时,它总是给我“调用未定义的方法 App\Models\Photo::needsToApproveFollowRequests()”。

这是我的 User.php

    <?php
    
    namespace App\Models;
    
    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Contracts\Auth\CanResetPassword;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Notifications\Notifiable;
    use Laravel\Sanctum\HasApiTokens;
    use Overtrue\LaravelFollow\Followable;
    
    
    
    
    class User extends Authenticatable implements MustVerifyEmail
    {
        use HasApiTokens, HasFactory, Notifiable, Followable;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var string[]
         */
        protected $fillable = [
            'firstName',
            'lastName',
            'name',
            'email',
            'password',
            
        ];
    
        /**
         * The attributes that should be hidden for serialization.
         *
         * @var array
         */
        protected $hidden = [
            'password',
            'remember_token',
        ];
    
        /**
         * The attributes that should be cast.
         *
         * @var array
         */
        protected $casts = [
            'email_verified_at' => 'datetime',
        ];
}

这是我尝试使用的控制器中的功能:

public function getSnatch($id) {
    //Let's try to find the image from database first
    $image = Photo::find($id);

    if(!$image) {
      abort(404);
    }

    $imageThumb = Photo::find($id)->paginate(1);
    $user = User::find($image->user);

    $currentUser = User::find(auth()->user()->id);
    

    // $user = User::where('id', $userID)->first();
    $lastId = Photo::where('id', '<', $image->id)->max('id');
    $nextId = Photo::where('id', '>', $image->id)->min('id');
    
    // $nextPageNumber = $image->id + 1;
    
    $maxId = Photo::find($id)->max('id');
    $minId = Photo::find($id)->min('id');
    
    // $imageCount = count(DB::table('photos')->get());

    // ddd($nextId);



    $likeImage = $currentUser->toggleFollow($image);

    $totalLikes = $image->followers();
    

    if ($lastId < $minId) {
      $lastId = $maxId;
    }
    
    if ($nextId === NULL) {
      $nextId = $minId;
    }
    
    
    //If found, we load the view and pass the image info asparameter, else we redirect to main page with errormessage
    if($image) {
      return View::make('tpl.permalink')
      ->with('image', $image)
      ->with('lastId', $lastId)
      ->with('nextId', $nextId)
      ->with('user', $user)
      ->with('imageThumb', $imageThumb)
      ->with('currentUser', $currentUser)
      ->with('likeImage', $likeImage)
      ->with('totalLikes', $totalLikes);
    } else {
      return Redirect::to('/')->with('error','Image not found');
    }
}

composer 中的所有内容都安装得很好。 我也试过删除vendor文件夹,清除composer的缓存,重新安装composer,还是不行。

【问题讨论】:

    标签: laravel


    【解决方案1】:

    您必须覆盖模型中使用 Followable 特征的方法。

    public function needsToApproveFollowRequests()
    {
        // Your custom logic here
        return (bool) $this->private;
    }
    

    【讨论】:

    • 仍然给出同样的错误:(
    • @Jonypaul 你覆盖了用户模型或照片模型的方法?因为出现错误的是照片模型。
    • 现在我做了正确的,它不再给这个问题了。仍然不起作用,但这是因为这段代码不适合我的项目。 :(谢谢你的帮助!
    • @Jonypaul 如果有帮助,请考虑将我的答案标记为正确:)。
    • 对不起,我完全忘记了这样做!
    猜你喜欢
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2017-12-12
    • 2012-02-14
    • 2015-04-12
    • 2016-06-21
    • 2020-11-16
    相关资源
    最近更新 更多