前提・実現したいこと
現在、Laravel5.4.23にて、共通レイアウト用のBladeファイルと、そのリンク先の個々のユーザー専用コンテンツ設定ページを作っています。
Bladeファイルのリンクを設定する際に、以下のエラーが発生しました。エラーなく設定変更できるようにするにはどうしたら良いのでしょうか?
尚、共通レイアウト用Bladeファイルの 56 ~ 57 行目の<a>アンカータグの中身を/cannel等に変更した場合は、特にエラーは表示されておりません。ソースコードの完全版もアップロード可能です、お申し付けください。
発生している問題・エラーメッセージ
(3/3) ErrorException
Trying to get property of non-object (View: /Users/thbc002/Downloads/Code/8282_Code/takatube/takatube/resources/views/layouts/app.blade.php) (View: /Users/thbc002/Downloads/Code/8282_Code/takatube/takatube/resources/views/layouts/app.blade.php)
(2/3) ErrorException
Trying to get property of non-object (View: /Users/thbc002/Downloads/Code/8282_Code/takatube/takatube/resources/views/layouts/app.blade.php)
(1/3) ErrorException
Trying to get property of non-object
#####下記は共通レイアウト用の/resources/views/layouts/app.blade.php
php
1 2<!DOCTYPE html> 3<html lang="{{ app()->getLocale() }}"> 4<head> 5 <meta charset="utf-8"> 6 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 7 <meta name="viewport" content="width=device-width, initial-scale=1"> 8 9 <!-- CSRF Token --> 10 <meta name="csrf-token" content="{{ csrf_token() }}"> 11 12 <title>{{ config('app.name', 'Laravel') }}</title> 13 14 <!-- Styles --> 15 <link href="{{ asset('css/app.css') }}" rel="stylesheet"> 16</head> 17<body> 18 <div id="app"> 19 <nav class="navbar navbar-default navbar-static-top"> 20 <div class="container"> 21 <div class="navbar-header"> 22 23 <!-- Collapsed Hamburger --> 24 <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse"> 25 <span class="sr-only">Toggle Navigation</span> 26 <span class="icon-bar"></span> 27 <span class="icon-bar"></span> 28 <span class="icon-bar"></span> 29 </button> 30 31 <!-- Branding Image --> 32 <a class="navbar-brand" href="{{ url('/') }}"> 33 {{ config('app.name', 'Laravel') }} 34 </a> 35 </div> 36 37 <div class="collapse navbar-collapse" id="app-navbar-collapse"> 38 <!-- Left Side Of Navbar --> 39 <ul class="nav navbar-nav"> 40 41 </ul> 42 43 <!-- Right Side Of Navbar --> 44 <ul class="nav navbar-nav navbar-right"> 45 <!-- Authentication Links --> 46 @if (Auth::guest()) 47 <li><a href="{{ route('login') }}">ログイン</a></li> 48 <li><a href="{{ route('register') }}">新規登録</a></li> 49 @else 50 <li class="dropdown"> 51 <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> 52 {{ Auth::user()->name }} <span class="caret"></span> 53 </a> 54 55 <ul class="dropdown-menu" role="menu"> 56 <li><a href="/account">アカウント設定</a></li> 57 <li><a href="/channel/{{ Auth::user()->channels()->first()->slug }}">私のチャンネル</a></li> 58 <li><a href="/channel/{{ Auth::user()->channels()->first()->slug }}/settings">チャンネル設定</a></li> 59 <li> 60 <a href="{{ route('logout') }}" 61 onclick="event.preventDefault(); 62 document.getElementById('logout-form').submit();"> 63 ログアウト 64 </a> 65 66 <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;"> 67 {{ csrf_field() }} 68 </form> 69 </li> 70 </ul> 71 </li> 72 @endif 73 </ul> 74 </div> 75 </div> 76 </nav> 77 78 @yield('content') 79 </div> 80 81 <!-- Scripts --> 82 <script src="{{ asset('js/app.js') }}"></script> 83</body> 84</html>
試したこと
php artisan make:auth
DB作成し、マイグレーションファイルの生成と修正をした後に、model と controller の作成、マイグレーションを実行しました。
#####、/database/migrations/2014_10_12_000000_create_users_table.php
php
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateUsersTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('users', function (Blueprint $table) { 17 $table->increments('id'); 18 $table->string('name'); 19 $table->string('email')->unique(); 20 $table->string('password'); 21 $table->rememberToken(); 22 $table->timestamps(); 23 }); 24 } 25 26 /** 27 * Reverse the migrations. 28 * 29 * @return void 30 */ 31 public function down() 32 { 33 Schema::dropIfExists('users'); 34 } 35} 36
#####生成したマイグレーションファイル/database/migrations/2018_04_05_021909_create_channels_table.php は下記のように修正
php
1<?php 2 3use Illuminate\Support\Facades\Schema; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Database\Migrations\Migration; 6 7class CreateChannelsTable extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('channels', function (Blueprint $table) { 17 $table->increments('id'); 18 $table->integer('user_id')->unsigned()->index(); 19 $table->string('name'); 20 $table->string('slug'); 21 $table->text('description')->nullable(); 22 $table->string('cover')->nullable(); 23 $table->string('avatar')->nullable(); 24 $table->timestamps(); 25 26 $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 27 28 }); 29 } 30 31 /** 32 * Reverse the migrations. 33 * 34 * @return void 35 */ 36 public function down() 37 { 38 Schema::dropIfExists('channels'); 39 } 40} 41
######/app/User.php に下記の内容を追加
php
1public function channels(){ 2 return $this->hasMany(Channel::class); 3 }
#####/app/User.phpUser.php
php
1 2<?php 3 4namespace App; 5 6use Illuminate\Notifications\Notifiable; 7use Illuminate\Foundation\Auth\User as Authenticatable; 8 9class User extends Authenticatable 10{ 11 use Notifiable; 12 13 /** 14 * The attributes that are mass assignable. 15 * 16 * @var array 17 */ 18 protected $fillable = [ 19 'name', 'email', 'password', 20 ]; 21 22 /** 23 * The attributes that should be hidden for arrays. 24 * 25 * @var array 26 */ 27 protected $hidden = [ 28 'password', 'remember_token', 29 ]; 30 31 public function channels(){ 32 return $this->hasMany(Channel::class); 33 } 34} 35
#####/app/Channel.php
php
1<?php 2 3namespace App; 4 5 6use Laravel\Scout\Searchable; 7use Illuminate\Database\Eloquent\Model; 8 9class Channel extends Model 10{ 11 12 use Searchable; 13 14 protected $fillable = [ 15 'name', 16 'slug', 17 'description', 18 'cover', 19 'avatar', 20 ]; 21 22 public function user(){ 23 return $this->belongsTo(User::class); 24 } 25 26 public function getRouteKeyName(){ 27 return 'slug'; 28 } 29 30}
/app/Http/Controllers/ChannelController.php
php
1<?php 2 3namespace App\Http\Controllers; 4 5use App\Jobs\UploadChannelCoverImage; 6use App\Jobs\UploadProfileImage; 7 8use App\Channel; 9use Illuminate\Http\Request; 10 11class ChannelController extends Controller 12{ 13 14 public function show(Channel $channel){ 15 return view('channel.show', [ 16 'channel' => $channel 17 ]); 18 } 19 20 public function edit(Channel $channel){ 21 22 $this->authorize('edit', $channel); 23 24 return view('channel.settings', [ 25 'channel' => $channel 26 ]); 27 } 28 29 public function update(Request $request, Channel $channel){ 30 31 $this->authorize('update', $channel); 32 33 $this->validate($request, [ 34 'name' => 'required|max:255|unique:channels,name,' . $channel->id, 35 'slug' => 'required|max:255|alpha_num|unique:channels,slug,' . $channel->id 36 ]); 37 38 if($request->file('cover')){ 39 $request->file('cover')->move(storage_path() . "/uploads", $fileId = uniqid(true)); 40 $this->dispatch(new UploadChannelCoverImage($channel, $fileId)); 41 } 42 43 if($request->file('avatar')){ 44 $request->file('avatar')->move(storage_path() . "/uploads", $fileId = uniqid(true)); 45 $this->dispatch(new UploadProfileImage($channel, $fileId)); 46 } 47 48 $channel->update([ 49 'name' => $request->name, 50 'slug' => $request->slug, 51 'description' => $request->description 52 ]); 53 54 return redirect()->to("/channel/{$channel->slug}/settings"); 55 56 } 57}
#####/routes/web.php
<?php Auth::routes(); Route::group(['middleware' => ['auth']], function(){ Route::get('/account', 'AccountController@show'); Route::post('/account', 'AccountController@update'); Route::get('/channel/{channel}/settings', 'ChannelController@edit'); Route::post('/channel/{channel}/settings', 'ChannelController@update'); });
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/14 09:26 編集
2018/04/14 12:26 編集