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

99,125

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
Share:
99,125
Yosua Michael
Author by

Yosua Michael

Updated on July 15, 2022

Comments

  • Yosua Michael
    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
    Yosua Michael about 7 years
    Trying to get property of non-object
  • Yosua Michael
    Yosua Michael about 7 years
    Undefined variable: petanidetail that's the error message if i change with your suggest
  • ram pratap singh
    ram pratap singh about 7 years
    you should pass petanidetail with view return as follow above in controller
  • Nirbhay Kularia
    Nirbhay Kularia about 7 years
    try $p['id_user'] instead of $p->id _user, for all the values
  • VeeZ Phone
    VeeZ Phone over 6 years
  • Hemant Kumar
    Hemant Kumar over 5 years
    You can send data from controller via compact function e.g. : $owners = \App\User::all() return view('viewowners', compact('owners'));
  • Sandesh Poudel
    Sandesh Poudel almost 3 years
    That's great but it's advised not to use DB class in Route file. This may make your application vulnerable.