数组在localhost上工作正常,但在实时服务器上不工作(给出错误消息Undefined offset:0)-Laravel-5.8

时间:2019-06-10 22:31:54

标签: laravel-5.8

在localhost上一切正常,但是当迁移到godaddy实时服务器(cpanel)时,我在刀片视图中始终收到此错误(未定义偏移量:0)

我已经使用运行PHP 7.2.12的XAMPP在本地主机上测试了该应用程序,并且运行良好,但是现在我将其移至运行PHP 7.3的godaddy cpanel中,并且不断出现此错误

//这是我的路线 路线:: get('/ conversations','DoctorsController @ Conversations');

//这是我的控制器 公共功能对话(请求$ request){

    //authenticate user
    if($request->us == 'guest'){

        return redirect()->intended('login');

    }else{

    $unread=DB::table('messaging')
            ->where([
                    ['Reciever', Auth::user()->id],
                    ['ReadStatus', '=', '']
                    ])
            ->get();


    $pending=$unread->count();

    //retrieve previous chat;
    $conversations=DB::table('messaging')
                ->where('Sender', Auth::user()->id)
                ->orWhere('Reciever', Auth::user()->id)
                ->groupBy('Sender')
                ->orderBy('ReadStatus', 'asc')
                ->get();

    //retrieve profile of users in the previous chat
    $profiles = array();
    $read_status = array();
    foreach($conversations as $conversation){
    if($conversation->Sender == Auth::user()->id){

    //check user role to know which database to query
    $userRole=DB::table('role_user')
            ->where('user_id', $conversation->Reciever)
            ->get();

    if($userRole[0]->role_id === 2){

        #retrieve the sender details from doctors table
        $profile=DB::table('doctors')
                    ->where('doctor_id', $conversation->Reciever)
                    ->get();


    }else{

        //retrieve the sender details from users table
        $profile=DB::table('profiles')
                    ->where('user_id', $conversation->Reciever)
                    ->get();

    }


        if(in_array($profile, $profiles)){

        }else{
        array_push($profiles, $profile);

        }

        //retrieve the reciever details
    }else if($conversation->Reciever == Auth::user()->id){

        //check user role to know which database to query
        $userRole=DB::table('role_user')
                ->where('user_id', $conversation->Sender)
                ->get();

        if($userRole[0]->role_id === 2){

            $profile=DB::table('doctors')
                    ->where('doctor_id', $conversation->Sender)
                    ->get();


        }else{


            $profile=DB::table('profiles')
                    ->where('user_id', $conversation->Sender)
                    ->get();


        }

        //retrive unread chat;
            $unreadconvers=DB::table('messaging')
                ->select('ReadStatus')
                ->where([
                        ['Reciever', Auth::user()->id],
                        ['Sender', $conversation->Sender],
                        ['ReadStatus', '=', '']
                        ])
                ->get();


            if(in_array($profile, $profiles)){

            }else{
            $profile['unreads'] = $unreadconvers->count();
            array_push($profiles, $profile);
            //array_push($read_status, $unreadconvers->count());

            }


        }

        $i++;
    }

    return view('conversations')->with(['profile'=>$profiles, 'pending'=>$pending, 'unreads'=>$read_status]);
    //return to the conversation blade

    }
}

//这是我的Blade模板 @foreach(将$ profile作为$ profile)

      <div class="col-md-4 element-animate">
        <div class="media d-block media-custom text-center">
          <img src= "{{ URL::to(isset($profile[0]->image) ? $profile[0]->image : '../img/user.png') }}" alt="Image Placeholder" class="img-fluid img-fluid-doctors">
          <div class="media-body">
            <a href="{{ isset($profile[0]->doctor_id) ? url('/chat-doctor?db='.$profile[0]->doctor_id) : url('/chat-doctor?us='.$profile[0]->user_id)  }}" class="envelop"><i class="far fa-envelope"></i><span class="unread">{{ isset($profile['unreads']) ? $profile['unreads'] : 0 }}</span>
            <h3 class="mt-0 text-black">{{ $profile[0]->name }}</h3>
            </a>

          </div>
        </div>
      </div>

      @endforeach

在控制器上,该代码应从链接到登录用户的发送或接收的链接数据库中检索所有消息,使用数组存储它们,并在遍历每个数组的刀片模板中显示它们。 / p>

当前这是在localhost上执行的操作,但是在实时服务器上,我收到此错误消息未定义偏移量:0(查看:/resources/views/conversations.blade.php)

2 个答案:

答案 0 :(得分:0)

您正在覆盖您的foreach循环中的变量,因此,在第二次迭代中,它将在配置文件对象而不是原始数组中循环。

将您的控制器更改为:

'profiles' => $profiles,

并更改您的foreach以遍历$profiles

@foreach ($profiles as $profile)

然后将您的$profile[0]替换为$profile

答案 1 :(得分:0)

我找到了解决此问题的方法,我使用的是===而不是==我有此代码的地方

if($ userRole [0]-> role_id === 2)

我现在将这一行代码更改为

if($ userRole [0]-> role_id == 2)

现在运行良好。

谢谢您的回应秦亮。

相关问题