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

尝试从数据透视表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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?