laravel eloquent 模型 属性 的问题
Issue with laravel eloquent model property
我正在尝试在视图中打印 {{$day->date}}。而不是完整日期 YYYY-MM-DD 属性 只有 returns 年 YYYY.
谁能解释一下为什么会这样?以及如何从 属性 中获取完整日期?
代码
@foreach($calendar as $day)
{{dd($day->date)}}
<br>
@endforeach
预期输出
2022-03-01
2022-03-02
...
...
实际输出
2022
2022
...
...
视图中 $day
的属性:使用 {{dd($day)}}
blade
#attributes: array:4 [▼
"date" => "2022-03-01"
"day" => "Tuesday"
"note" => "Public Holiday - Maha Sivarathri"
"is_working_day" => 0
]
视图中 {{dd($day->date)}}
的输出
2022
控制器
$calendar = Calendar::wherebetween('date',[$date_from,$date_to])->get();
$data['calendar']=$calendar;
return view('cms.advanceprogram.calendar')->with($data);
Calendar.php模型
class Calendar extends Model{
use HasFactory;
protected $primaryKey = 'date';
public function advanceprograms(){
return $this->belongsToMany(AdvanceProgram::class, 'advance_program_calendar', 'date', 'advance_program_id');
}
}
迁移
Schema::create('calendars', function (Blueprint $table) {
$table->date('date')->primary();
$table->string('day');
$table->string('note')->nullable();
$table->boolean('is_working_day');
});
我认为,您不应该使用日期作为主键。为主键添加列 id
。
Laravel 将主键转换为整数。替换或删除模型中的 protected $primaryKey
,您将在 Blade 中得到正确的结果。
型号Calendar
class Calendar extends Model
{
use HasFactory;
public function advanceprograms(){
return $this->belongsToMany(AdvanceProgram::class, 'advance_program_calendar', 'date', 'advance_program_id');
}
}
查看
@foreach($calendar as $day)
{{$day->date}}
<br>
@endforeach
结果:
2022-03-01
或
添加到Calendar
行protected $keyType = 'string';
像这样:
class Calendar extends Model
{
use HasFactory;
protected $primaryKey = 'date';
protected $keyType = 'string';
public function advanceprograms(){
return $this->belongsToMany(AdvanceProgram::class, 'advance_program_calendar', 'date', 'advance_program_id');
}
}
或
添加到Calendar
行protected $keyType = 'date';
那么字段 date
将是 Carbon
class 的实例。
像这样:
class Calendar extends Model
{
use HasFactory;
protected $primaryKey = 'date';
protected $keyType = 'date';
public function advanceprograms(){
return $this->belongsToMany(AdvanceProgram::class, 'advance_program_calendar', 'date', 'advance_program_id');
}
}
那你可以在视图中使用Carbon方法。像这样:
@foreach($calendar as $day)
{{$day->date->format('d-m-Y')}}
<br>
@endforeach
你可以试试这个
protected $primaryKey = 'date';
public $incrementing = false; // no auto increment
public $timestamps = false; // if there are not timestamp table
I think it's work
我正在尝试在视图中打印 {{$day->date}}。而不是完整日期 YYYY-MM-DD 属性 只有 returns 年 YYYY.
谁能解释一下为什么会这样?以及如何从 属性 中获取完整日期?
代码
@foreach($calendar as $day)
{{dd($day->date)}}
<br>
@endforeach
预期输出
2022-03-01
2022-03-02
...
...
实际输出
2022
2022
...
...
视图中 $day
的属性:使用 {{dd($day)}}
blade
#attributes: array:4 [▼
"date" => "2022-03-01"
"day" => "Tuesday"
"note" => "Public Holiday - Maha Sivarathri"
"is_working_day" => 0
]
视图中 {{dd($day->date)}}
的输出
2022
控制器
$calendar = Calendar::wherebetween('date',[$date_from,$date_to])->get();
$data['calendar']=$calendar;
return view('cms.advanceprogram.calendar')->with($data);
Calendar.php模型
class Calendar extends Model{
use HasFactory;
protected $primaryKey = 'date';
public function advanceprograms(){
return $this->belongsToMany(AdvanceProgram::class, 'advance_program_calendar', 'date', 'advance_program_id');
}
}
迁移
Schema::create('calendars', function (Blueprint $table) {
$table->date('date')->primary();
$table->string('day');
$table->string('note')->nullable();
$table->boolean('is_working_day');
});
我认为,您不应该使用日期作为主键。为主键添加列 id
。
Laravel 将主键转换为整数。替换或删除模型中的 protected $primaryKey
,您将在 Blade 中得到正确的结果。
型号Calendar
class Calendar extends Model
{
use HasFactory;
public function advanceprograms(){
return $this->belongsToMany(AdvanceProgram::class, 'advance_program_calendar', 'date', 'advance_program_id');
}
}
查看
@foreach($calendar as $day)
{{$day->date}}
<br>
@endforeach
结果:
2022-03-01
或
添加到Calendar
行protected $keyType = 'string';
像这样:
class Calendar extends Model
{
use HasFactory;
protected $primaryKey = 'date';
protected $keyType = 'string';
public function advanceprograms(){
return $this->belongsToMany(AdvanceProgram::class, 'advance_program_calendar', 'date', 'advance_program_id');
}
}
或
添加到Calendar
行protected $keyType = 'date';
那么字段 date
将是 Carbon
class 的实例。
像这样:
class Calendar extends Model
{
use HasFactory;
protected $primaryKey = 'date';
protected $keyType = 'date';
public function advanceprograms(){
return $this->belongsToMany(AdvanceProgram::class, 'advance_program_calendar', 'date', 'advance_program_id');
}
}
那你可以在视图中使用Carbon方法。像这样:
@foreach($calendar as $day)
{{$day->date->format('d-m-Y')}}
<br>
@endforeach
你可以试试这个
protected $primaryKey = 'date';
public $incrementing = false; // no auto increment
public $timestamps = false; // if there are not timestamp table
I think it's work