35

I am using laravel 5.2 ,i want to get all the column (attribute) name of selected eloquent model,is there way to do this?

following is my code in controller method

    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);
Jatin Parmar
  • 2,759
  • 5
  • 20
  • 31

3 Answers3

78
$columns = Schema::getColumnListing('users'); // users table
dd($columns); // dump the result and die
Kuldeep Mishra
  • 3,846
  • 1
  • 21
  • 26
22

If you want get the names of your attributes, you can try this

$item = News::find($request->newsID);

    $attributes = array_keys($item->getOriginal());
// or 
$attributes = array_keys($item->getAttributes());
Pavel
  • 918
  • 7
  • 18
2

don't know i am right or not, but i have overcome with following code,and its working for me.

    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);
Jatin Parmar
  • 2,759
  • 5
  • 20
  • 31