如何在laravel4中从数据库中获取最后一个id

时间:2015-06-30 10:00:08

标签: laravel-4

我正在尝试从数据库中获取值到我的网页。 我的数据库表 WysTeachermessage

id sendname  replyname     message    stop_id    start_id

1   x           y            hai           2          1

2   y           x            hai           2          1

我只希望来自此表的最后一条消息

id sendname    replyname   message    stop_id    start_id

2   y              x          hai           2          1

我正在使用控制器代码

$send_stop=['sendname'=>x,'replyname'=>y,'stop_id'=>2,'start_stop'=>1];
      $receiver_stop=['replyname'=>x,'sendname'=>y,'stop_id'=>2,'start_id'=>1];
      $teacherinboxes=WysTeachermessage::where($send_stop)
                                        ->orWhere($receiver_stop)
                                        ->orWhere('stop_id',0)
                                        ->orderBy('id','desc')
                                        ->get();

如何从数据库中获取最后一个id值。??

我的控制器代码

$adm_msg=array(); foreach($students as $student) { $send_stop=['student_id'=>$student->id,'receiver_id'=>$userid]; $receiver_stop=['student_id'=>$student->id,'sender_id'=>$userid]; $parent_messages=WysMessage::where($send_stop) ->orWhere($receiver_stop) ->orderBy('id','desc') ->first(); $adm_msg[$student->id] = $parent_messages['send_msg']; } $students=WysStudent::all(); $parent =WysParent::all(); $clss = DB::table('wys_classes') ->join('wys_classnames', 'wys_classes.cls', '=', 'wys_classnames.id') ->join('wys_divnames', 'wys_classes.divn', '=', 'wys_divnames.id') ->select('wys_classes.id', 'wys_classnames.classname', 'wys_divnames.divname') ->get(); $send_stop=['receiver_id'=>$userid,'parent_stop_id'=>1,'teacher_stop_id'=>0]; $receiver_stop=['sender_id'=>$userid,'parent_stop_id'=>0,'teacher_stop_id'=>0]; $recv_stop=['receiver_id'=>$userid,'parent_stop_id'=>0,'teacher_stop_id'=>0]; $send_admin=['sender_id'=>$userid,'parent_stop_id'=>1,'teacher_stop_id'=>0]; $parent_messages=WysMessage::where($send_stop) ->orWhere($receiver_stop) ->orWhere($send_admin) ->orWhere($recv_stop) ->orderBy('id','desc') ->get();

1 个答案:

答案 0 :(得分:0)

只需使用first()而不是get()

$teacherinboxes=WysTeachermessage::where($send_stop)
                                    ->orWhere($receiver_stop)
                                    ->orWhere('stop_id',0)
                                    ->orderBy('id','desc') 
                                    ->first(); // use first() instead of get()