首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >阵列在本地主机上工作正常,但在实时服务器上工作不正常(给出错误消息Undefined offset: 0) - Laravel-5.8

阵列在本地主机上工作正常,但在实时服务器上工作不正常(给出错误消息Undefined offset: 0) - Laravel-5.8
EN

Stack Overflow用户
提问于 2019-06-11 06:31:55
回答 2查看 307关注 0票数 1

一切都在本地主机上运行得很好,但是当迁移到godaddy live server(cpanel)时,我的刀片视图上总是出现这个错误(未定义的偏移量:0

我使用运行PHP 7.2.12的XAMPP在我的本地主机上测试了这个应用程序,它运行得非常好,但现在我将它移动到运行PHP 7.3的godaddy cpanel,它一直给我这个错误

//This is my Route Route::get('/conversations','DoctorsController@Conversations');

//这是我的控制器公共函数会话(请求$request){

代码语言:javascript
复制
    //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

    }
}

//这是我的刀片模板@foreach($profile as $profile)

代码语言:javascript
复制
      <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

在控制器中,此代码预期从链接到登录用户的数据库中检索所有消息,无论是发送还是接收,使用数组存储它们,并在循环通过每个数组的刀片模板中显示它们。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-14 22:51:43

我已经找到了这个问题的解决方案,我使用的是===而不是==,因为我有下面的代码

if($userRole->role_id === 2)

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

if($userRole->role_id == 2)

现在运行得很好。

谢谢你的回复,梁展良。

票数 0
EN

Stack Overflow用户

发布于 2019-06-11 06:38:14

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

将控制器更改为:

代码语言:javascript
复制
'profiles' => $profiles,

并将foreach改为通过$profiles循环:

代码语言:javascript
复制
@foreach ($profiles as $profile)

$profile替换你的$profile[0]

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56534293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档