johnbob
johnbob

Reputation: 57

Relationship data to view - Laravel 5.2

Having a Problem to output a relationship data to view. Error message is: "Trying to get property of non-object View".

For explanation all Task depend to a job. So Task belongsTo Job and Job hasMany tasks. I have all Relationships in my Models and test it in Tinker everything works.

In my view I Output every Task name and the job name

  @foreach ($tasks as $task)
    <div class="list-item">
         <span class="item-name">{{ $task->job->name }}
         <span class="item-name">{{ $task->name}} </span>
     </div>
  @endforeach

The index function of my TaskController:

public function index(Request $request)
{
    $label = Label::all();
    $user = User::all();
    $task = Task::orderBy('duedate')->get();
    $team = Team::all();
    $customer = Customer::all();
    $status = Status::all();
    $job = Job::all();
    return view('tasks.index')->with([
        'tasks' => $task,
        'teams' => $team,
        'customers' => $customer,
        'labels' => $label,
        'users' => $user,
        'jobs' => $job,
        'statuses' => $status,
    ]);
}

Table schema / output from tinker

 id: 1,
 assigned_user_id: 1,
 team_id: 4,
 name: "Label many to many ",
 duration: 2,
 created_at: "2016-06-16 14:50:57",
 updated_at: "2016-07-05 09:10:34",
 job_id: 1,
 duedate: "0000-00-00 00:00:00",
 status_id: 3,
 job: App\Job {#702
   id: 1,
   name: "quia",
   jobnumber: "8076",
   customer_id: 2,
   status_id: 0,
   created_at: null,
   updated_at: null,
 },
 user: null,

Relationships

**Job Model **

class Job extends Model
{
protected $fillable = ['name', 'jobnumber', 'customer_id', 'status_id'];

/**
 * Get all Task for Job
 */
public function task()
{
    return $this->hasMany(Task::class);
}

Task model

public function job()
{
    return $this->belongsTo(Job::class);
}

Hope you can help me, thanks!

Upvotes: 1

Views: 252

Answers (4)

Sunny Kumar
Sunny Kumar

Reputation: 544

This kind o f error occurs when you are trying to print a model value that doesn't exists. Try to print using {{isset($task->job->name)?$task->job->name:'Task without Job'}} and check what it outputs.

Upvotes: 1

Sanzeeb Aryal
Sanzeeb Aryal

Reputation: 3266

With reference to your answer:

In the DB some Task entries had an job_id to an none exisiting Job

You can use has method to limit your results based on the existence of a relationship. see doc for querying relationship absence

$task=Task::orderBy('duedate')->has('job')->get();

Upvotes: 1

johnbob
johnbob

Reputation: 57

Found the solution.

In the DB some Task entries had an job_id to an none exisiting Job, thats it.

Upvotes: 0

AntoineB
AntoineB

Reputation: 4694

You don't load the "job" relationship on $task before trying to access it's values :

public function index(Request $request)
{
    $label = Label::all();
    $user = User::all();
    $task = Task::with('job')->orderBy('duedate')->get(); // loading the relationship
    $team = Team::all();
    $customer = Customer::all();
    $status = Status::all();
    $job = Job::all();
    return view('tasks.index')->with([
        'tasks' => $task,
        'teams' => $team,
        'customers' => $customer,
        'labels' => $label,
        'users' => $user,
        'jobs' => $job,
        'statuses' => $status,
    ]);
}

Upvotes: 0

Related Questions