简体   繁体   English

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

[英]Trying to get column data from pivot table 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): 为了向您展示我所尝试的内容,我将首先向您展示我的模型(我的数据透视表是ordertasks和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: Tagtask表:

<?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: 这是我的TaskController.php,其中包含以下代码:

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: 现在好了,我想用以下代码在浏览器上显示$ Task-> ordertask ['id_user']的部分:

@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. 所以很高兴我想从ordertask表中输出id_user。 Gladly I'm waiting on your answer. 很高兴我在等待您的答复。 Anyway thanks for your response. 无论如何,谢谢您的回复。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM