php - 对整数调用成员函数 follow()

标签 php laravel

当一个用户关注另一个用户时,我试图将用户保存到关注者表中。当我尝试让一个用户关注另一个用户时,我得到

Call to a member function follow() on integer

每当我尝试关注其他用户时。

关注按钮/表单

{!! Form::open(['route' => 'follow_user']) !!}

  {!! Form::hidden('id', $user->id) !!}

  <button type="submit" class="btn btn-primary">Follow {{$user->name}}</button>

{!! Form::close() !!}

路线

Route::post('/follow', [
    'as' => 'follow_user', 'uses' => 'FollowersController@store'
]);

关注者 Controller

public function store()
{
    $user1 = Auth::user()->id;        
    $user2 = Input::get('id');
    $user1->follow($user2);

    return redirect()->action('HomeController@index');
}

我在用户模型中使用的方法

function followers()
{
    return $this->belongsToMany('App\User', 'followers', 'user_id', 'follower_id');
}

function follow(User $user) {
    $this->followers()->attach($user->id);
}

function unfollow(User $user) {
    $this->followers()->detach($user->id);
}

最佳答案

您尝试在 ID 上运行 follow(),而不是 User 对象(如您可能想要的那样)。

这将返回一个整数:

$user1 = Auth::user()->id;

也许你想要这样的东西:

$user1 = Auth::user();        
$user2 = Input::get('id');
$user1->follow(User::find($user2));

感谢@blackpla9ue 的修复。

关于php - 对整数调用成员函数 follow(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36889606/

相关文章:

php - 如何在html主体中使用select选项值作为php sql语句同一页面中的变量

php - 通过 AJAX 调用任何 Wordpress PHP 函数

php - Windows 中使用 Laravel5 的调度程序任务

php - 在 Laravel 中基于外键搜索

php - 拉维尔 5.1 : How to limit account access so one account can be accessed at one time

php - Plupload Onload 显示上传文件列表

php - 匹配以 pics.domain.com 开头的所有图像

php - 无法使这个准备好的选择语句起作用

php - 如何在 Laravel 中记录用户操作?如何监听 Controller 方法?

javascript - Laravel + AngularJS 应用程序结构