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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?