dpzff20644 2015-02-26 13:24
浏览 36

尝试从数据表中删除行时,尝试在Laravel中获取非对象的属性

I think I know why the error is happening, I just am not sure how to fix it.

I have a datatable that has a edit and delete button for each row. The table contains the correct data. When I press the delete button for a row, I send the user to a delete confirm view. The error happens in the delete view.

This is the line that errors

<form id="deleteForm" class="form-horizontal" method="post" action="@if (isset($guestlist)){{ URL::to('entir/guests/' . $guestlist->id . '/delete') }}@endif" autocomplete="off">

I believe the issue is due to the $guestlist not being a object.

So in the controller I use the following function to get the data

public function getData()
{
   $guestlists = Guestlist::select(array('guestlists.id',  DB::raw('CONCAT(guestlists.first_name, " ", guestlists.last_name) AS g_name'),'guestlists.start_date', 'guestlists.end_date', 'guestlists.notes', 'guestlists.vendor'));

   return Datatables::of($guestlists)->add_column('actions', '<a href="{{{ URL::to(\'entir/guests/\' . $id . \'/edit\' ) }}}" class="iframe glyphicon glyphicon-edit"> </a>
       <a href="{{{ URL::to(\'entir/guests/\' . $id . \'/delete\' ) }}}" class="glyphicon glyphicon-remove"> </a>                            
        ')
        ->remove_column('id')            
        ->make();
}

Here is the function to call the delete view

public function getDelete($guestlist)
{       
       return View::make('site/entir/guests/delete', compact('guestlist'));

}

If I add dd($guestlist); before the return I can see that it does not contain the row data.

For example, lets say the row has a first name, last name and a few other fields. The id of this row is 10. When I review the output of the dd(); I only see this

string '10' (length=2)

I would have expected the object guestlist and all the row data. I am not sure why this is happening. I am using a laravel starter site (https://github.com/andrewelkins/Laravel-4-Bootstrap-Starter-Site) and I replicated what the author did for his datatable with delete button. When I add dd(); to his delete user function I see his object and the row data... I am puzzled.

Thanks

  • 写回答

1条回答 默认 最新

  • doudan5136 2015-02-26 17:19
    关注

    Your query doesn't fetch any data. Append ->get() to it.

    Guestlist::select(array('guestlists.id',  DB::raw('CONCAT(guestlists.first_name, " ", guestlists.last_name) AS g_name'),'guestlists.start_date', 'guestlists.end_date', 'guestlists.notes', 'guestlists.vendor'))->get();
    

    http://laravel.com/docs/5.0/queries

    If you need the results in an array, append ->get()->toArray().

    评论

报告相同问题?

悬赏问题

  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解