Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何利用用户和管理员开发多认证聊天系统_Php_Mysql_Laravel_Authentication_Chat - Fatal编程技术网

Php 如何利用用户和管理员开发多认证聊天系统

Php 如何利用用户和管理员开发多认证聊天系统,php,mysql,laravel,authentication,chat,Php,Mysql,Laravel,Authentication,Chat,我正在开发一个聊天系统,允许教师(管理员表)和学生(用户表)使用laravel的聊天功能登录并相互发送消息。 我能够开发学生之间的聊天系统,并且能够开发多个登录功能,这意味着教师可以作为教师登录,学生可以作为学生登录。 但是我不能开发教师和学生之间的聊天功能。 我如何开发这个? 很抱歉打扰你,但是如果你能帮助我,我将不胜感激!! 以下代码是实现聊天功能和学生之间多次登录时的代码 ~MsgController.php~ <?php namespace App\Http\Controller

我正在开发一个聊天系统,允许教师(管理员表)和学生(用户表)使用laravel的聊天功能登录并相互发送消息。 我能够开发学生之间的聊天系统,并且能够开发多个登录功能,这意味着教师可以作为教师登录,学生可以作为学生登录。 但是我不能开发教师和学生之间的聊天功能。 我如何开发这个? 很抱歉打扰你,但是如果你能帮助我,我将不胜感激!! 以下代码是实现聊天功能和学生之间多次登录时的代码

~MsgController.php~

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Msg;
use App\Chat;
use Auth;

class MsgController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'msg' => 'required',
            'c_id' => 'required',
        ]);
        $cu = Auth::user()->id;
        $msg = new Msg;
        $msg->msg = $request->msg;
        $msg->user_id = $cu;
        $msg->chat_id = $request->c_id;
        $msg->seen = 0;
        $msg->save();

        if (!empty($msg)){
            $resp["status"] = 1;
            $resp['txt'] = "Successfully Create A New Msg";
            $resp['obj'] = $msg;

            $c = Chat::find($request->c_id);
            $resp['count'] = count($c->msgs);
            $count = count($c->msgs);
            if ( $count > 1 ) {
                $resp['fst'] = 0;
            }else {
                $resp['fst'] = 1;
            }
        }else {
            $resp["status"] = 0;
            $resp['txt'] = "Something went wrong!";
        }
        return json_encode($resp);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


    public function message_list(Request $request)
    {
        $chat = Chat::find($request->c_id);
        if ($request->limit > 10) {

            $total = $chat->msgs()->orderBy("id", "desc")->get();

            if (count($total) < $request->limit + 10) {
                $resp['end'] = true;
            }

            $msgs = $chat->msgs()->take($request->limit)->skip($request->limit - 10)->orderBy("id","desc")->get();
        }else {
            $msgs = $chat->msgs()->take($request->limit)->orderBy("id","desc")->get();
        }
        $me = Auth::user();

        if (!isset($resp['end'])) {
            $resp['end'] = false;
        }

        $resp['status'] = 1;
        $resp['txt'] = (string) view('layouts.msg_list', compact("msgs", "me"));
        return json_encode($resp);
    }


    public function new_message_list(Request $request)
    {
        $chat = Chat::find($request->c_id);
        $me = Auth::user();
        if ($request->me == 1) {
            $msgs = $chat->msgs()->where('seen','=',0)->where('user_id','=',$me->id)->orderBy("id","desc")->take(1)->get();
        }else {
            $msgs = $chat->msgs()->where('seen','=',0)->where('user_id','<>',$me->id)->get();
        }
        if ( count($msgs) > 0){
            $resp['status'] = 1;
            $resp['txt'] = (string) view('layouts.msg_list', compact("msgs","me"));
        }else{
            $resp['status'] = 2;
        }

        
        return json_encode($resp);
    }
    

    public function message_seen(Request $request)
    {
        $ck = Msg::where('seen', '=', 0)->where('chat_id', '=', $request->c_id)->where('user_id','<>', Auth::user()->id)->update(['seen'=>1]);
        if ($ck) {
            $resp['status'] = 1;
        }else {
            $resp['status'] = 0;
        }
        return json_encode($resp);
    }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Chat;
use App\User;
use Auth;


class ChatController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'users' => "required"
        ]);

        $chk_chats = Auth::user()->chats;

        $already_exist = "";
        foreach ($chk_chats as $ct) {
            $un = [];
            $already_exist = false;
            foreach ($ct->users as $u) {
                if (Auth::user()->id != $u->id) {
                    $un[] = (string)$u->id;
                }
            }

            if ($request->users == $un) {
                $already_exist = true;
                break;
            }else{
                $already_exist = false;
            }
        }

        if (!$already_exist) {
            $chat = new Chat;
            $chat->user_id = Auth::user()->id;
            $chat->save();
            $chat->users()->attach(Auth::user()->id);
            foreach ($request->users as $id) {
                $chat->users()->attach($id);
            }
            if (!empty($chat)) {
                $resp['status'] = 1;
                $resp['txt'] = "Successfully Created A New Chat";
                $resp['obj'] = $chat;
                $resp['objusers'] = $chat->users;
            }else{
                $resp['status'] = 0;
                $resp['txt'] = "Something went wrong!";
            }
        }else{
            $resp['status'] = 0;
            $resp['txt'] = "Chat Already Exist!";
        }

        return json_encode($resp);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.

     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function chat_update()
    {
        $chats = Auth::user()->chats()->orderby('id','desc')->get();
        $total_msg = Chat::chat_update($chats);

        return json_encode($total_msg);
    }

    public function chat_list()
    {   
        $chats = Auth::user()->chats()->orderby('id','desc')->get();
        $me = Auth::user();
        $total_msg = Chat::chat_update($chats);
        $resp['status'] = 1;
        $resp['txt'] = (string) view('layouts.chat_list', compact("chats","me",'total_msg'));
        return json_encode($resp);
    }
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Activechat;
use App\User;
use Auth;


class ActiveChatController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'c_id' => 'required'
        ]);
        $cu = Auth::user()->id;
        $chk = Activechat::where('user_id','=',$cu)->first();
        if ( !empty($chk)) {
            $chk->chat_id = $request->c_id;
            $chk->save();
        }else{
            $active = new ActiveChat;
            $active->chat_id = $request->c_id;
            $active->user_id = $cu;
            $active->typing = false;
            $active->save();
        }

        if (!empty($active) || !empty($chk)) {
            $resp['status'] = 1;
            $resp['txt'] = "Success";
        }else {
            $resp['status'] = 0;
            $resp['txt'] = "Something went wrong!";
        }

        return json_encode($resp);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function set_active(Request $request)
    {
        $cu = Auth::user()->id;
        $chk = Activechat::where('user_id','=',$cu)->first();

        if ( !empty($chk) ) {
            $chk->typing = $request->con;
            $chk->save();
        }

        if ( !empty($chk) ) {
            $resp['status'] = 1;
            $resp['con'] = $request->con;
        }else {
            $resp['status'] = 0;
        }

        return json_encode($resp);
    }



    public function check_active(Request $request)
    {
        $cu = Auth::user()->id;
        $chk = Activechat::where('chat_id','=',$request->c_id)->where('typing','=',1)->get();

        $usr = [];
        if ( count($chk) > 0 ) {
            foreach ($chk as $value) {
                $u = User::find($value->user_id);
                if ($u->id != $cu ) {
                    $usr[] = $u->name;
                }
            }

            if (count($usr) == 1 && $usr != Auth::user()->name && $usr != null ) {
                $resp['user_name'] = $usr;
                $resp['txt'] = 1;

            }elseif (count($usr) > 1 ) {
                $resp['user_name'] = implode(",", $usr);
                $resp['txt'] = 1;
            }else {
                $resp['txt'] = 0;
            }


        }else {
            $resp['txt'] = 0;
        }

        if ( count($usr) > 0 ) {
            $resp['status'] = 1;
        }else {
            $resp['status'] = 0;
            $resp['txt'] = "something went wrong!";
        }

        return json_encode($resp);
    }

}

如果你有一个学生之间的聊天功能,这与学生和老师之间的聊天有什么区别?我没有时间阅读您的整个代码库来找出它,您应该缩小确切的问题范围,只发布代码的这一部分。在什么方面它现在不起作用了?看看你们的数据库表,我看不出有任何东西表明学生和老师之间的区别。你在用户记录中有没有一列可以显示你在帖子中遗漏的差异?谢谢你的评论。在此聊天系统中,您可以选择与哪个人聊天。当我只有学生表时,学生和教师将一起显示在您选择人员的选择位置。为了避免这种情况,我必须有两张桌子。关于数据库表,它只为学生显示,因为我不知道如何更改表。为什么必须有两个表?只要有一个表示“学生”或“老师”的栏,并根据需要选择它们。请你仔细解释一下好吗?
<!DOCTYPE html>
<html lang="{{ config('app.locale') }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('app.name', 'KigyouDe') }}</title>

    <!-- Styles -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/css/bootstrap-select.min.css">
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    <link href="{{ asset('css/custom.css') }}" rel="stylesheet">

    <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
    <script src="{{ asset('js/app.js') }}"></script>
    <script src="{{ asset('js/bootstrap-select.js') }}"></script>
    <script src="{{ asset('js/custom.js') }}"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
        window.Laravel = {!! json_encode([
            'csrfToken' => csrf_token(),
        ]) !!};
    </script>
</head>
<body>
    <div id="app">
        <nav class="navbar navbar-default navbar-static-top">
            <div class="container">
                <div class="navbar-header">

                    <!-- Collapsed Hamburger -->
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
                        <span class="sr-only">Toggle Navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>

                    <!-- Branding Image -->
                    <a class="navbar-brand" href="{{ url('/') }}">
                        {{ config('app.name', 'Kigyoude') }}
                    </a>
                </div>

                <div class="collapse navbar-collapse" id="app-navbar-collapse">
                    <!-- Left Side Of Navbar -->
                    <ul class="nav navbar-nav">
                        &nbsp;
                    </ul>

                    <!-- Right Side Of Navbar -->
                    <ul class="nav navbar-nav navbar-right">

                        <h5>自分の名前をクリックしてプロフィール画像を設定してみよう!</h5>

                        <!-- Authentication Links -->
                        @if (Auth::guest())
                            <li><a href="{{ route('login') }}">Login</a></li>
                            <li><a href="{{ route('register') }}">Register</a></li>
                        @else
                            <li class="">
                                <button type="button" class="btn btn-primary navbar-btn" data-toggle="modal" data-target="#myModal">
                                        <span class="glyphicon glyphicon-plus font-weight-bold"></span> New Chat
                                </button>
                            </li>
                            <li>
                                <a href="{{ route('reset_password') }}">
                                    reset password
                                </a>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
                                    {{ Auth::user()->name }} <span class="caret"></span>
                                </a>
                                <ul class="dropdown-menu" role="menu">
                                    <li>
                                        <a href="{{ route('profile') }}">
                                            Profile
                                        </a>
                                    </li>
                                    <li>
                                        <a href="{{ route('logout') }}"
                                            onclick="event.preventDefault();
                                                    document.getElementById('logout-form').submit();">
                                            Logout
                                        </a>
                                        <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                            {{ csrf_field() }}
                                        </form>
                                    </li>
                                </ul>
                            </li>
                        @endif
                    </ul>
                </div>
            </div>
        </nav>

        @yield('content')
    </div>

@if(Auth::user()):
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Create New Chat</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <div class="create-chat-status"></div>
            <form id="create-form">
                {{ csrf_field() }}
                    <fieldset class="form-group">
                        <label for="formGroupExampleInput2">Select Users</label>
                        <select id="create-data" class="selectpicker" multiple>
                        @if(isset($users))
                            @foreach($users as $user)
                                <option value="{{$user->id}}">{{$user->name}}</option>
                            @endforeach
                        @endif
                        </select>
                    </fieldset>
                </form>
            </form>
          </div>
          <div class="modal-footer">
            <button data-deselectAllText="Deselect All" type="button" class="btn btn-secondary" data-dismiss="modal" id="close">Close</button>
            <button disabled type="button" class="btn btn-primary" id="create">Create</button>
          </div>
        </div>
      </div>
    </div>
@endif
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.2/js/bootstrap-select.min.js"></script>
</body>
</html>
@if( count($chats) > 0 )
    @foreach($chats as $chat)
    <div id="{{$chat->id}}" class="chat-item">
        @if( count($chat->users) > 2 )
            <img src="{{ asset('img/default-group.jpg') }}" class="img-circle img-responsive chat-item-img">
        @else
            <?php
                foreach ($chat->users as $u) {
                    if ($me->id !== $u->id) {
                        $img = $u->pic;
                        $id = $u->id;
                    }
                }
            ?>
            <img src="{{ asset('img/'. $img) }}" class="img-circle img-responsive chat-item-img">
            <a href="/show/{{$id}}">Check Profile</a>
        @endif
        <div class="chat-item-users">
            <?php
                $un = [];
                foreach ($chat->users as $u) {
                    if ($me->id !== $u->id) {
                        $un[] = $u->name;
                    }
                }
                $un = implode(", ", $un);
                echo ( strlen($un) > 17 ) ? substr($un, 0, 17) ."...":$un;
            ?>
        </div>
        <?php
            if ( array_key_exists($chat->id, $total_msg) ) {
                $c = ($total_msg[$chat->id] > 20 ) ? "20+" : $total_msg[$chat->id];
                echo "<div class='new-msg-count'>". $c ."</div>";
            }
        ?>

    </div>
    @endforeach
@else
    <div class="no-record text-center">No chat Exist</div>
@endif
@if( count($msgs) > 0 )
    @if( count($msgs) == 1 )
        @foreach($msgs as $msg)
            <div id="{{$msg->id}}" class="msg-item <?php echo ($msg->user_id == $me->id) ? 'me' : ''; ?>">
            
                    <img src="{{ asset('img/'.$msg->user->pic) }}" class="img-circle img-responsive msg-item-img">

                <div class="msg-item-txt">
                    {{$msg->msg}}
                <div class="msg-item-data">
                    @if( $msg->created_at->diffInHours(\Carbon\Carbon::now(), false ) > 24 )
                        {{$msg->created_at->format('d F Y h:i A') }}
                    @else
                        {{$msg->created_at->diffForHumans()}}
                    @endif
                </div>
                </div>
            </div>
        @endforeach
    @else
        @foreach($msgs->reverse() as $msg)
            <div id="{{$msg->id}}" class="msg-item <?php echo ($msg->user_id == $me->id) ? 'me' : ''; ?>">
            
                    <img src="{{ asset('img/'.$msg->user->pic) }}" class="img-circle img-responsive msg-item-img">

                <div class="msg-item-txt">
                    {{$msg->msg}}
                    
                <div class="msg-item-data">
                    @if( $msg->created_at->diffInHours(\Carbon\Carbon::now(), false ) > 24 )
                        {{$msg->created_at->format('d F Y h:i A') }}
                    @else
                        {{$msg->created_at->diffForHumans()}}
                    @endif
                </div>
                </div>
            </div>
        @endforeach
    @endif
@else
    <div class="no-record text-center">No Message Exist</div>
@endif
users table...id,name,email,self_production,pic,password,remember_token,created_at,updated_at
chats table...id,user_id,created_at,updated_at
activechats table...id,user_id,chat_id,typing
chat_user table...chat_id,user_id,created_at,updated_at
migrations table...id,migration,batch
msgs table...id,msg,chat_id,user_id,seen,updated_at,created_at
password_resets table...email,token,created_at