skip to Main Content

I am new to laravel 8 and blade syntax.
I am working on a project where users (agents) can upload apartments.
i have agent table with corresponding model relationship

agent model

public function property()
    {
        return $this->hasMany(Property::class, property_id, id);
    }

property model


 public function agents()
    {
    return $this->belongsTo(Agent::class, agent_id, id);

    }

I used breeze to build up my registration and login

i want when an agent is logged in, i will display in his dash board list of his properties.


@foreach(Auth::user()->properties as $property)
                                @if(($loop->count) > 0)
                                <td class="py-3 px-2">
                                    <div class="inline-flex space-x-3 items-center">
                                        <span>{{$property->propertyId}}</span>
                                    </div>
                                </td>

                                <td class="py-3 px-2">{{$property->building}}</td>
                                <td class="py-3 px-2">{{$property->rent->name}}</td>
@else
You have not uploaded any apartment
@endif
@endforeach

but i get error "Undefined variable $properties".

Meanwhile, {{ Auth::user()->othernames }} works fine.

AgentController

public function index()
    {
        return view ('dashboard', [
            'agent' => Agent::all(),
            'appointment'=>Appointment::all(),
            'properties'=>Property::all(),
            'schedule'=>AgentSchedule::all()


        ]);
    }

propertyController

public function create()
    {

        
        return view('pages.uploadapartment', [
            'states'=> State::all(),
            'areas'=>Area::all(),
            'buildings'=>building::all(),
            'buildingtypes'=>BuildingType::all(),
            'rentpaymentmethods'=>rentpaymentmethod::all(),
            'flattypes'=>flattype::all(),
        ]);

    }

2

Answers


  1. Chosen as BEST ANSWER

    I solved this problem by creating a controller for logged in agent. If the agent wants to see his property

    controller

    public function index()
         {
    
            
            
            return view('property.index', [
                'property'=>Property::where('agent_id', Auth::user()->id)->get() 
            ]);
         }
    

    blade view

     @foreach($property as $property)
                            
                            
                         <div>
                            
                                    {{$property->propertyId}}
                            
                            </div>
    @endforeach
    

  2. Since you are not using user model to define the relationship!

    Your relationship should be like this:

    //In your agent model
    public function properties()
    {
        return $this->hasMany(Property::class, 'property_id', 'id');
    }
    
    //In property model
    public function agent()
    {
        return $this->belongsTo(Agent::class, 'agent_id', 'id');
    }
    

    And when you want properties from a logged-in user, you grab them like this: $properties = Auth::user()->properties;

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