Hi I am retrieving tasks in my tasks table in laravel. each task has a related status_id, these statuses are held in the statuses table, a user can also create their own statuses.
id | user_id | status_name
1 | 1 | low
2 | 1 | high
id | user_id |status_id | task_name | task_brief
1 | 1 |2 | taskone | brief
2 | 1 |1 | tasktwo | brief
I want to retrieve all the users tasks, each task has a status e.g. Dev, Test, In Progress etc. This is reflected in the tasks table by the status_id
. I'm trying to retrieve the status name so I can show this on a dashboard. My query is correct as I have tested it using the following:
{{ '<pre>',print_r($user),'</pre>' }}
The data received is correct, from the task to the user to the status db details. However there seems to be a problem with the way I am retrieving the data in my foreach
as I am getting the following error:
I'm not sure whats going on but my code is below, would appreciate if somebody could help as my knowledge is quite limited.
UserController.php
public function opentasks() {
$user = User::with(array('tasks', 'tasks.status'))->find(Auth::user()->id);
return View::make('users.opentasks')->with('user', $user);
}
users/opentasks.blade.php
@foreach($user->tasks as $task)
{{ $task->task_name }}
{{ $task->task_brief}}
@if($task->status->count() > 0)
@foreach($task->status as $status)
{{ $status->status_name }}
@endforeach
@endif
@endforeach
task.php
public function status(){
return $this->hasOne('Status', 'id', 'status_id');
}
user.php
public function tasks(){
return $this->hasMany('Task', 'user_id');
}
Routes.php
{
Route::any("user/opentasks", [
"as" => "user/opentasks",
"uses" => "UserController@opentasks"
]);
Table schema
Tasks table
Status Table
Error Logs
[2014-03-14 22:01:28] log.ERROR: exception 'ErrorException' with message 'Trying to get property of non-object' in /Applications/XAMPP/xamppfiles/htdocs/iTempus/app/storage/views/118f4221e407fdefc163f47a92f5bbe3:6
Stack trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/iTempus/app/storage/views/118f4221e407fdefc163f47a92f5bbe3(6): Illuminate\Exception\Handler->handleError(8, 'Trying to get p...', '/Applications/X...', 6, Array)
#1 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(38): include('/Applications/X...')
#2 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(45): Illuminate\View\Engines\PhpEngine->evaluatePath('/Applications/X...', Array)
#3 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/View/View.php(100): Illuminate\View\Engines\CompilerEngine->get('/Applications/X...', Array)
#4 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/View/View.php(81): Illuminate\View\View->getContents()
#5 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Http/Response.php(70): Illuminate\View\View->render()
#6 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Response.php(141): Illuminate\Http\Response->setContent(Object(Illuminate\View\View))
#7 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1414): Symfony\Component\HttpFoundation\Response->__construct(Object(Illuminate\View\View))
#8 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Controllers/Controller.php(156): Illuminate\Routing\Router->prepare(Object(Illuminate\View\View), Object(Illuminate\Http\Request))
#9 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Controllers/Controller.php(126): Illuminate\Routing\Controllers\Controller->processResponse(Object(Illuminate\Routing\Router), 'opentasks', Object(Illuminate\View\View))
#10 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Router.php(985): Illuminate\Routing\Controllers\Controller->callAction(Object(Illuminate\Foundation\Application), Object(Illuminate\Routing\Router), 'opentasks', Array)
#11 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#12 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Route.php(80): call_user_func_array(Object(Closure), Array)
#13 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Route.php(47): Illuminate\Routing\Route->callCallable()
#14 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1016): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#15 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(574): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#16 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(550): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#17 /Applications/XAMPP/xamppfiles/htdocs/iTempus/public/index.php(49): Illuminate\Foundation\Application->run()
#18 {main} [] []
2014-03-14 22:01:28] log.ERROR: exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1429
Stack trace:
#0 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1050): Illuminate\Routing\Router->handleRoutingException(Object(Symfony\Component\Routing\Exception\ResourceNotFoundException))
#1 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1014): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#2 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(574): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#3 /Applications/XAMPP/xamppfiles/htdocs/iTempus/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(550): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#4 /Applications/XAMPP/xamppfiles/htdocs/iTempus/public/index.php(49): Illuminate\Foundation\Application->run()
#5 {main} [] []
try this instead
@foreach($user->tasks as $task)
{{ $task->task_name }}
{{ $task->task_brief}}
@if(!is_null($task->status))
{{ $task->status->status_name }}
@endif
@endforeach
You have some null values on the return, so check for it before trying to parse.
Also, you should be using belongsTo relationship on task.status
.
Goodluck.