thinkphpÔõÑùʵÏÖ˽ÐŹ¦Ð§
Ëæ×ÅÉç½»ÍøÂçµÄÉú³¤£¬Ë½ÐŹ¦Ð§ÒѳÉΪÏÖ´úÉç½»ÍøÂç±Ø±¸µÄÒ»ÏЧ¡£ÔÚthinkphpÖУ¬ÊµÏÖ˽ÐŹ¦Ð§²¢²»ÄÑ£¬±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃthinkphp¿ò¼ÜÀ´ÊµÏÖ˽ÐŹ¦Ð§¡£
Ò»¡¢Êý¾Ý¿âÉè¼Æ
ΪÁËʵÏÖ˽ÐŹ¦Ð§£¬Ê×ÏÈÐèÒªÉè¼ÆÊý¾Ý¿â¡£ÎÒÃÇ¿ÉÒÔ½¨ÉèÁ½¸ö±í£¬»®·ÖΪusers±íºÍmessages±í¡£
users±íÖаüÀ¨Óû§µÄid¡¢Óû§Ãû¡¢ÃÜÂëµÈÐÅÏ¢£º
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
µÇ¼ºó¸´ÖÆ
messages±íÖаüÀ¨Ë½ÐŵÄid¡¢·¢ËÍÕߺÍÎüÊÕÕßµÄid¡¢Ë½ÐŵÄÄÚÈÝ¡¢·¢ËÍʱ¼äµÈÐÅÏ¢£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
CREATE TABLE `messages` ( `id` int(11) NOT NULL AUTO_INCREMENT, `sender_id` int(11) DEFAULT NULL, `receiver_id` int(11) DEFAULT NULL, `content` text, `created_at` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
µÇ¼ºó¸´ÖÆ
¶þ¡¢¿ØÖÆÆ÷
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔ×îÏÈʵÏÖ˽ÐŹ¦Ð§ÁË¡£Ê×ÏÈÒª½¨ÉèÒ»¸öMessages¿ØÖÆÆ÷£¬ÓÃÀ´´¦Öóͷ£Ë½ÐÅÏà¹ØµÄ²Ù×÷¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÌìÉúÒ»¸öMessages¿ØÖÆÆ÷£º
php think make:controller Messages
µÇ¼ºó¸´ÖÆ
ÔÚMessages¿ØÖÆÆ÷ÖУ¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÁ½¸öÒªÁ죺indexºÍsend¡£
indexÒªÁì
indexÒªÁìÓÃÀ´ÏÔʾÓû§µÄ˽ÐÅÁÐ±í£¬´úÂëÈçÏ£º
public function index() { $user = session('user'); $messages = Message::where('sender_id','=',$user->id) ->orWhere('receiver_id','=',$user->id) ->orderBy('created_at', 'desc') ->paginate(10); return view('messages.index', ['messages' => $messages]); }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÒªÁìÖУ¬ÎÒÃÇÊ×ÏÈ»ñÈ¡Ä¿½ñµÇÈÎÃü»§µÄÐÅÏ¢¡£È»ºó£¬ÅÌÎÊmessages±íÖÐËùÓз¢ËÍÕß»òÎüÊÕÕßΪĿ½ñÓû§µÄ˽ÐÅ£¬²¢×¼Ê±¼äµ¹Ðò¡¢·ÖÒ³µÄ·½·¨¾ÙÐÐÏÔʾ¡£
sendÒªÁì
sendÒªÁìÓÃÀ´·¢ËÍ˽ÐÅ£¬´úÂëÈçÏ£º
public function send(Request $request) { $content = $request->input('content'); $receiverId = $request->input('receiver_id'); $message = new Message(); $message->sender_id = session('user')->id; $message->receiver_id = $receiverId; $message->content = $content; $message->created_at = Carbon::now(); $result = $message->save(); return redirect('/messages'); }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÒªÁìÖУ¬ÎÒÃÇÊ×ÏÈ»ñÈ¡Óû§ÊäÈëµÄ˽ÐÅÄÚÈݺÍÎüÊÕÕßµÄid¡£È»ºó£¬½¨ÉèÒ»¸öеÄMessageʵÀý£¬ÉèÖ÷¢ËÍÕßµÄid¡¢ÎüÊÕÕßµÄid¡¢ÄÚÈݺͷ¢ËÍʱ¼ä£¬²¢½«Ë½ÐÅÉúÑĵ½Êý¾Ý¿âÖС£
Èý¡¢ÊÓͼ
×îºó£¬ÎÒÃÇÐèÒª½¨ÉèÁ½¸öÊÓͼ£ºmessages.indexºÍmessages.send¡£
messages.indexÊÓͼ
messages.indexÊÓͼÓÃÀ´ÏÔʾĿ½ñÓû§µÄËùÓÐ˽ÐÅ£¬´úÂëÈçÏ£º
@foreach($messages as $message) <div> <p>·¢ËÍÕߣº{{$message->sender->username}}</p> <p>ÎüÊÕÕߣº{{$message->receiver->username}}</p> <p>{{$message->content}}</p> <p>ʱ¼ä£º{{$message->created_at}}</p> </div> @endforeach {{$messages->links()}}
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÊÓͼÖУ¬ÎÒÃÇʹÓÃÁËÑ»·Óï¾äÀ´±éÀúËùÓÐ˽ÐÅ£¬²¢ÏÔʾ·¢ËÍÕß¡¢ÎüÊÕÕß¡¢ÄÚÈݺÍʱ¼äµÈÐÅÏ¢¡£ÔÚ×îºó£¬Ê¹Ó÷ÖÒ³º¯Êý$messages->links()À´ÏÔʾ·ÖÒ³Á´½Ó¡£
messages.sendÊÓͼ
messages.sendÊÓͼÓÃÀ´ÏÔʾ·¢ËÍ˽ÐÅµÄ±íµ¥£¬´úÂëÈçÏ£º
µÇ¼ºó¸´ÖÆ @csrf ÊÕ¼þÈË£º @foreach($users as $user) id}}”>{{$user->username}} @endforeach ˽ÐÅÄÚÈÝ£º ·¢ËÍ
ÔÚÕâ¸öÊÓͼÖУ¬ÎÒÃÇʹÓÃÁËÒ»¸öÏÂÀ¿òÀ´Ñ¡ÔñÎüÊÕÕߣ¬²¢½¨ÉèÁËÒ»¸öÎı¾¿òÀ´ÊäÈë˽ÐÅÄÚÈÝ¡£
ËÄ¡¢×ܽá
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇ¿ÉÒÔÔÚThinkPHP¿ò¼ÜÖÐʵÏÖ˽ÐŹ¦Ð§¡£ÔÚ²Ù×÷Àú³ÌÖÐҪעÖØÊý¾ÝÇå¾²ÎÊÌ⣬ÀýÈçÓû§ÊÇ·ñµÇ¼¡¢ÊÇ·ñÓÐȨ»á¼ûµÈ¡£Í¨¹ýºÃµÄÉè¼ÆºÍÇå¾²µÄʵÏÖ£¬Ë½ÐŹ¦Ð§Äܹ»¸üºÃµØÌá¸ßÉç½»ÍøÂçµÄÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇthinkphpÔõÑùʵÏÖ˽ÐŹ¦Ð§µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡