2016-07-14 10 views
-1

我正在關注laracasts視頻以創建關注選項,但點擊用戶名時顯示上述錯誤,我不知道在哪裏定義此變量。 Followscontroller未定義的變量:currentUser

<?php 

namespace App\Http\Controllers; 
use Redirect; 
use App\User; 
use Laracasts\Commander\CommanderTrait; 
use App\FollowUserCommand; 
use Sentinel; 
use Illuminate\Support\Facades\Input; 
use App\Http\Requests; 
use App\Http\Controllers\Controller; 


class FollowsController extends Controller 
{ 
use CommanderTrait; 
/** 
* Follow a User 
* 
* @param \Illuminate\Http\Request $request 
* @return \Illuminate\Http\Response 
*/ 
public function store() 
{ 
    $input = array_add(Input::all(), 'user_id', Sentinel::getuser()->id); 
    $this->execute(FollowUserCommand::class, $input); 
    return Redirect::back(); 
} 


/** 
* Unfollow a User 
* 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function destroy($id) 
{ 
    // 
} 
} 

FollowUserCommand

<?php namespace App; 
use App\User; 

class FollowUserCommand { 

public $user_id; 
public $userIdToFollow; 

function __construct($user_id, $userIdToFollow) 
{ 
    $this->user_id = $user_id; 
    $this->userIdToFollow = $userIdToFollow; 
} 
} 

FollowUserCommandHandler

<?php namespace App; 

use Laracasts\Commander\CommandHandler; 

class FollowUserCommandHandler implements CommandHandler { 

protected $userRepo; 

function __construct(UserRepository $userRepo) 
{ 
    $this->userRepo = $userRepo; 
} 




public function handle($command) 
{ 
    $user = $this->userRepo->findById($command->user_id); 

    $this->userRepo->follow($command->userIdToFollow, $user); 

    return $user; 
} 

} 

UserRepository

<?php namespace App; 
use App\User; 

class UserRepository { 

public function save(User $user) 
{ 
    return $user->save(); 
} 

public function getPaginated($howMany = 4) 
{ 
    return User::orderBy('first_name', 'asc')->paginate($howMany); 
} 

public function findByUsername($username) 
{ 
    return User::with(['feeds' => function($query) 
    { 
     $query->latest(); 
    } 

    ])->whereUsername($username)->first(); 
} 

public function findById($id) 
{ 
    return User::findOrFail($id); 
} 

public function follow($userIdToFollow, User $user) 
{ 
    return $user->follows()->attach($userIdToFollow); 
} 
} 

user.php的

<?php namespace App; 
use Cartalyst\Sentinel\Users\EloquentUser; 
use Illuminate\Database\Eloquent\SoftDeletes; 


class User extends EloquentUser { 


/** 
* The database table used by the model. 
* 
* @var string 
*/ 
protected $table = 'users'; 

/** 
* The attributes to be fillable from the model. 
* 
* A dirty hack to allow fields to be fillable by calling empty fillable array 
* 
* @var array 
*/ 
protected $fillable = []; 
protected $guarded = ['id']; 

/** 
* The attributes excluded from the model's JSON form. 
* 
* @var array 
*/ 
protected $hidden = ['password', 'remember_token']; 

/** 
* To allow soft deletes 
*/ 
use SoftDeletes; 

protected $dates = ['deleted_at']; 



public function feeds() 
{ 
    return $this->hasMany('App\Feed'); 
} 

public function comment() 
{ 
    return $this->hasMany('App\Comment'); 
} 


    // This function allows us to get a list of users following us 
public function follows() 
{ 
    return $this->belongsToMany(self::class, 'follows', 'follower_id', 'followed_id')->withTimestamps(); 
} 

// Get all users we are following 
public function following() 
{ 
    return $this->belongsToMany('User', 'followers', 'user_id', 'follow_id')->withTimestamps(); 
} 

// if current user follows another user 
public function isFollowedBy(User $otherUser) 
{ 
    $idsWhoOtherUserFollows = $otherUser->follows()->lists('followed_id'); 

    return in_array($this->id, $idsWhoOtherUserFollows) ; 
} 
} 

form.blade.php

@if($user->isFollowedBy($currentUser)) 

<p>You are following {{ $user->username }}<p> 

@else 
{!! Form::open(['route' => 'follows_path']) !!} 
{!! Form::hidden('userIdToFollow', $user->id) !!} 
<button type="submit" class="btn btn-primary">Follow {{ $user->username }} </button> 
{!! Form::close() !!} 
@endif 
+0

您使用的是什麼版本的laravel?你應該堅持一個標籤。此外,請讓我知道,如果你已經嘗試了我的解決方案 – Jonathan

+0

否使用你的解決方案後我得到這個錯誤參數1傳遞給應用程序\用戶:: isFollowedBy()必須是App \ User的實例,在第一行和已定義的C:\ wamp \ www \ newjosh \ storage \ framework \ views \ 186e95fed899bd7661ff5cbd0b490365並定義(視圖:C:\ wamp \ www \ newjosh \ resources \ views \ member \ partials \ follow-form.blade.php) (View:C:\ wamp \ www \ newjosh \ resources \ views \ member \ partials \ follow-form.blade.php) –

+0

我正在使用laravel 5.1 –

回答

1

假設教程實現的驗證類,可以通過改變@if($user->isFollowedBy($currentUser))來獲得當前用戶@if($user->isFollowedBy(\Illuminate\Support\Facades\Auth::user()))。否則很難閱讀你的代碼,但對你想要徹底的讚賞。

你顯然不想用這種方式使用Auth::user()。嘗試使用它作爲Auth::user()沒有完整的名稱空間,但否則在處理該視圖的控制器中添加命名空間爲use Illuminate\Support\Facades\Auth;

+0

使用您的解決方案後沒有我收到此錯誤參數1通過到App \ User :: isFollowedBy()必須是App \ User的一個實例,給定爲null,在第1行調用並在C:\ wamp \ www \ newjosh \ storage \ framework \ views \ 186e95fed899bd7661ff5cbd0b490365中定義(View:C:\ wamp \ www \ newjosh \ resources \ views \ member \ partials \ follow-form.blade.php)(查看:C:\ wamp \ www \ newjosh \ resources \ views \ member \ partials \ follow-form.blade.php) –

+0

我正在使用laravel 5.1 –