dongle19863
2014-10-15 23:58
浏览 40
已采纳

尝试从数据透视表laravel获取列数据

Hi I'm trying to output a certain column from my pivot table. To show you what I have tried, I will first show you my models (my pivot tables are ordertasks and tagtasks):

Task table:

class Task extends \Eloquent {

    // Add your validation rules here
    public static $rules = [
        // 'title' => 'required'
    ];

    // Don't forget to fill this array
    protected $fillable = ['task_name','task_description','hour','difficulty'];

    public function ordertask()
    {   
        //oneToMany
        return $this->hasMany('Ordertask', 'id_task', 'id');
    }

    public function tagtask()
    {   
        //oneToMany
        return $this->hasMany('Tagtask', 'id_task', 'id');
    }


}

Tagtask table:

<?php

class Tagtask extends \Eloquent {
    protected $fillable = ['id_tag','id_task'];

    public function task()
    {   
        //manyToOne
        return $this->belongsTo('Task', 'id_task', 'id');
        //return $this->belongsTo('Task');
    }
    public function tag()
    {   
        //manyToOne
        return $this->belongsTo('Tag', 'id_tag', 'id');

    }
}

Ordertask table:

<?php

class Ordertask extends \Eloquent {

    // Add your validation rules here
    public static $rules = [
        // 'title' => 'required'
    ];

    // Don't forget to fill this array
    protected $fillable = ['id_order','id_task', 'hour', 'hourprice','id_user', 'createdBy'];

    public function user()
    {   
        //manyToOne
        return $this->belongsTo('User', 'id_user', 'id');
        //return $this->belongsTo('User');
    }

    public function task()
    {   
        //manyToOne
        return $this->belongsTo('Task', 'id_task', 'id');
        //return $this->belongsTo('Task');
    }
}

Here is my TaskController.php with the following piece of code:

public function index()
    {
        $Tasks=Task::with('tagtask','ordertask')->get(); 
        return View::make('user.tasks.index', compact('Tasks'));
    }

Okay now comes the part where I want to show $Task->ordertask['id_user'] on my browser with the following piece of code:

@if (count($Tasks)) 
<ul>
   @foreach($Tasks as $Task)
   <div class="row">
      <div class="col-md-3">
         <li>
            {{--as a third parameter we need to pass in the id of task, because it needs to be fed to
            our actual user.task.edit route. --}}
            {{link_to_route('user.tasks.edit', $Task['task_name'], array($Task->id))}}
         </li>
      </div>
      <div class="col-md-3">
         <li>   
            {{$Task->ordertask['id_user']}}
         </li>
      </div>
      <div class="col-md-3">
         <li>   
            {{$Task['task_hour']}}
         </li>
      </div>
      <div class="col-md-3">
         <li>
            {{Form::open(array('route'=>array('user.tasks.destroy',$Task->id),
            'method'=>'delete','class'=>'destroy'))}}
            {{Form::submit('Delete')}}
            {{Form::close()}}
         </li>
      </div>
   </div>
   @endforeach
</ul>
@endif

Unfortunately that doesn't work because I get the following error:

Undefined index: id_user

Instead of:

{{$Task->ordertask['id_user']}}

I have also tried this just to see what output it gave me:

{{$Task->ordertask}}

Which gave me the following output:

[{"id":5,"id_order":2,"id_task":1,"hour":63,"hourprice":15,"id_user":5,"createdBy":4,"created_at":"2014-10-13 10:21:33","updated_at":"2014-10-13 10:21:33"}]

So gladly I want to output id_user from the ordertask table. Gladly I'm waiting on your answer. Anyway thanks for your response.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dp815292 2014-10-16 05:59
    已采纳

    One Task can have many order tasks, so to display users instead of

    {{$Task->ordertask['id_user']}}
    

    You should use:

    @foreach ($Task->ordertask as $ot)
     {{ $ot->id_user }}
    @endforeach
    
    点赞 打赏 评论

相关推荐 更多相似问题