ErrorException参数1传递给(Laravel 5.2)

时间:2016-01-16 23:27:54

标签: php laravel relationships laravel-5.2

所以我试图"喜欢"状态,当我这样做时,我得到这个错误的回报

ErrorException in User.php line 107:
Argument 1 passed to SCM\User::hasLikedStatus() must be an instance of Status, instance of SCM\Status given, called in C:\xampp\htdocs\app\Http\Controllers\StatusController.php on line 66 and defined

当我删除"使用状态;"从我的User.php函数工作,它使用类似的ID更新我的数据库。这可能是因为我链接了我的状态公共功能,如" SCM \ Status"?

routes.php文件

<?php

/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/


/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/

Route::group(['middleware' => ['web']], function () {
    Route::get('/login', function () {
    return view('auth/login');
});
    Route::get('/register', function () {
    return view('auth/login');
});
    /**
    *User Profile
    */

    Route::get('/user/{username}', [
        'as' => 'profile.index', 'uses' => 'ProfileController@getProfile'
]);
    Route::get('/profile/edit', [
        'uses' => 'ProfileController@getEdit', 'as' => 'profile.edit', 'middleware' => ['auth'],
]);

    Route::post('/profile/edit', [
        'uses' => 'ProfileController@postEdit', 'middleware' => ['auth'],
]);

    Route::get('/settings', [
        'uses' => 'ProfileController@getEdit', 'as' => 'layouts.-settings', 'middleware' => ['auth'],
]);

    Route::post('/settings', [
        'uses' => 'ProfileController@postEdit', 'middleware' => ['auth'],
]);
    /**
    * Friends
    */

    Route::get('/friends', [
        'uses' => 'FriendController@getIndex', 'as' => 'friend.index', 'middleware' => ['auth'],
]);

    Route::get('/friends/add/{username}', [
        'uses' => 'FriendController@getAdd', 'as' => 'friend.add', 'middleware' => ['auth'],
]);
    Route::get('/friends/accept/{username}', [
        'uses' => 'FriendController@getAccept', 'as' => 'friend.accept', 'middleware' => ['auth'],
]);
    /**
    * Statuses
    */

Route::post('/status', [
    'uses' => 'StatusController@postStatus', 'as' => 'status.post', 'middleware' => ['auth'],

]);

Route::post('/status/{statusId}/reply', [
    'uses' => 'StatusController@postReply', 'as' => 'status.reply', 'middleware' => ['auth'],

]);

Route::get('/status/{statusId}/like', [
    'uses' => 'StatusController@getLike', 'as' => 'status.like', 'middleware' => ['auth'],

]);
});

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::get('/', [
    'as' => 'welcome', 'uses' => 'WelcomeController@index'
]);

    Route::get('/profile', function () {
    return view('layouts/-profile');
});

    Route::get('profile/{username}', function () {
    return view('layouts/-profile');
});




    Route::get('/home', 'HomeController@index');
});

/**
* Search
*/
Route::get('/search', [
    'as' => 'search.results', 'uses' => 'SearchController@getResults'
]);

User.php(型号)

<?php

namespace SCM;

use Status;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'email', 'password',
    ];

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

    protected $primaryKey = 'id';

    public function getAvatarUrl()
    {
        return "http://www.gravatar.com/avatar/{{ md5 ($this->email)}}?d=mm&s=40 ";
    }

    public function statuses()
    {
        return $this->hasMany('SCM\Status', 'user_id');
    }

    public function likes()
    {
        return $this->hasMany('SCM\Like', 'user_id');
    }

    public function friendsOfMine()
    {
        return $this->belongsToMany('SCM\User', 'friends', 'user_id', 'friend_id');
    }

    public function friendOf()
    {
        return $this->belongsToMany('SCM\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 hasFriendRequestPending(User $user)
    {
        return (bool) $this->friendRequestsPending()->where('id', $user->id)->count();

    }

    public function hasFriendRequestReceived(User $user)
    {
        return (bool) $this->friendRequests()->where('id', $user->id)->count();

    }

    public function addFriend(User $user)
    {
        $this->friendOf()->attach($user->id);

    }

    public function acceptFriendRequest(User $user)
    {
        $this->friendRequests()->where('id', $user->id)->first()->pivot->update([

            'accepted' => true,

        ]);

    }

    public function isFriendsWith(User $user)

    {
        return (bool) $this->friends()->where('id', $user->id)->count();
    }

    public function hasLikedStatus(Status $status)
    {
        return (bool) $status->likes
        ->where('likeable_id', $status->id)
        ->where('likeable_type', get_class($status))
        ->where('user_id', $this->id)
        ->count();
    }
}

Status.php(模型)

<?php

namespace SCM;

use Illuminate\Database\Eloquent\Model;

class Status extends Model
{
    protected $table = 'statuses';

    protected $fillable = [
        'body'
    ];

    public function user()
    {
        return $this->belongsTo('SCM\User', 'user_id');
    }

    public function scopeNotReply($query)
    {
        return $query->whereNull('parent_id');
    }

    public function replies()
    {
        return $this->hasMany('SCM\Status', 'parent_id');
    }

    public function likes()
    {
        return $this->morphMany('SCM\Like', 'likeable');
    }
}

Likes.php(模特)

<?php

namespace SCM;

use Illuminate\Database\Eloquent\Model;

class Like extends Model
{
    protected $table = 'likeable';

    public function likeable()
    {
        return $this->morphTo();
    }

    public function user ()
    {
        return $this->belongsTo('SCM\User', 'user_id');
    }
}

StatusController.php

<?php

namespace SCM\Http\Controllers;

use Flash;
use Auth;
use Illuminate\Http\Request;
use SCM\User;
use SCM\Status;

class StatusController extends Controller
{
    public function postStatus(Request $request)
    {
        $this->validate($request, [
            'status' => 'required|max:1000',
        ]);

        Auth::user()->statuses()->create([
            'body' => $request->input('status'),
        ]);

        return redirect()->route('welcome')->with('info', 'Status posted.');
    }

    public function postReply(Request $request, $statusId)
    {
        $this->validate($request, [
                "reply-{$statusId}" => 'required|max:1000',
        ], [
            'required' => 'The reply body is required.'
        ]);

        $status = Status::notReply()->find($statusId);

        if (!$status) {
            return redirect()->route('welcome');
        }

        if (!Auth::user()->isFriendsWith($status->user) && Auth::user()->id !==
            $status->user->id) {
            return redirect()->route('welcome');

        }

        $reply = Status::create([
            'body' => $request->input("reply-{$statusId}"),
        ])->user()->associate(Auth::user());

        $status->replies()->save($reply);

        return redirect()->back();
    }

    public function getLike($statusId)
    {
        $status = Status::find($statusId);

        if (!$status) {
            return redirect()->route('welcome');
        }

        if (!Auth::user()->isFriendsWith($status->user)) {
            return redirect()->route('welcome');
        }

        if (Auth::user()->hasLikedStatus($status)) {
            return redirect()->back();
        }

        $like = $status->likes()->create([]);
        Auth::user()->likes()->save($like);

        return redirect()->back();
    }
}

1 个答案:

答案 0 :(得分:1)

删除useStatus的{​​{1}}语句。当您这样做时,您实际上是在尝试使用Users.php。您的文件已在命名空间\Status中,因此您不需要任何use语句来使用同一命名空间中的类。

因此,在您的方法定义中,您说要将SCM的实例作为参数,但传递的是\Status