How to get data from database to view page in laravel?
Solution 1
[SOLVE]
Thank you guys, I already solve this problem
This is the solved code
web.php (routes)
Route::get('listpetani', function () {
$petani = DB::table('tbl_user')->get();
return view('listpetani', ['petani' => $petani]);
});
and in my listpetani.blade.php
@foreach($petani as $key => $data)
<tr>
<th>{{$data->id_user}}</th>
<th>{{$data->nama_user}}</th>
<th>{{$data->alamat}}</th>
<th>{{$data->no_telp}}</th>
<th>{{$data->id_lokasi}}</th>
</tr>
@endforeach
Solution 2
You can get data from database in view also
@php( $contacts = \App\Contact::all() )
@php( $owners = \App\User::all())
<select class="form-control" name="owner_name" id="owner_name">
@foreach($contacts as $contact)
<option value="{{ $contact->contact_owner_id }}">{{ $contact->contact_owner }}</option>
@endforeach
</select>
It would be better if you pass data from controller to view.
return view('greetings', ['name' => 'Victoria']);
Checkout the docs: https://laravel.com/docs/8.x/views#passing-data-to-views
Solution 3
Alternatively you can use @forelse loop inside laravel blade
@forelse($name as $data)
<tr>
<th>{{ $data->id}}</th>
<th>{{ $data->name}}</th>
<th>{{ $data->age}}</th>
<th>{{ $data->address}}</th>
</tr>
@empty
<tr><td colspan="4">No record found</td></tr>
@endforelse
Solution 4
In your controller:
$select = DB::select('select * from student');
return view ('index')->with('name',$select);
In Your view:
@foreach($name as $data)
<tr>
<th>{{ $data->id}}</th> <br>
<th>{{ $data->name}}</th> <br>
<th>{{ $data->age}}</th> <br>
<th>{{ $data->address}}</th>
</tr>
@endforeach
I hope this can help you.
Solution 5
@foreach($petani as $p)
<tr>
<td>{{ $p['id_user'] }}</td>
<td>{{ $p['username'] }}</td>
<td>{{ $p['alamat'] }}</td>
<td>{{ $p['no_telp'] }}</td>
<td>{{ $p['id_lokasi'] }}</td>
</tr>
@endforeach
Yosua Michael
Updated on July 15, 2022Comments
-
Yosua Michael almost 2 years
I am using Laravel 5.4 and I want to view my data in database from my view page (
listpetani.blade.php
).Here is the code of my project:
HTML:
<div class="table-responsive"> <table class="table table-striped table-hover table-condensed"> <thead> <tr> <th><strong>No</strong></th> <th><strong>Nama Petani</strong></th> <th><strong>Alamat</strong></th> <th><strong>No. Handphone</strong></th> <th><strong>Lokasi</strong></th> </tr> </thead> <tbody> <tr> <th></th> <th></th> <th></th> <th></th> <th></th> </tr> </tbody> </table> </div>
PHP: In my
listpetani.blade.php
I have an empty table and I want to show data from database tbl_user:Route::get('listpetani', function () { $petani = DB::table('tbl_user')->pluck('id_user', 'username', 'alamat', 'no_telp', 'id_lokasi'); return view('listpetani', ['petani' => $petani]); });
And the table in my page: view in browser
I want to show all the data from database into my view in laravel 5.4. Can anybody help me?
-
Yosua Michael about 7 yearsTrying to get property of non-object
-
Yosua Michael about 7 yearsUndefined variable: petanidetail that's the error message if i change with your suggest
-
ram pratap singh about 7 yearsyou should pass petanidetail with view return as follow above in controller
-
Nirbhay Kularia about 7 yearstry $p['id_user'] instead of $p->id _user, for all the values
-
VeeZ Phone over 6 yearscan you help for this stackoverflow.com/questions/45893651/…
-
Hemant Kumar over 5 yearsYou can send data from controller via compact function e.g. : $owners = \App\User::all() return view('viewowners', compact('owners'));
-
Sandesh Poudel almost 3 yearsThat's great but it's advised not to use DB class in Route file. This may make your application vulnerable.