0

I have around 50 objects in an array.

$user = User::with('abc','def','ghi','jkl','mno','pqr')
        ->where([['id', '<>', Auth::user()->id],['role', '<>', 2]])->simplePaginate(15);

$users = json_decode($user,true);

Have I placed ->simplePaginate(15) this correctly? Can I do json_decode after that? like I am doing? I dont get any error in controller.

In Views

    @section('content')
<div class="container-fluid">
<div></br></br></div>
@include('errors.list')

<div class="row">
  @foreach($users as $user)
   <a href="#" onclick=document.getElementById('{{$user->id}}').style.display='block'>
   <div class="col-md-3" style="margin-bottom:50px;">
   @if($user["search_status"] == 'matched')
         <div class="w3-card-12-disabled" style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;">
            @else
      <div class="w3-card-12" style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;">
      @endif
      <img align="center" src="{{$user->photo}}" style=" border-radius:50%; ; border-style: solid; border-color: #333; border-width: 5px; margin-left:15%;" width="150px !important" height="150px !important" />
      <h3 style="color:#000;  
    padding-top: 20px;" align="center">{{ $user->firstname}}</br>{{ $user->lastname}}</h3>

      <div style="display:flex; justify-content: center;">
      <div   class="tooltip2">
      <img  align="middle" style="" src="{{$user->natives->flag}}"/>
      <span class="tooltiptext">{{$user->natives->language}}</span>
      </div>
    </div>

    </br>
      </div>
   </div>     
   </a>    
   @endforeach



   <div class="row">
     @foreach($users as $user)
     <div id="{{$user->id}}" class="w3-modal">
         <div class="w3-modal-content w3-animate-top w3-card-8">
            <div id="div1" style="padding:40px 30px 30px 30px;">
               <img style="border-radius:50%; border:5px solid #333; margin-left:18px" src="{{$user->photo}}"  width="200px !important" height="200px !important">
               <div style="text-align: center; padding-top:70px;">
                  {!! Form::open(['url' => '/users/'. $user->id.'/reportc']) !!}
                  {!! Form::hidden('_method', 'POST') !!}
                  {!! Form::hidden('friend', $user->id, null, ['class'=>'form-control']) !!}   
                  {!! Form::submit('Report', ['class'=>'btn', 'style' => 'padding: 10px 10px 10px 10px; color:#000; border-color:#000;']) !!}
                  {!! Form::close() !!}
               </div>
               </div>
            <div id="div2" style="padding:40px 0px 40px 0px;">
            <span onclick=document.getElementById('{{$user->id}}').style.display='none'
                  class="w3-closebtn">&times;</span>

              <h1>{{ $user->firstname}} {{ $user->lastname}}</h1>
               </br>


               </br>
               <h5 style="padding-top:30px;"><b>About Me:</b> {{ $user->bio}} </h5>
               <h5 style="padding-top:10px;"><b>Campus:</b>  {{$user->campus->campus_name}}</h5>
               <h5 style="padding-top:10px;"><b>School:</b> {{ $user->schools->schools_name}} </h5>
               <h5 style="padding-top:10px;"><b>Year:</b> {{ $user->years->years_name}} </h5>
               <h5 style="padding-top:10px;"><b>Native Speaker of:</b> {{$user->natives->language}} </h5>
               <h5 style="padding-top:10px;"><b>Second Native Speaker of:</b> {{ $user->snatives->language }} </h5>

               <h5 style="padding-top:10px;"><b>Seeking Speaker of:</b>
               @foreach($seeks as $seek) 
                @if($seek->user_id === $user->id) 
                  {{ $seek->language}}, 
                @endif 
               @endforeach </h5>


            </div>
         </div>
      </div>
     @endforeach
   </div>

   {!! $users->links() !!}

</div>
</div>
@endsection

Even if I dont put this code {{ $users->links() }} in view I still get same error. I guess issue is with the pagination code on controller.

Edited

I have this small issue still:

Before

<h5 style="padding-top:10px;"><b>Campus:</b>  {{$user['campus']['campus_name']}}</h5>
               <h5 style="padding-top:10px;"><b>School:</b> {{ $user['schools']['schools_name']}} </h5>
               <h5 style="padding-top:10px;"><b>Year:</b> {{ $user['years']['years_name']}} </h5>
               <h5 style="padding-top:10px;"><b>Native Speaker of:</b> {{$user['natives']['language']}} </h5>
               <h5 style="padding-top:10px;"><b>Second Native Speaker of:</b> {{$user['snatives']['language']}} </h5>

               <h5 style="padding-top:10px;"><b>Seeking Speaker of:</b>
               @foreach($seeks as $seek) 
                @if($seek->user_id === $user['id']) 
                  {{ $seek->language}}, 
                @endif 
               @endforeach </h5>

After

 <h5 style="padding-top:10px;"><b>Campus:</b>  {{$user->campus->campus_name}}</h5>
               <h5 style="padding-top:10px;"><b>School:</b> {{ $user->schools->schools_name}} </h5>
               <h5 style="padding-top:10px;"><b>Year:</b> {{ $user->years->years_name}} </h5>
               <h5 style="padding-top:10px;"><b>Native Speaker of:</b> {{$user->natives->language}} </h5>
               <h5 style="padding-top:10px;"><b>Second Native Speaker of:</b> {{$user->snatives->language}} </h5>

               <h5 style="padding-top:10px;"><b>Seeking Speaker of:</b>
               @foreach($seeks as $seek) 
                @if($seek->user_id === $user->id) 
                  {{ $seek->language}}, 
                @endif 
               @endforeach </h5>

This gives After code gives me error trying to get property of non-object which is why I was using json_decode() and Before code. How do I fix these?

result

{  
   "per_page":12,
   "current_page":1,
   "next_page_url":"http:\/\/localhost:8000\/users\/community?page=2",
   "prev_page_url":null,
   "from":1,
   "to":12,
   "data":[  
      {  
         "id":5,
         "firstname":"Beth",
         "lastname":"Hanley",
         "email":"bah3@org.uk",
         "role":1,
         "photo":"uploads\/2.jpg",
         "bio":"Hockey fan, foodie, gamer, Saul Bass fan and HTML5 Guru.",
         "campus":{  
            "id":1,
            "campus_name":"name"
         },
         "school":"1",
         "year":"1",
         "native_lang":"2",
         "native_lang_flag":"2",
         "search_status":"available",
         "created_at":null,
         "updated_at":null,
         "lastlogin":null,
         "verification_code":null,
         "isverify":"0",
         "native_lang_2":null,
         "native_lang_flag_2":null,
         "schools":{  
            "id":1,
            "schools_name":"School of Energy, Geoscience, Infrastructure and Society"
         },
         "years":{  
            "id":1,
            "years_name":"First"
         },
         "flags":{  
            "id":2,
            "name":"\u00c5land Islands",
            "flag":"\/flags\/AX-32.png"
         },
         "natives":{  
            "id":2,
            "language":"English",
            "flag":"\/flags\/GB-32.png"
         },
         "snatives":null
      },

User Model

public function friends()
{
    return $this->hasMany('App\Friend','friend_id','id');
}

public function seeks()
{
    return $this->belongsToMany('App\Language');
}

public function natives()
{
    return $this->hasOne('App\Language','id','native_lang');
}

public function snatives()
{
    return $this->hasOne('App\Language','id','native_lang_2');
}

public function roles()
{
    return $this->hasOne('App\Role','id','role');
}

public function campus()
{
    return $this->hasOne('App\Campus','id','campus');
}

public function schools()
{
    return $this->hasOne('App\School','id','school');
}

public function years()
{
    return $this->hasOne('App\Year','id','year');
}

public function flags()
{
    return $this->hasOne('App\Flag','id','native_lang_flag');
}

Campus Model

public function users()
{
    return $this->belongsTo('App\User','campus','id');
}

Year Model

public function users()
{
    return $this->belongsTo('App\User','year','id');
}
Murlidhar Fichadia
  • 2,589
  • 6
  • 43
  • 93

1 Answers1

1

You don't need to json encode anything and access the object property directly.

Controller

$users = User::with('abc','def','ghi','jkl','mno','pqr')
    ->where([
        ['id', '<>', auth()->id()],
        ['role', '<>', 2]
    ])->simplePaginate(15);

return view('someview', compact('users'));

View

<div class="row">
    @foreach($users as $user)
        <a href="#" onclick="document.getElementById('{{ $user->id }}').style.display='block'">
            <div class="col-md-3" style="margin-bottom:50px;">
                @if($user->search_status == 'matched')
                    <div class="w3-card-12-disabled"
                         style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;">
                        @else
                            <div class="w3-card-12"
                                 style="border-radius:15px;width:225px; padding-top: 20px; background-color: #FFA534;">
                                @endif
                                <img align="center" src="{{ $user->photo }}"
                                     style=" border-radius:50%; ; border-style: solid; border-color: #333; border-width: 5px; margin-left:15%;"
                                     width="150px !important" height="150px !important"/>
                                <h3 style="color:#000;
    padding-top: 20px;" align="center">{{ $user->firstname }}<br/>{{ $user->lastname}}</h3>

                                <br/>
                            </div>
                    </div>
            </div>
        </a>
    @endforeach
</div>

{!! $users->links() !!}
Sandeesh
  • 11,486
  • 3
  • 31
  • 42
  • I have added more code. I changed code like this: $user->john->doe for $user['john']['doe'] but then it throws error. Trying to get property of non-object. That is the reason I went for json_decode. How do I fix this? – Murlidhar Fichadia May 29 '17 at 18:59
  • @MurlidharFichadia if you're eager loading the relation and they exist, this shouldn't happen. Can you run `dd($users);` before the return view and share the results – Sandeesh May 29 '17 at 19:01
  • I have added results. just one of the array objects. if you want me to add more objects, I will update it – Murlidhar Fichadia May 29 '17 at 19:08
  • @MurlidharFichadia it's enough, i see the issue. In this single object `"snatives":null`, but you're accessing `$user->snatives->language`. Therefore the error. You cannot directly try to access the properties of relationships unless they're available. You can add a check on related objects before accessing their property `{{ isset($user->snatives) ? $user->snatives->language : 'N/A' }}`. Just an example. You can set the default value to whatever you need. – Sandeesh May 29 '17 at 19:11
  • error is in this line: {{$user->campus->campus_name}} until forloop starts. I removed all the code below and this is the line that shows error. all the lines with two arrows has this error: $user->campus->campus_name – Murlidhar Fichadia May 29 '17 at 19:15
  • I have added 3 sample models – Murlidhar Fichadia May 29 '17 at 19:21
  • Can you share your complete view code? The second part of the code with issue which you added as an edit should be inside the foreach loop of the first view code. If not `$user` object won't exist, only `$users` will. If you need this part of code to be defined separately then use another foreach loop on $users and display it. It's 1 am here and i'm off. If you don't get it working then i'll reply in the morning. – Sandeesh May 29 '17 at 19:25
  • I have added entire view code. you can take ur time to reply. the first part of the view is a profile card and below is another bunch of forloop code that is seen as popup when profile card is clicked. – Murlidhar Fichadia May 29 '17 at 19:34
  • I got my issue resolved I had campus field name in users table and the relation was called campus too. which was causing trouble loading. – Murlidhar Fichadia May 29 '17 at 21:06