https://teratail.com/questions/313328の続きです。
エラー全文
Trying to get property 'articles' of non-object
articlesなんていうプロパティ無いよ~っていうエラーだと思うのですが
Article.phpあるいはUser.phpなどのモデルが悪さをしているのかなと思いますが皆目分かりません。
どうかよろしくお願いします。
環境
Laravel:Laravel Framework 6.18.20
OS: Windows10
UserController.php
<?php namespace App\Http\Controllers; use App\User; use Illuminate\Http\Request; class UserController extends Controller { public function show(string $name) { $user = User::where('name', $name)->first() ->load(['articles.user', 'articles.likes', 'articles.tags']); $articles = $user->articles->sortByDesc('created_at'); // ここがnon-objectになってしまう return view('users.show', [ 'user' => $user, 'articles' => $articles, ]); } public function edit(string $name) { $user = User::where('name', $name)->first(); // UserPolicyのupdateメソッドでアクセス制限 $this->authorize('update', $user); return view('users.edit', ['user' => $user]); }
Article.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Article extends Model { protected $fillable = ['title', 'body']; public function user(): BelongsTo { return $this->belongsTo('App\User'); } public function likes(): BelongsToMany { return $this->belongsToMany('App\User', 'likes')->withTimestamps(); } public function isLikedBy(?User $user): bool { return $user ? (bool)$this->likes->where('id', $user->id)->count() : false; }
User.php
<?php namespace App; use App\Mail\BareMail; use App\Notifications\PasswordResetNotification; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', 'user_id', 'self_introduction', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; public function sendPasswordResetNotification($token) { $this->notify(new PasswordResetNotification($token, new BareMail())); } public function articles(): HasMany { return $this->hasMany('App\Article'); }
追記
LoginController.php
<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use App\User; use App\Providers\RouteServiceProvider; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; use Laravel\Socialite\Facades\Socialite; class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller handles authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | to conveniently provide its functionality to your applications. | */ use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = RouteServiceProvider::HOME; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } protected function authenticated(Request $request) { // フラッシュメッセージを表示 session()->flash('flash_message', 'ログインしました'); return redirect('/'); } public function redirectToProvider(string $provider) { return Socialite::driver($provider)->redirect(); } public function handleProviderCallback(Request $request, string $provider) // Googlelogin { $providerUser = Socialite::driver($provider)->stateless()->user(); $user = User::where('email', $providerUser->getEmail())->first(); if ($user) { $this->guard()->login($user, true); return $this->sendLoginResponse($request); } return redirect()->route('register.{provider}', [ 'provider' => $provider, 'email' => $providerUser->getEmail(), 'token' => $providerUser->token, ]); } }
ルーティング
<?php Auth::routes(); // グーグルログイン Route::prefix('login')->name('login.')->group(function () { Route::get('/{provider}', 'Auth\LoginController@redirectToProvider')->name('{provider}'); Route::get('/{provider}/callback', 'Auth\LoginController@handleProviderCallback')->name('{provider}.callback'); }); Route::get('/about','AboutContoroller@about')->name('articles.about'); Route::prefix('register')->name('register.')->group(function () { Route::get('/{provider}', 'Auth\RegisterController@showProviderUserRegistrationForm')->name('{provider}'); Route::post('/{provider}', 'Auth\RegisterController@registerProviderUser')->name('{provider}'); }); Route::middleware('auth')->group(function () { Route::get('/edit', 'UserController@edit')->name('edit'); // ユーザーのプロフィール編集 Route::put('/{name}/follow', 'UserController@follow')->name('follow'); Route::delete('/{name}/follow', 'UserController@unfollow')->name('unfollow'); }); }); Route::resource('/comments', 'CommentController')->only(['store']);
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/02 06:23
2021/01/02 06:24
2021/01/02 06:49
2021/01/02 06:51
2021/01/02 06:52
2021/01/02 07:47