2016-04-17 4 views
0

私は、databseからデータを取得する際にlaravelプロセスをステップバイステップで手助けしたいと思います。これは私がやったことです。問題は、データが表示されていないことです。私はこれではあまりよくないですし、いくつかのhelp.Thankslaravelを使用してデータベースからデータを後退させる

ViewController.php

<?php 
namespace App\Http\Controllers\AddressBook; 
use DB; 
use App\Http\Controllers\Controller; 
use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Input; 
use Illuminate\Support\Facades\Session; 
use Illuminate\Support\Facades\Validator; 
use Illuminate\Support\Facades\Redirect; 

class ViewController extends Controller 
{ 
    /** 
    * Show a list of all of the application's users. 
    * 
    * 
    */ 
     Public function getContacts(){ 

     $contacts= AddressBookModel::all(); 

     $data = ['contacts' => $contacts]; 

     return view('view')->with($data); 

     } 

} 
**Route** 
Route::get('contacts',[ 
    'uses'=>'AddressBook\[email protected]' 
]); 
The Route is working well and its connecting and display the content in view.blade.php 

**view.blade.php** 
<!DOCTYPE html> 
<html> 
    <head> 
     <title>Test</title> 



    </head> 
    <body> 
     <h2 align="center">These are the Registered Contacts in the Database</h2> 
    </body> 

</html> 

答えて

0

はこれを試してみてください必要があります。 コントローラー:

public function getContacts() 
    { 
     // Try to name your model Contact instead of AddressBookModel 
     $contacts= AddressBookModel::all();// = Contact::all();  
     return view('view')->withContacts($contacts); 
    } 

ビュー:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Test</title> 
</head> 
<body> 
    <h2 align="center">These are the Registered Contacts in the Database</h2> 
<table> 
<!-- I assume that name and phone are contact model attributes--> 
<th>Name</th> 
<th>Phone</th> 
@foreach ($contacts $as $contact) 
    <tr> 
     <td> {{$contact->name}} </td> 
     <td> {{$contact->phone}} </td> 
    </tr> 
@endforeach 
</table> 
</body> 

+0

おかげでこの機能を追加しました。それは私がそれを達成するのを助けましたが、これはうまくいった –

+0

:) 私の答えを参考にしてください:)) – BKF

0

私はview.blade.phpにご入力のための

@foreach($contacts as $display) {{$display}} 
    @endforeach 
関連する問題