Yosua Michael
Yosua Michael

Reputation: 417

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 the 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 on my page:

view in browser

I want to show all the data from the database in my view in Laravel 5.4. Can anybody help me?

Upvotes: 10

Views: 139114

Answers (9)

Kamlesh
Kamlesh

Reputation: 6135

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)!!}

Upvotes: 0

Hemant Kumar
Hemant Kumar

Reputation: 1125

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

Upvotes: 5

Egy2015
Egy2015

Reputation: 11

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'.

Upvotes: 1

11Strong
11Strong

Reputation: 139

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.

Upvotes: 4

Khusal Berva
Khusal Berva

Reputation: 134

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

Upvotes: 4

Yosua Michael
Yosua Michael

Reputation: 417

[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

Upvotes: 12

Nirbhay Kularia
Nirbhay Kularia

Reputation: 101

@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

Upvotes: 3

ram pratap singh
ram pratap singh

Reputation: 53

**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;
}

Upvotes: 2

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

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;
}

Upvotes: 0

Related Questions