I want to display data in Laravel based on data that has been input, currently I can display user data but cannot display other data based on the ID that was input, for example I have input child data that is linked to user data
User Controller
public function getAnak(){
$dataanak = DB::table('users as I')
->select('A.id_anak', 'A.nama_anak','A.tempat_lhr','A.tgl_lhr','A.bb_lahir','A.tb_lahir','A.jenis_kelamin','A.jenis_persalinan','A.tempat_persalinan','A.dokter','A.NIK_anak')
->leftjoin('anaks as A', 'I.id', '=', 'A.id_ibu')
->get();
return view('user.dataanak', compact('dataanak'));
}
So I want to display child data based on the logged in user, currently I can only display user data as follows
dashboard.blade.php
<div class="row">
<div class="col-lg-4">
<div class="card mb-4">
<div class="card-body text-center">
<img src="https://static.vecteezy.com/system/resources/thumbnails/002/534/006/small/social-media-chatting-online-blank-profile-picture-head-and-body-icon-people-standing-icon-grey-background-free-vector.jpg" alt="avatar"
class="rounded-circle img-fluid" style="width: 150px;">
<h5 class="my-3"></h5>
<p class="text-muted mb-1">Ny. {{ auth()->user()->nama_ibu }}</p>
<p class="text-muted mb-4">{{ auth()->user()->NIK }}</p>
</div>
</div>
</div>
can anyone help this problem?
>Solution :
Filter the child data based on the user’s ID. Try the following.
use Illuminate\Support\Facades\Auth;
public function getAnak()
{
$userId = Auth::id();
$dataanak = DB::table('anaks')
->select('id_anak', 'nama_anak', 'tempat_lhr', 'tgl_lhr',
'bb_lahir', 'tb_lahir', 'jenis_kelamin',
'jenis_persalinan', 'tempat_persalinan', 'dokter', 'NIK_anak')
->where('id_ibu', $userId)
->get();
return view('user.dataanak', compact('dataanak'));
}
View
@section('content')
<div class="container">
<h1>Child Data</h1>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Date of Birth</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
@foreach ($dataanak as $child)
<tr>
<td>{{ $child->nama_anak }}</td>
<td>{{ $child->tgl_lhr }}</td>
<td>{{ $child->jenis_kelamin }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endsection