我有這張表,其中有4個id與其他表相關。使用laravel顯示列表中的列表表格的值5
表列表:
id table1_id table2_id table3_id table4_id
1 2 3 2 1
2 1 4 3 3
我現在要輸出與該表的ID的價值
id table1_name table2_name table3_name table4_name
1 jay student singer actor
2 jeane teacher drummer actress
ListController:
public function index()
{
$res = ListModel::with('table1','table2','table3','table4')->get();
$foreach($res as r){
return $r->table1_name;
}
return view('list.index',compact('res'));
}
我在這裏的問題是,輸出將是空的,而不是傑伊。我現在可以顯示的值?請幫助..
名單表
public function up()
{
Schema::create('list_table', function (Blueprint $table) {
$table->increments('id');
$table->string('table1_id');
$table->string('table2_id');
$table->string('table3_id');
$table->string('table4_id');
$table->timestamps();
});
}
//附加信息
表1型號
public function list(){
return $this->belongsTo('App\ListModel');
}
列表模型
public function table1(){
return $this->hasMany("App\Table1",'id','table1_id');
}
更新值
id crime_type_id crime_name_id crime_suspect_id crime_victim_id
1 1 1 1 1
預期輸出:
crime_type_des crime_name_des crime_suspect_name victim_name
against property theft Mcdonald Hillary
ReportController:
public function index()
{
$display_crime = CrimeReport::all();
return view('crimereports.index',compact('display_crime'));
}
report_table:
public function up()
{
Schema::create('crime_reports', function (Blueprint $table) {
$table->increments('id');
$table->string('crime_type_id');
$table->string('crime_name_id');
$table->string('suspect_id');
$table->string('victim_id');
$table->timestamps();
});
}
//附加信息
CrimeName模型
public function crimeReport(){
return $this->belongsTo('App\CrimeReport');
}
犯罪報表模型
public function crimeName(){
return $this->hasMany("App\CrimeName",'id','crime_name_id');
}
//休息了CrimeReportController
public function index()
{
$display_crime = CrimeReport::all();
return view('crimereports.index',compact('display_crime'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$display_crime_type = CrimeType::lists('crime_type','id');
$display_crime_name = CrimeName::lists('crime_description','id');
return view('crimereports.create',compact('display_crime_type','display_crime_name'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(CrimeNewReportRequest $request)
{
$input = $request->all();
CrimeReport::create($input);
return redirect()->back()->withSuccess("Fields were inserted!");;
}
感謝您的答覆,但我仍然得到空值。 –
輸出未定義屬性:Illuminate \ Database \ Eloquent \ Collection :: $ name; –
啊它的工作,但輸出仍然null.hehe –