我有这个表,其中4个id与其他表有关。
表格列表:
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'));
}
我的问题是输出将为null而不是jay。如何显示值?请帮助..
列表
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 Model
public function crimeReport(){
return $this->belongsTo('App\CrimeReport');
}
犯罪报告模型
public function crimeName(){
return $this->hasMany("App\CrimeName",'id','crime_name_id');
}
// Rest for 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!");;
}
答案 0 :(得分:0)
更改您的关系定义如下:
表1模型
public function list(){
return $this->hasMany('App\ListModel');
}
列表模型
public function table1(){
return $this->belongsTo("App\Table1");
}
您需要更新索引方法,如下所示,您可以转储$行以检查是否有效。
ListController
public function index()
{
$res = ListModel::with(['table1','table2','table3','table4'])->get();
$rows = [];
$foreach($res as r) {
$rows[] = [r->table1->name, r->table2->name, r->table3->name, r->table4->name];
}
dd($rows);
}
答案 1 :(得分:0)
您在原始模型上访问table1_name会将_更改为 - > ($ r-> table1-> name)
编辑: 现在它开始变得更具可读性。以下是我认为的关系应该是什么。
CrimeReport.php
public function crimeType()
{
return $this->belongsTo(CrimeType::class);
}
public function crimeName()
{
return $this->belongsTo(CrimeName::class);
}
public function suspect()
{
return $this->belongsTo(Suspect::class);
}
public function victim()
{
return $this->belongsTo(Victim::class);
}
似乎没有必要立即定义任何关系中的任何关系,如果需要稍后再添加它们,但没有必要创建关系只是因为它在那里。
现在,为了让报告变得简单,可以使用一些联接或者急切加载。
CrimeReportController.php
public function index()
{
$results = CrimeReport::query()
->join('crime_types as ct','ct.id', '=', 'crime_reports.crime_type_id')
->join('crime_names as cn','cn.id', '=', 'crime_reports.crime_name_id')
->join('suspects as s','s.id', '=', 'crime_reports.suspect_id')
->join('victims' as v','v.id', '=', 'crime_reports.victim_id')
->get([
'crime_report.*', //to get ids and timestamps
'ct.name as crime_type',
'cn.name as crime_name',
'suspect.name as suspent_name',
'victim.name as victim_name'
])
dd($results->toArray());
}
现在$ results仍将是CrimeReport的集合,因此您仍然可以访问您可能想要在那里投入的所有模型好东西。除此之外你还有你的桌子,你可以将它们打印到一个html表格中,然后你就有了你的报告。
如果您想在查询中添加一些很酷的搜索内容,那么您可以使用whereHas来执行此操作:
//this would just go before the get()
$query->whereHas('suspect', function($q){
$q->where('hair_color','=','brown')
->whereBetween('height',5.8,6)
})
当然,你可能也想做一些leftJoins而不是因为我认为他们都不会有受害者/嫌疑人