'How to get data from database to view page in laravel?

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?



Solution 1:[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:[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:[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:[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:[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

Solution 6:[6]

**In side controller you pass this **:

$petanidetail = DB::table('tb1_user')->get()->toArray();
return view('listpetani', compact('petanidetail'));

and Inside view you use petanidetail variable as follow:

foreach($petanidetail as $data)
{
echo $data;
}

Solution 7:[7]

Try this:

$petani = DB::table('tbl_user')->pluck('id_user', 'username', 'alamat', 'no_telp', 'id_lokasi');
return view('listpetani', ['petani' => $petani]);

on your view iterate $petani using foreach() like:

foreach($petani as $data)
{
    echo $data->id_user;  // $petani is a Std Class Object here
    echo $data->username;
}

Solution 8:[8]

I hope you already know this, but try use Model and Controller as well, Let the route take the request, and pass it to controller and use Eloquent so your code can be this short:

$petani = PetaniModel::all();
return view('listpetani', compact('petani));

and instead using foreach, use forelse with @empty statement incase your 'listpetani' is empty and give some information to the view like 'The List is Empty'.

Solution 9:[9]

Other answers are correct, I just want to add one more thing, if your database field has html tags then system will print html tags like < p > for paragraph tag. To remove this issue you can use below solution:

You may need to apply html_entity_decode to your data.

This works fine for Laravel 4

{{html_entity_decode($post->body)}}

For Laravel 5 you may need to use this instead

{!!html_entity_decode($post->body)!!}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul Chu
Solution 2
Solution 3 Khusal Berva
Solution 4 Khusal Berva
Solution 5
Solution 6
Solution 7 Mayank Pandeyz
Solution 8 David Buck
Solution 9