2015-06-30 37 views
0

我試圖從數據庫中提取值到我的網頁。 我的數據庫表WysTeachermessage如何在laravel4中從數據庫中獲取最後一個ID

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();

回答

0

只需使用第一個()而不是get()方法

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

我還以爲代碼,但它不是工作..因爲sendname(X)發送味精另一個(z)stop_id = 0,如果使用此代碼不起作用。 – cnbwd

+0

我的完整控制器代碼是上面..請檢查並幫助我.. – cnbwd

+0

in $ send_stop,您在那裏寫了'start_stop',這是一個錯字嗎?或者這應該是'start_id'而不是?你能否提供更多解釋什麼是stop_id和start_id? –

相關問題