我无法以某种方式跟踪我的代码出了什么问题。我试图显示一个typ_name而不是id,所以我在类型和基础结构模型中都定义了以下关系,但是我无法显示该名称。
下面显示的型号代码
type.php
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class type
* @package App\Models
* @version January 16, 2020, 4:01 am UTC
*
* @property string typ_name
*/
class type extends Model
{
use SoftDeletes;
public $table = 'types';
protected $dates = ['deleted_at'];
public $fillable = [
'typ_name'
];
public function type()
{
return $this->hasMany('infrastructure');
}
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'typ_name' => 'string'
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'typ_name' => 'required'
];
}
infrastructure.php
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class infrastructure
* @package App\Models
* @version January 23, 2020, 1:44 am UTC
*
* @property string inf_name
* @property integer inf_height
* @property integer inf_width
* @property integer typ_id
* @property integer island_id
* @property integer engnr_id
* @property integer designer_id
* @property integer foreman_id
* @property string start_date
* @property string cmplt_date
* @property string inf_lifspan
* @property string inf_file
* @property integer inf_lat
* @property integer inf_long
* @property string inf_comment
*/
class infrastructure extends Model
{
use SoftDeletes;
public $table = 'infrastructures';
protected $dates = ['deleted_at'];
public $fillable = [
'inf_name',
'inf_height',
'inf_width',
'inf_length',
'typ_id',
'island_id',
'engnr_id',
'designer_id',
'foreman_id',
'start_date',
'cmplt_date',
'inf_lifspan',
'inf_file',
'inf_lat',
'inf_long',
'inf_comment'
];
/**
* Get the type record associated with the Infrastructure.
*/
public function type()
{
return $this->belongsTo('type', 'typ_id', 'id');
}
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'inf_name' => 'string',
'inf_height' => 'integer',
'inf_width' => 'integer',
'inf_length' => 'integer',
'typ_id' => 'integer',
'island_id' => 'integer',
'engnr_id' => 'integer',
'designer_id' => 'integer',
'foreman_id' => 'integer',
'start_date' => 'date',
'cmplt_date' => 'date',
'inf_lifspan' => 'date',
'inf_lat' => 'double',
'inf_long' => 'double',
'inf_comment' => 'string'
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'inf_name' => 'required',
'inf_height' => 'required',
'inf_width' => 'required',
'inf_length' => 'required',
'typ_id' => 'required',
'island_id' => 'required',
'engnr_id' => 'required',
'designer_id' => 'required',
'foreman_id' => 'required',
'start_date' => 'required',
'cmplt_date' => 'required',
'inf_lifspan' => 'required',
'inf_file' => 'required',
'inf_lat' => 'required',
'inf_long' => 'required',
'inf_comment' => 'required'
];
}
infrastructureController.php
public function show($id)
{
$infrastructure = $this->infrastructureRepository->find($id);
if (empty($infrastructure)) {
Flash::error('Infrastructure not found');
return redirect(route('infrastructures.index'));
}
return view('infrastructures.show')->with('infrastructure', $infrastructure);
}
在我的blade.php中
<!-- Typ Id Field -->
<div class="form-group">
{!! Form::label('typ_id', 'Type:') !!}
{{ $infrastructure->type->typ_name }}
</div>
答案 0 :(得分:0)
我认为您在infrastructure.php
中定义的关系错误。
应该是
public function type()
{
return $this->belongsTo(type::class, 'typ_id');
}
typ_id
是infrastructures
(?)表中的外键
此外,别忘了调整您的type.php
public function type()
{
return $this->hasMany(infrastructure::class);
}