I was trying to show foreign data from Competency table to Summary table. It works, but it didn't show the data. It's just blank. No Errors. Look at this image:
The empty cell should be the competency_name (look "competency table" image).
On the Add page, the dropdown showed 3 values but they are all blank. And they are the exact same number of value (rows) in the Competency table on the db (3). It means that they are connected.
I already tried to make new table, new model and migration, but it's just show the same thing.
The Summary model:
class Summary extends Model
{
protected $table = "summary";
protected $primaryKey = "id";
protected $fillable = [
'id', 'competency_id', 'price'
];
public function competency()
{
return $this->belongsTo(Competency::class);
}
}
The Competency model:
class Competency extends Model
{
protected $table = "competency";
protected $primaryKey = "id";
protected $fillable = [
'id', 'competency_name'
];
public function summary()
{
return $this->hasMany(Summary::class);
}
}
This is my SummaryController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Summary;
use App\Models\Competency;
class SummaryController extends Controller
{
public function index()
{
$summaryData = Summary::with('competency')->get();
return view('dashboard', compact('summaryData'));
}
}
My dashboard.blade.php:
<tbody>
@foreach ($summaryData as $item)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$item->competency->competency}}</td>
<td>{{$item->price}}</td>
<td>
<a href="{{url('edits', $item->id)}}" >Edit</a>
<a href="{{route('delete', $item->id)}}" >Delete</a>
</td>
</tr>
@endforeach
</tbody>
I need your help. I really appreciate your answers. Thank you.
CodePudding user response:
u have missed a small point inside foreach loop... make this correction
{{$item->competency->competency_name}}