嗨,伙计们! 我在图像上附加了我想在表格中看到的字段。但是,我正在检查刀片模板,可以找到为什么我无法访问该属性的原因。我创建了模型之间的特定关系,但似乎不可能从数据库中提取此信息。
这是模特
class Booking_Stays_Abroad extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'booking__stays__abroads';
/**
* The database primary key value.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = [
'id_stays_abroad',
'user_id',
'status',
'departure_date',
'return_date',
'number_weeks',
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [];
/**
* Set and get the departure_date
*
* @param string $value
* @return void
*/
public function setDepartureDateAttribute($value):void
{
$this->attributes['departure_date'] = Carbon::createFromFormat(config('app.date_format'), $value)->format('Y-m-d');
}
public function getDepartureDateAttribute($value)
{
return Carbon::createFromFormat('Y-m-d', $value)->format(config('app.date_format'));
}
/**
* Set and get the return_date.
*
* @param string $value
* @return void
*/
public function setReturnDateAttribute($value):void
{
$this->attributes['return_date'] = Carbon::createFromFormat(config('app.date_format'), $value)->format('Y-m-d');
}
public function getReturnDateAttribute($value)
{
return Carbon::createFromFormat('Y-m-d', $value)->format(config('app.date_format'));
}
public function stays_abroad()
{
return $this->belongsTo(Stays_Abroad::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
另一个
class Stays_Abroad extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'stays_abroads';
/**
* The database primary key value.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = [
'location',
'county',
'nation',
'country',
'school',
'accommodation',
'landmarks',
'price_week'
];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [];
/**
* Set the return_date.
*
* @param string $value
* @return void
*/
public function booking_stays_abroad()
{
return $this->hasMany(Booking_Stays_Abroad::class);
}
}
这是刀锋模板
<div class="form-group {{ $errors->has('id_stays_abroad') ? 'has-error' : '' }}">
<label for="id_stays_abroad" class="col-md-2 control-label">Id Stays Abroad</label>
<div class="col-md-10">
<select class="form-control" id="id_stays_abroad" name="id_stays_abroad">
<option value="" style="display: none;" {{ old('id_stays_abroad', optional($bookingStaysAbroad)->id_stays_abroad ?: '') == '' ? 'selected' : '' }} disabled selected>Select id_stays_abroad</option>
@foreach ($staysAbroads as $key => $staysAbroad)
<option value="{{ $key }}" {{ old('id_stays_abroad', optional($bookingStaysAbroad)->id_stays_abroad ?: '') == $key ? 'selected' : '' }}>
{{ $staysAbroad }}
</option>
@endforeach
</select>
{!! $errors->first('id', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('user_id') ? 'has-error' : '' }}">
<label for="user_id" class="col-md-2 control-label">User</label>
<div class="col-md-10">
<select class="form-control" id="user_id" name="user_id">
<option value="" style="display: none;" {{ old('user_id', optional($bookingStaysAbroad)->user_id ?: '') == '' ? 'selected' : '' }} disabled selected>Select user</option>
@foreach ($users as $key => $user)
<option value="{{ $key }}" {{ old('user_id', optional($bookingStaysAbroad)->user_id) == $key ? 'selected' : '' }}>
{{ $user }}
</option>
@endforeach
</select>
{!! $errors->first('user_id', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('departure_date') ? 'has-error' : '' }}">
<label for="departure_date" class="col-md-2 control-label">Departure Date</label>
<div class="col-md-10">
<input class="form-control datepicker" name="departure_date" type="text" id="departure_date" value="{{ old('departure_date', optional($staysAbroad)->departure_date) }}" placeholder="Enter departure date here...">
{!! $errors->first('departure_date', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('return_date') ? 'has-error' : '' }}">
<label for="return_date" class="col-md-2 control-label">Return Date</label>
<div class="col-md-10">
<input class="form-control datepicker" name="return_date" type="text" id="return_date" value="{{ old('return_date', optional($staysAbroad)->return_date) }}" placeholder="Enter return date here...">
{!! $errors->first('return_date', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('price_week') ? 'has-error' : '' }}">
<label for="number_weeks" class="col-md-2 control-label">Number of Weeks</label>
<div class="col-md-10">
<input class="form-control" name="number_weeks" type="number" id="number_weeks" value="{{ old('price_week', optional($staysAbroad)->price_week) }}" minlength="1" placeholder="Enter price week here...">
{!! $errors->first('price_week', '<p class="help-block">:message</p>') !!}
</div>
</div>
为简单起见,我想提取属于Stays_Abroad模型的 location 属性,并将其显示在index.blade中。
谢谢。
答案 0 :(得分:0)
您的模型Booking_Stays_Abroad的外键为'id_stays_abroad'。当您在Stays_Abroad模型中像这样返回$ this-> hasMany(Booking_Stays_Abroad :: class)创建关系时,它期望Bookin__Stays__Abroad中的外键类似于'stays_abroad_id'。因此,要么更正另一个表中的外键,要么像
那样明确提及外键public function booking_stays_abroad()
{
return $this>hasMany(Booking_Stays_Abroad::class, 'id_stays_abroad', 'id');
}