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 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项