【发布时间】:2015-01-20 07:48:40
【问题描述】:
我有两张表,分别是behavioralmain 和behavioralsub,behaviorsub 是behaviormain 的chils 表。显然,behaviorsub 有 parent_id,它对应于 behaviormain 的 id。我尝试使用此代码加入这两个表。
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>
在上面的代码中,我假设它可以得到我想要的,但很遗憾地说,视图侧的唯一显示就是这个;这是父表,而子表没有检索,
category,commitment。
我需要的输出是这样的。
commitment
-administer
-president
category
-secretary
-mixture
请问怎么做。我已经尝试了一切,但没有任何反应
【问题讨论】:
标签: php mysql laravel controller eloquent