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
I solved this problem by creating a controller for logged in agent. If the agent wants to see his property
controller
blade view
Since you are not using user model to define the relationship!
Your relationship should be like this:
And when you want properties from a logged-in user, you grab them like this:
$properties = Auth::user()->properties;