0
votes

Despite having implemented many L4 relationships and them working most of the time. I am struggling and for some reason cannot see where I have gone wrong. It is almost certainly something obvious.

The relationship is that A task has one topic, topics have many tasks.

Task Model

class Task extends Eloquent {


    protected $guarded = array();

    public static $rules = array(

    );

    public function resources()
    {
        return $this->belongsToMany('Resource');
    }


    public function topic()
    {
        return $this->belongsTo('Topic', 'topic_id');
    }

    }

Topic Model

class Topic extends Eloquent {

    protected $guarded = array();

    public static $rules = array();


    public function tasks()
    {
        return $this->hasMany('Task');
    }

}

Task Controller

 public function show($id)
    {
        $task = $this->task->where('number', $id);

        return View::make('tasks.show', compact('task'));
    }

when I dd ($task) the relations array is empty.

object(Task)#688 (21) { ["guarded":protected]=> array(0) { } ["connection":protected]=> NULL ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(11) { ["id"]=> string(1) "9" ["number"]=> string(1) "6" ["topic"]=> string(1) "2" ["topic_old"]=> string(10) "War Effort" ["task"]=> string(28) "You want us to collect Moss?" ["objective"]=> string(36) "Collecting Information including Q&A" ["method"]=> string(29) "Supported historical research" ["context"]=> string(0) "" ["level"]=> string(0) "" ["created_at"]=> string(19) "0000-00-00 00:00:00" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } ["original":protected]=> array(11) { ["id"]=> string(1) "9" ["number"]=> string(1) "6" ["topic"]=> string(1) "2" ["topic_old"]=> string(10) "War Effort" ["task"]=> string(28) "You want us to collect Moss?" ["objective"]=> string(36) "Collecting Information including Q&A" ["method"]=> string(29) "Supported historical research" ["context"]=> string(0) "" ["level"]=> string(0) "" ["created_at"]=> string(19) "0000-00-00 00:00:00" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) }

1
DB tasks table has topic_id latitudehopper

1 Answers

0
votes

You need to call the relationship that you declare in the model before. In this case I will use Eager Loading, with() function.

$tasks = $this->task->where('number', $id)->with('topic');

source: http://laravel.com/docs/4.2/eloquent#eager-loading

I would like to change the variable to $tasks because using where(), you will get instant of Illuminate\Database\Eloquent\Collection (many results)

And you can check the result by.

dd($tasks->get());