0

このフォーラムからのサポートに感謝します。私がフォローしているチュートリアルはユーザー/質問ベースであるため、ユーザーと質問の関係を使用してユーザー名を取得したいと考えています。私のモデルでは、関係は次のように定義されています: 質問モデルでは

use App\User;

class Question extends Model
{

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

ユーザーモデルでは:

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

QuestionController では、次のように定義しています。

public function show($id = null)
    {
        $question = Question::find($id);

        return view('questions.view')
            ->with('title', 'Make it Snappy - Question')
            ->with('question', $question);
    }

...そしてview.blade.phpで次のように定義しました:

@section('content')
    <h1>
        {!! ucfirst($question->user->username) !!}
    </h1>
    <p> {!! e($question->questions) !!} </p>
@stop

ページを実行すると、タイトルが示すようにエラーがスローされます。

2/2
ErrorException in d97e482321a7dfdc3ee708c22c0fb08e line 3:
Trying to get property of non-object (View: C:\Users\ken4ward\Documents\xampp\htdocs\rphlimitedweb\resources\views\questions\view.blade.php)
in d97e482321a7dfdc3ee708c22c0fb08e line 3
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44
at PhpEngine->evaluatePath('C:\Users\ken4ward\Documents\xampp\htdocs\rphlimitedweb\storage\framework\views/d97e482321a7dfdc3ee708c22c0fb08e', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'title' => null, 'question' => object(Question))) in CompilerEngine.php line 58
at CompilerEngine->get('C:\Users\ken4ward\Documents\xampp\htdocs\rphlimitedweb\resources\views/questions/view.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'title' => null, 'question' => object(Question))) in View.php line 135
at View->getContents() in View.php line 106
at View->renderContents() in View.php line 80
at View->render() in Response.php line 51
at Response->setContent(object(View)) in Response.php line 198
at Response->__construct(object(View)) in Router.php line 1229
at Router->prepareResponse(object(Request), object(View)) in ControllerDispatcher.php line 113
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack(object(QuestionController), object(Route), object(Request), 'show') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\QuestionController', 'show') in Route.php line 203
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 708
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 710
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 675
at Router->dispatchToRoute(object(Request)) in Router.php line 635
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54
at require_once('C:\Users\ken4ward\Documents\xampp\htdocs\rphlimitedweb\public\index.php') in server.php line 21
1/2
ErrorException in d97e482321a7dfdc3ee708c22c0fb08e line 3:
Trying to get property of non-object
in d97e482321a7dfdc3ee708c22c0fb08e line 3
at HandleExceptions->handleError('8', 'Trying to get property of non-object', 'C:\Users\ken4ward\Documents\xampp\htdocs\rphlimitedweb\storage\framework\views\d97e482321a7dfdc3ee708c22c0fb08e', '3', array('__path' => 'C:\Users\ken4ward\Documents\xampp\htdocs\rphlimitedweb\storage\framework\views/d97e482321a7dfdc3ee708c22c0fb08e', '__data' => array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'title' => null, 'question' => object(Question)), 'obLevel' => '1', '__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'title' => null, 'question' => object(Question))) in d97e482321a7dfdc3ee708c22c0fb08e line 3
at include('C:\Users\ken4ward\Documents\xampp\htdocs\rphlimitedweb\storage\framework\views\d97e482321a7dfdc3ee708c22c0fb08e') in PhpEngine.php line 42
at PhpEngine->evaluatePath('C:\Users\ken4ward\Documents\xampp\htdocs\rphlimitedweb\storage\framework\views/d97e482321a7dfdc3ee708c22c0fb08e', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'title' => null, 'question' => object(Question))) in CompilerEngine.php line 58
at CompilerEngine->get('C:\Users\ken4ward\Documents\xampp\htdocs\rphlimitedweb\resources\views/questions/view.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'title' => null, 'question' => object(Question))) in View.php line 135
at View->getContents() in View.php line 106
at View->renderContents() in View.php line 80
at View->render() in Response.php line 51
at Response->setContent(object(View)) in Response.php line 198
at Response->__construct(object(View)) in Router.php line 1229
at Router->prepareResponse(object(Request), object(View)) in ControllerDispatcher.php line 113
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack(object(QuestionController), object(Route), object(Request), 'show') in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\QuestionController', 'show') in Route.php line 203
at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
at Route->run(object(Request)) in Router.php line 708
at Router->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Router.php line 710
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 675
at Router->dispatchToRoute(object(Request)) in Router.php line 635
at Router->dispatch(object(Request)) in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54
at require_once('C:\Users\ken4ward\Documents\xampp\htdocs\rphlimitedweb\public\index.php') in server.php line 21
4

2 に答える 2

1

取得しているエラーは、コレクションuser()内の関数の呼び出し方法に関連しています。$question

ドキュメントはこれについてしっかりと理解するのに適した場所ですが、基本的にすべてのEloquent関係は関数を介して定義されるため、実際に関係クエリを実行せずにそれらの関数を呼び出して関係のインスタンスを取得できます。

そのため、次のようにする必要があります。

{!! ucfirst($question->user()->username) !!}

user()ではなく、現在連鎖しているものはありませんuser

于 2015-12-08T06:37:45.013 に答える
0

@ジェームズ、あなたの貢献は非常に有効です。感謝します。テーブルフィールドの命名規則について指摘した正確な場所。Laravel は、テーブル名に id を追加して外部キーを作成することにより、関係テーブルをマップします。私の間違いは「」の省略でした。これとは異なる命名規則がある場合は、モデルの作成時に関数で指定する必要があります。例:

public function questions(){
    return $this->hasMany('App\Question', 'userid');
}

関係が定義されている上記の関数では、テーブル名と id の間に「_」を追加して、user_id のようにすると、そのままにしておくことができます。この Laravel 標準では、関数に 2 番目のプロパティを追加する必要はありません。

public function questions(){
        return $this->hasMany('App\Question');
    }
于 2015-12-08T10:06:11.340 に答える