skip to Main Content

Everything works perfectly okay on localhost but when migrated to godaddy live server(cpanel) I keep getting this error (Undefined offset: 0) on my blade view

I have tested the application on my localhost using XAMPP running PHP 7.2.12 and it works very fine but now I moved it to godaddy cpanel running PHP 7.3 and it keeps giving me this error

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

//This is my Controller
public function Conversations(Request $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

    }
}

//This is my Blade template
@foreach($profile as $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

At the Controller, this code is expected to retrieve all the messages from the database linking to the logged in user either send or received, store them using an array and display them at the blade template looping through each of the array.

Currently that is what it does on localhost but on live server I get this error message Undefined offset: 0 (View: /resources/views/conversations.blade.php)

2

Answers


  1. Chosen as BEST ANSWER

    I have found the solution to this issue, I was using === instead of == where I have this code

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

    I now change this line of code to

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

    and now is working perfectly well.

    Thank you for your response Chin Leung.


  2. You are overwriting the variable in your foreach loop, therefore on the second iteration, it’s looping in the profile object instead of your original array.

    Change your controller to:

    'profiles' => $profiles,
    

    And change your foreach to loop through $profiles instead:

    @foreach ($profiles as $profile)
    

    And replace your $profile[0] with $profile.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search