I have two tables that are related by a foregin key:
Equipment Table
ID primary Site_ID foregin UnitNo Model
Site Table
ID Sitename
I want to show all the equipment from the equipment table but where is says site_id, I want to show the Site Name.
I am very new to laravel 4 and I am really stuck on how to produce this, my code is as follows:
<?php var_dump($equipment); ?>
@foreach($equipment as $item)
<ul>
<li>{{$item->site_id}}</li>
<li>{{$item->unitNo}}</li>
<li>{{$item->Model}}</li>
</ul>
@endforeach
Controller:
<?php
class EquipmentController extends BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$equipment = equipment::all();
return View::make('equipment.index', compact('equipment'));
}
Equipment Model
<?php
class Equipment extends Eloquent {
protected $guarded = array();
public static $rules = array();
public function site()
{
return $this->hasOne('site', 'site_id');
}
}
Site Model
<?php
class Site extends Eloquent {
protected $guarded = array();
public static $rules = array();
public function equipment()
{
return $this->hasMany('equipment');
}
}
I am completley lost at this point. Any help help would be apprciated
Thanks