dpqaaczn141761 2014-11-22 03:17
浏览 24

在Laravel中显示父数据下的子数据

I have two tables which is behavioralmain and behavioralsub the behavioralsub is the chils table of the behavioralmain. Apparently, the behavioralsub have parent_id which is corresponding to the id of the behavioralmain. I've tried to join the two table with the use of this code. behavioralmain Table

ID |   category_name  |   actual weight   | weight
 1      commitment            0               7
 2      category              0               5

behavioralsub Table

ID |   category_name  |   parent_id   |  weight
 1      administer            1            7
 2      president             1            5
 3      secretary             2            7
 4      mixture               2            5

behavioralmain.model

     /**
     * The database table used by the model.
     *
     * @var string
     */
    public $timestamps=false;
    protected $table = 'behavioralmain';


    public function behavioralsub()
         {
            return $this->hasMany('BehavioralSub','id','parent_id');
         }

}

behavoiralsub.model

<?php 
class BehavioralSub  extends Eloquent   {

     /**
     * The database table used by the model.
     *
     * @var string
     */
    public $timestamps=false;
    protected $table = 'behavioralsub';

    protected $fillable=array('id','survey_question','weight');


    public function parenting() {
    return $this->belongsTo('behavioralmain','id','parent_id');
  }
}

BehavoiralSubController.php

public function index()
    {
        return View::make('behavioralsub.index')->with('sub',BehavioralSub::get())->with('sub',BehavioralMcain::with(array('behavioralsub'))->get());
    }

Index.blade.php

 <tbody>
@foreach($sub as $sub)
    <tr>
      <td>{{{$sub->category_name}}}</td>
      <td>{{{$sub->survey_question}}}</td>
      <td>{{{$sub->weight}}}</td>                    
      <td><a href="{{{route('behavioralsub.edit',$sub->id)}}}"><i class="fa fa-edit"></i>Edit</a>                        
      </td>
      <td>
                                        <a href="{{{route('behavioralsub.destroy',$sub->id)}}}"><i class="glyphicon glyphicon-trash"></i>Delete</a>                        

                                    </td>

                                </tr>
                                @endforeach
                            </tbody>

In the code above, I assume that it can get what I want but sad to say the only displays in the view side is this only;this the parent table while the child table didn't retrieve, category,commitment.

The output that I need is like this.

commitment
   -administer
   -president
category
   -secretary
   -mixture

Please how to do this. I've tried everything but nothing's happen

  • 写回答

1条回答 默认 最新

  • douyaju4259 2014-11-22 03:33
    关注

    Try this:

    behavioralsub model:

    public function parenting() {
        return $this->belongsTo('BehavioralMain', 'parent_id', 'ID');
    }
    

    behavioralmain model

    public function behavioralsub() {
        return $this->hasMany('BehavioralSub', 'parent_id', 'ID');
    }
    

    The way you are passing data to the View has closing parenthesis in all the wrong places and seems to be method chaining the wrong objects. Try changing it to:

    public function index() {
            return View::make('behavioralsub.index')->with(array('main' => BehavioralMcain::with('behavioralsub')->get());
    }
    

    which would make your view:

    <tbody>
        @foreach($main->behavioralsub as $sub)
            <tr>
                <td>{{{$sub->category_name}}}</td>
                <td>{{{$sub->survey_question}}}</td>
                <td>{{{$sub->weight}}}</td>                    
                <td>
                    <a href="{{{route('behavioralsub.edit',$sub->id)}}}"><i class="fa fa-edit"></i>Edit</a>                        
                </td>
                <td>
                    <a href="{{{route('behavioralsub.destroy',$sub->id)}}}"><i class="glyphicon glyphicon-trash"></i>Delete</a>                        
    
                </td>
    
            </tr>
        @endforeach
    </tbody>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)