为返回 null 的 foreach() 视图提供的参数无效
Invalid argument supplied for foreach() view returning null
当我在名为“statuses”的模型中创建一个变量并在控制器中调用它时,我得到了数组数据。但是,当调用第二个变量时,它是 returning null 并在 blade.
中给我错误
模型中
/**
* Statuses.
*
* @var array
*/
protected $statuses = [
0 => 'InActive',
1 => 'Published',
2 => 'Draft',
3 => 'Scheduled',
];
/**
* Visibilities.
*
* @var array
*/
protected $visibilities = [
0 => 'Public',
1 => 'Private',
2 => 'Password Protected',
];
在控制器中
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$article = new Post();
$categories = Category::all();
$tags = Tag::all();
$statuses = $article->statuses;
$visibilities = $article->visibilities;
return view('posts.create', compact('categories','tags','statuses', 'visibilities'));
}
当我 dd($article)
我得到
当我 dd($statuses)
我得到
当我 dd($visibilities)
它 return 空的时候
而且我在 blade
中称呼它
@foreach($visibilities as $key => $value)
<option value="{{ $key }}" {{ (isset($post->visibility) && $post->visibility == $key) ? 'selected' : '' }} >{{$value}}</option>
@endforeach
我收到一条错误消息
Invalid argument supplied for foreach() (View:
我做错了什么才能获得 null return 可见性?
编辑:我正在粘贴我的关系特征
namespace App\Models\Traits\Attributes;
trait BlogAttributes
{
/**
* @return string
*/
public function getActionButtonsAttribute()
{
return '<div class="btn-group" role="group" aria-label="'.trans('labels.backend.access.users.user_actions').'">'.
$this->getEditButtonAttribute('edit-blog', 'admin.blogs.edit').
$this->getDeleteButtonAttribute('delete-blog', 'admin.blogs.destroy').
'</div>';
}
/**
* Get Display Status Attribute.
*
* @var string
*/
public function getDisplayStatusAttribute(): string
{
return $this->statuses[$this->status] ?? null;
}
/**
* Get Statuses Attribute.
*
* @var string
*/
public function getStatusesAttribute(): array
{
return $this->statuses;
}
/**
* Get Display Visibility Attribute.
*
* @var string
*/
public function getDisplayVisibilityAttribute(): string
{
return $this->visibilities[$this->visibility] ?? null;
}
/**
* Get Visibility Attribute.
*
* @var string
*/
public function getDisplayVisibilitiesAttribute(): array
{
return $this->visibilities;
}
}
感谢@lagbox 帮助解决了这个问题。
正在将 getDisplayVisibilitiesAttribute()
更改为 getVisibilitiesAttribute()
当我在名为“statuses”的模型中创建一个变量并在控制器中调用它时,我得到了数组数据。但是,当调用第二个变量时,它是 returning null 并在 blade.
中给我错误模型中
/**
* Statuses.
*
* @var array
*/
protected $statuses = [
0 => 'InActive',
1 => 'Published',
2 => 'Draft',
3 => 'Scheduled',
];
/**
* Visibilities.
*
* @var array
*/
protected $visibilities = [
0 => 'Public',
1 => 'Private',
2 => 'Password Protected',
];
在控制器中
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$article = new Post();
$categories = Category::all();
$tags = Tag::all();
$statuses = $article->statuses;
$visibilities = $article->visibilities;
return view('posts.create', compact('categories','tags','statuses', 'visibilities'));
}
当我 dd($article)
我得到
当我 dd($statuses)
我得到
当我 dd($visibilities)
它 return 空的时候
而且我在 blade
中称呼它 @foreach($visibilities as $key => $value)
<option value="{{ $key }}" {{ (isset($post->visibility) && $post->visibility == $key) ? 'selected' : '' }} >{{$value}}</option>
@endforeach
我收到一条错误消息
Invalid argument supplied for foreach() (View:
我做错了什么才能获得 null return 可见性?
编辑:我正在粘贴我的关系特征
namespace App\Models\Traits\Attributes;
trait BlogAttributes
{
/**
* @return string
*/
public function getActionButtonsAttribute()
{
return '<div class="btn-group" role="group" aria-label="'.trans('labels.backend.access.users.user_actions').'">'.
$this->getEditButtonAttribute('edit-blog', 'admin.blogs.edit').
$this->getDeleteButtonAttribute('delete-blog', 'admin.blogs.destroy').
'</div>';
}
/**
* Get Display Status Attribute.
*
* @var string
*/
public function getDisplayStatusAttribute(): string
{
return $this->statuses[$this->status] ?? null;
}
/**
* Get Statuses Attribute.
*
* @var string
*/
public function getStatusesAttribute(): array
{
return $this->statuses;
}
/**
* Get Display Visibility Attribute.
*
* @var string
*/
public function getDisplayVisibilityAttribute(): string
{
return $this->visibilities[$this->visibility] ?? null;
}
/**
* Get Visibility Attribute.
*
* @var string
*/
public function getDisplayVisibilitiesAttribute(): array
{
return $this->visibilities;
}
}
感谢@lagbox 帮助解决了这个问题。
正在将 getDisplayVisibilitiesAttribute()
更改为 getVisibilitiesAttribute()