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