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

@James,您的贡献非常有效。我很感激。正是您指出的有关表字段命名约定的地方。Laravel 通过将id 附加到表名来映射关系表以创建外键。我的错误是省略了“ ”。如果您有与此不同的命名约定,则必须在创建模型时在函数中指定。一个例子:

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

在上面定义关系的函数中,如果我在表名和 id 之间附加“_”,我可以保留它,如下所示:user_id。使用这个 Laravel 标准,您不需要在函数中添加第二个属性。

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