mardi 20 mars 2018

I have properties assigned to specific watchlists. How can I get all properties - Laravel

I have a feature in my Web App for Watchlists. A user can create a watchlist, which is related to the user, once it has a status of active, the user can add properties to that specific watchlist. Users can have multiple watchlists, each with different properties. There is a index pages that displays the list, and a show page that displays the properties in a specific list.

I wan't to have a profile that displays all properties, from all of the users lists. Here is my code

This is the results Controller/View here the user can click on a property and it adds to a user watchlist. I have removed the searchlogic.

public function search(Request $request){
      //Wathlists Logic
      $inspirationsArray = Watchlists::where('user_id', Auth::id())->where('active', 1)->first();
      $filteredData = $inspirationsArray;
      if(count($inspirationsArray) >= 1) {
        $inspirationsArray = $inspirationsArray->properties;
        $arrayInfo = [];
        foreach($inspirationsArray as $image) {
          array_push($arrayInfo, $image->image_info) ;
        }
      }
      return view('pages/search/results', compact('properties', 'user', 'arrayInfo', 'filteredData'));
  }

This is the watchlist properties controller which adds to properties to the active watchlist

public function create(Request $request, $image_info){
  //Adds ad data to the active watchlist
  $Watchlist = Watchlists::where('user_id', Auth::id())->where('active', 1)->first();

  if($Watchlist == null){
    return redirect('watchlist/create');
  } else {
    $saveData = [
      "image_info" => $image_info,
      "image_url" => $request->image_url,
      "watchlists_id" => $Watchlist->id
    ];
    $inspiration = WatchedProperties::create($saveData);

    return back();
  }

}

This is the normal showpage which shows all the properties on one list.

              <div class="row">
                <!--
                  Getting all properties in a specific watchlists
                  WatchlistController - Show
                -->
                @foreach ($Watchlists->properties as $WatchedProperties)
                  <div class="col-md-4">
                    <div class="box">
                      <div style="position: relative; background: url('') no-repeat center center;-webkit-background-size: cover;-moz-background-size: cover;-o-background-size: cover;background-size: cover; height: 200px;"></div>
                    </div>
                  </div>
                @endforeach
              </div>
            </div>
          </div>
          <div class="col-md-2">
            <center>
              <a href="/watchlist//edit" class="edit-btn">Edit</a>
              <a href="/watchlist//delete" onclick="confirm()" class="delete-btn">Delete</a>
            </center>
          </div>
        </div>
      </div>
    </div>
  </div>
@endsection

For my account controller, I'm trying to get every property from all the tenants watchlist to display on their profile page

Here is the account controller, and here is the show page for the profile.

public function index(){
   //Authenticated different user types
   //Sends Landlord and Tenant to appropiate pages

   $user = Auth::user();
   $properties = PropertyAdvert::where('user_id', Auth::id())->get();
   $Watchlists = Watchlists::where('user_id', Auth::user()->id)->get();

   if($user->userType != "Landlord"){
     return view('/pages/account/tenant/index', compact('properties', 'user', 'Watchlists'));
   }
   else {
     return view('/pages/account/landlord/index', compact('properties', 'user'));
   }
}

Profile show page, same as watchlist show page

  <div class="row">
                <!--
                  Getting all properties in a specific watchlists
                  WatchlistController - Show
                -->
                @foreach ($Watchlists->properties as $WatchedProperties)
                  <div class="col-md-4">
                    <div class="box">
                      <div style="position: relative; background: url('') no-repeat center center;-webkit-background-size: cover;-moz-background-size: cover;-o-background-size: cover;background-size: cover; height: 200px;"></div>
                    </div>
                  </div>
                @endforeach
              </div>
            </div>
          </div>
          <div class="col-md-2">
            <center>
              <a href="/watchlist//edit" class="edit-btn">Edit</a>
              <a href="/watchlist//delete" onclick="confirm()" class="delete-btn">Delete</a>
            </center>

          </div>
        </div>

The errors i'm getting is

Property [properties] does not exist on this collection instance.

Thats the same for every variable when I comment on out. Any ideas?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2G3v1QT
via IFTTT

Aucun commentaire:

Enregistrer un commentaire