我正在使用laravel 5.2,我想获得所选的雄辩模型的所有列(属性)名称,有没有办法做到这一点?
以下是我在控制器方法中的代码
if($request->method=="edit")
{
/*we are in edit mode details of given news ID should be attached*/
$curNews=News::find($request->newsID);
if($curNews==null)return back()->withErrors(["Unable to Edit News:No Data Found For Given News ID"]);
foreach((array)$curNews->attributes as $key=>$val)
{
$data[$key]=$val;
}
}
return view('adminarea.news.news-addedit',$data);
答案 0 :(得分:24)
$columns = Schema::getColumnListing('users'); // users table
dd($columns); // dump the result and die
答案 1 :(得分:6)
如果您想获取属性的名称,可以试试这个
$item = News::find($request->newsID);
$attributes = array_keys($item->getOriginal());
// or
$attributes = array_keys($item->getAttributes());
答案 2 :(得分:1)
不知道我是对的,但我已经克服了以下代码,并为我工作。
if($request->method=="edit")
{
/*we are in edit mode details of given news ID should be attached*/
$curNews=News::find($request->newsID);
if($curNews==null)return back()->withErrors(["Unable to Edit News:No Data Found For Given News ID"]);
foreach($curNews->toArray() as $key=>$val)
{
$data[$key]=$val;
}
}
return view('adminarea.news.news-addedit',$data);