php - ErrorException Argument 1 passed to (Laravel 5.2) -
so i'm trying "like" status , when do, error in return
errorexception in user.php line 107: argument 1 passed scm\user::haslikedstatus() must instance of status, instance of scm\status given, called in c:\xampp\htdocs\app\http\controllers\statuscontroller.php on line 66 , defined when remove "use status;" user.php function works , updated database id. because linked status's public function "scm\status"?
routes.php
<?php /* |-------------------------------------------------------------------------- | routes file |-------------------------------------------------------------------------- | | here register of routes in application. | it's breeze. tell laravel uris should respond | , give controller call when uri requested. | */ /* |-------------------------------------------------------------------------- | application routes |-------------------------------------------------------------------------- | | route group applies "web" middleware group every route | contains. "web" middleware group defined in http | kernel , includes session state, csrf protection, , 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 (model)
<?php namespace scm; use status; use illuminate\foundation\auth\user authenticatable; class user extends authenticatable { /** * attributes mass assignable. * * @var array */ protected $fillable = [ 'username', 'email', 'password', ]; /** * attributes excluded 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 (model)
<?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 (model)
<?php namespace scm; use illuminate\database\eloquent\model; class 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 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(); } }
remove use statement status in users.php. when you're doing that, you're trying use \status. file in namespace scm, don't need use statement use classes in same namespace.
so in method definition you're saying want instance of \status parameter, passing in scm\status.
Comments
Post a Comment