Reputation: 2296
I appreciate the support from this forum. I want to retrieve the username by using the relationship between users and question, because the tutorial I'm following is User/Question based. I have In my models relationship have been defined like this: In the Question Model
use App\User;
class Question extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
In the User Model:
public function questions(){
return $this->hasMany('App\Question');
}
In the QuestionController I have it defined thus:
public function show($id = null)
{
$question = Question::find($id);
return view('questions.view')
->with('title', 'Make it Snappy - Question')
->with('question', $question);
}
... and in the view.blade.php I defined it thus:
@section('content')
<h1>
{!! ucfirst($question->user->username) !!}
</h1>
<p> {!! e($question->questions) !!} </p>
@stop
When I run the page it throws the error as the title describes: Whoops, looks like something went wrong.
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
Upvotes: 0
Views: 1706
Reputation: 2296
@James, your contribution is highly valid. I appreciate. Exactly where you pointed to about naming convention of table fields. Laravel maps relationship tables by appending id to the table name to create the foreign key. My mistake was the omission of the "". If you have a naming convention different from this, it has to be specified in the function while creating the model. An example:
public function questions(){
return $this->hasMany('App\Question', 'userid');
}
In the function above where the relationship is defined I could have left it thus if I'm appending '_' between the table name and the id to look like this: user_id. With this Laravel standard, you would not need to add the second properties in the function.
public function questions(){
return $this->hasMany('App\Question');
}
Upvotes: 0
Reputation: 16339
The error you are getting relates to how you are calling the user()
function in the $question
collection.
The docs are a good place for a solid understanding on this, but essentially all Eloquent relationships are defined via functions and so you may call those functions to obtain an instance of the relationship without actually executing the relationship queries.
As such it needs to be:
{!! ucfirst($question->user()->username) !!}
Nothing that we are now chaining user()
rather than user
.
Upvotes: 1