Postingan lainnya
Mohon bantuannya Data relasi tidak muncul pada view laravel
Model Jenis.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Jenis extends Model
{
protected $table="tbl_jenis_penyedia";
protected $fillable=['id_jenis_penyedia','jenis_penyedia'];
public function profile(){
return $this->belongsTo('App\Profile');
}
}
Model Profile.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
protected $table="tbl_profil_penyedia";
protected $fillable=['id_profil_penyedia','id_jenis_penyedia','nama', 'no_ktp', 'file', 'npwp', 'bank', 'no_rek', 'email', 'no_telp', 'keahlian', 'pengalaman', 'alamat', 'pendidikan'];
public function jenis(){
return $this->hasMany('App\Jenis');
}
}
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Profile;
class ProfileController extends Controller
{
public function index()
{
$profile = Profile::all();
return view('profile/homeprofile',['profile' => $profile]);
}
}
View
@foreach($profile as $p)
<tr>
<td>{{ $no++ }}</td>
<td>
@foreach($p->jenis as $a)
{{ $a->jenis_penyedia }},
@endforeach
</td>
<td>{{ $p->nama }}</td>
<td>{{ $p->no_ktp }}</td>
<td>{{ $p->npwp }}</td>
<td>{{ $p->bank }}</td>
<td>{{ $p->no_rek }}</td>
<td>{{ $p->email }}</td>
<td>{{ $p->no_telp }}</td>
<td>{{ $p->keahlian }}</td>
<td>{{ $p->pengalaman }}</td>
<td>{{ $p->alamat }}</td>
<td>{{ $p->pendidikan }}</td>
<td>
<a class="btn btn-outline-warning" type="button" href="/profile/edit/{{ $p->id_profil_penyedia }}"><i class="fas fa-edit"></i></a>
<a class="btn btn-outline-danger" type="button"href="/profile/hapus/{{ $p->id_profil_penyedia }}"> <i class="fas fa-trash-alt"></i></a>
</td>
</tr>
@endforeach
0
1 Jawaban:
Jawaban Terpilih
Dilihat dari table, mungkin harusnya model Jenis punya banyak model Profile dan model Profile hanya punya 1 model Jenis. Jadi model Profile seperti ini:
<pre> public function jenis(){ return $this->belongsTo('App\Jenis', 'id_jenis_penyedia', 'id_jenis_penyedia'); } </pre> Dan model Jenis seperti ini :
<pre> public function profile(){ return $this->hasMany('App\Profile', 'id_jenis_penyedia', 'id_jenis_penyedia'); } </pre> Dan di view nya cukup seperti ini:
<pre> <td> {{ $p->jenis->jenis_penyedia }} </td> </pre>
0