我希望有人帮助我逐步完成从数据库检索数据的laravel过程。这就是我所做的。问题是没有显示数据。我在这方面不太好,需要一些帮助。谢谢
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\ViewController@getContacts'
]);
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 :(得分: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>
答案 1 :(得分:0)
我在view.blade.php
中添加了这个功能 @foreach($contacts as $display) {{$display}}
@endforeach