我想從我的數據庫中獲取數據,但當我嘗試從另一個表中獲取數據時,我卡住了。laravel中一對多關係中的數據獲取
這是我想要做的,當一個用戶登錄到網頁時,他將能夠看到一個人名列表。通過點擊他們的名字,用戶將被重定向到另一個頁面,該頁面將顯示個人詳細信息,如姓名,地址和電子郵件。但是,因爲有這麼多的(假設200人左右)
問題:
Q1。如何將用戶重定向到另一個名稱(例如:插孔,用戶點擊插孔名稱,插孔ID = 1,我如何將其與用戶ID = 1相關聯,即外鍵)
Q2。我怎麼做一個循環來獲取他們的信息,而不是一個一個地指定它(例如:我不想說id = 1,是否有像id ++這將有助於自動添加)
home.blade.php
<table class="table table-bordered">
<tr>
<th><strong><big>Name AS PER NRIC/PASSPORT: </big></strong></th>
</tr>
<td>
<tr>
@foreach($data as $value)
<tr>
<th><a href="www.google.com"> {{$value->Name}}</a></th> --> I don't know how to redirect it to show user details
</tr>
@endforeach
</tr>
</tr>
</table>
控制器
public function index()
{
return view('home');
}
public function getData(){
$data['data'] = DB::table('personal_infos')->get()->sortByDesc('upload_time');
if(count($data)>0){
return view('home',$data);
}else{
return view('home');
}
}
}
路線
Route::get('/test','[email protected]');
的personal_info模型
class personal_info extends Eloquent
{
protected $fillable = array('Email', 'Name', 'address');
protected $table = 'personal_infos';
protected $primaryKey = 'id';
public function user_infos() {
return $this->hasMany('App\user_info,'user_id');
}
public function user_info1s() {
return $this->hasMany('App\user_info1','user_id');
}
}
USER_INFO和user_info1模型
class user_info1 extends Eloquent
{
protected $fillable = array('hobby','sport','user_id');
public function personal_infos() {
return $this->belongsTo('App\personal_info', 'user_id', 'id');
}
'personal_infos ::與( 'user_info1s') - > get()方法'? – madalinivascu
@madlinivascu我已經嘗試過了,但它只適用於技術升級,我想根據不同表中的用戶ID獲取數據 – blastme