数组在本地主机上工作正常但在实时服务器上不工作(给出错误消息未定义的偏移量:0)- Laravel-5.8

Array works fine on localhost but not working on live server (gives error message Undefined offset: 0) - Laravel-5.8

在本地主机上一切正常,但是当迁移到 Godaddy 实时服务器(cpanel)时,我在 blade 视图

上不断收到此错误(未定义的偏移量:0)

我已经在我的本地主机上使用 XAMPP 运行 PHP 7.2.12 测试了应用程序,它工作得很好,但现在我把它移到了 godaddy cpanel 运行 PHP 7.3,它一直给我这个错误

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

//这是我的控制器 public 函数对话(请求 $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

在控制器中,这段代码应该从数据库中检索所有发送或接收的链接到已登录用户的消息,使用数组存储它们并在 blade 模板循环显示它们每个数组。

目前这就是它在本地主机上所做的,但在实时服务器上我收到此错误消息 Undefined offset: 0 (View: /resources/views/conversations.blade.php)

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

将您的控制器更改为:

'profiles' => $profiles,

并更改您的 foreach 以循环遍历 $profiles

@foreach ($profiles as $profile)

并将你的 $profile[0] 替换为 $profile

我找到了这个问题的解决方案,我使用 === 而不是 == 我有这段代码

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

我现在把这行代码改成

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

现在运行良好。

感谢您的回复。