データベースからデータを取得する際の段階的な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::get('contacts',[
'uses'=>'AddressBook\ViewController@getContacts'
]);
ルートは正常に機能しており、接続して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>
これを試してください:コントローラー:
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>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加