How to Build a Property Listing App with Laravel

Property Listing App Laravel

Creating a property listing app with Laravel can be a game-changer for real estate professionals and developers alike. Whether you’re aiming to streamline the property search process or provide a platform for real estate agents to list properties, Laravel offers the perfect blend of flexibility and power. In this guide, we will walk you through the essential steps to build a robust property listing app with Laravel.

Table of Contents

Why Choose Laravel for Your Property Listing App?

When it comes to developing a property listing app, Laravel stands out for several reasons. Laravel is known for its elegant syntax, powerful tools, and a strong ecosystem that can help you create scalable and maintainable applications.

Top Features of Laravel for Property Listing Apps

Laravel comes packed with features that make it ideal for building property listing apps. Here are a few:

  1. Eloquent ORM: Simplifies database operations, making it easy to interact with your database.
  2. Blade Templating Engine: Provides a clean way to create dynamic views.
  3. Middleware: Ensures your app’s security and functionality through request filtering.
  4. Artisan CLI: A powerful command-line interface that simplifies development tasks.

Setting Up Your Development Environment

Before diving into the development of your property listing app, you need to set up your development environment. Here’s a step-by-step guide:

Installing Laravel

First, ensure you have Composer installed on your machine. Composer is a dependency manager for PHP, which you will use to install Laravel.

				
					composer create-project --prefer-dist laravel/laravel property-listing-app

				
			

Configuring Your Database

Laravel supports several databases, but for simplicity, we’ll use MySQL. Configure your .env file with your database credentials:

				
					DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=property_listing
DB_USERNAME=root
DB_PASSWORD=

				
			

Designing the Database Schema

A well-designed database is crucial for any property listing app. You’ll need tables for users, properties, and property details. Here’s a basic schema:

 

Users Table

ColumnTypeDescription
idIntegerPrimary key
nameStringName of the user
emailStringUser’s email address
passwordStringUser’s password
created_atTimestampTimestamp of creation
updated_atTimestampTimestamp of update

 

Properties Table

ColumnTypeDescription
idIntegerPrimary key
user_idIntegerForeign key referencing Users
titleStringTitle of the property
descriptionTextDetailed description of property
priceDecimalPrice of the property
locationStringLocation of the property
created_atTimestampTimestamp of creation
updated_atTimestampTimestamp of update

 

Property Details Table

ColumnTypeDescription
idIntegerPrimary key
property_idIntegerForeign key referencing Properties
bedroomsIntegerNumber of bedrooms
bathroomsIntegerNumber of bathrooms
sizeIntegerSize in square feet
amenitiesJSONList of amenities in JSON format
created_atTimestampTimestamp of creation
updated_atTimestampTimestamp of update

Creating Models and Migrations

Laravel’s Eloquent ORM makes it easy to work with your database. Create models and migrations for your tables:

				
					php artisan make:model User -m
php artisan make:model Property -m
php artisan make:model PropertyDetail -m

				
			

Defining Relationships

Define the relationships between your models in the model files:

				
					// User.php
public function properties()
{
    return $this->hasMany(Property::class);
}

// Property.php
public function user()
{
    return $this->belongsTo(User::class);
}

public function details()
{
    return $this->hasOne(PropertyDetail::class);
}

// PropertyDetail.php
public function property()
{
    return $this->belongsTo(Property::class);
}

				
			

Building the User Interface

A user-friendly interface is key to the success of your property listing app. Laravel’s Blade templating engine makes it easy to create clean and dynamic views.

Creating the Home Page

Your home page should feature a search bar, featured properties, and a navigation menu. Here’s a simple Blade template:

				
					<!-- resources/views/home.blade.php -->
<!DOCTYPE html>
<html>
<head>
    <title>Property Listing App</title>
</head>
<body>
    <h1>Welcome to Property Listing App</h1>
    <form action="/search" method="GET">
        <input type="text" name="query" placeholder="Search properties">
        <button type="submit">Search</button>
    </form>
    <h2>Featured Properties</h2>
    @foreach ($properties as $property)
        <div>
            <h3>{{ $property->title }}</h3>
            <p>{{ $property->description }}</p>
            <p>{{ $property->price }}</p>
        </div>
    @endforeach
</body>
</html>

				
			

Adding Navigation

To improve user experience, add navigation links to various sections of your app:

				
					<!-- resources/views/layouts/app.blade.php -->
<!DOCTYPE html>
<html>
<head>
    <title>@yield('title')</title>
</head>
<body>
    <nav>
        <a href="/">Home</a>
        <a href="/properties">All Properties</a>
        <a href="/properties/create">Add Property</a>
    </nav>
    <div class="content">
        @yield('content')
    </div>
</body>
</html>

				
			

Implementing Search Functionality

Search functionality is crucial for a property listing app. Users should be able to search for properties based on various criteria like location, price, and amenities.

Building the Search Controller

Create a search controller to handle search queries:
				
					php artisan make:controller SearchController
				
			
				
					// SearchController.php
public function search(Request $request)
{
    $query = $request->input('query');
    $properties = Property::where('title', 'like', "%$query%")
                          ->orWhere('location', 'like', "%$query%")
                          ->get();
    return view('search-results', compact('properties'));
}

				
			

Displaying Search Results

Create a Blade template to display search results:

				
					<!-- resources/views/search-results.blade.php -->
<!DOCTYPE html>
<html>
<head>
    <title>Search Results</title>
</head>
<body>
    <h1>Search Results</h1>
    @foreach ($properties as $property)
        <div>
            <h3>{{ $property->title }}</h3>
            <p>{{ $property->description }}</p>
            <p>{{ $property->price }}</p>
        </div>
    @endforeach
</body>
</html>

				
			

Adding Property Listings

Allow users to add property listings to the app. Create a form for submitting new properties:

Property Form

				
					<!-- resources/views/add-property.blade.php -->
<!DOCTYPE html>
<html>
<head>
    <title>Add Property</title>
</head>
<body>
    <h1>Add Property</h1>
    <form action="/properties" method="POST">
        @csrf
        <input type="text" name="title" placeholder="Title">
        <textarea name="description" placeholder="Description"></textarea>
        <input type="text" name="price" placeholder="Price">
        <input type="text" name="location" placeholder="Location">
        <button type="submit">Add Property</button>
    </form>
</body>
</html>

				
			

Handling Property Submissions

Create a controller method to handle property submissions:
				
					// PropertyController.php
public function store(Request $request)
{
    $property = new Property;
    $property->title = $request->title;
    $property->description = $request->description;
    $property->price = $request->price;
    $property->location = $request->location;
    $property->user_id = auth()->id();
    $property->save();

    return redirect('/');
}

				
			

Enhancing User Experience

To make your app more engaging, consider adding features like user profiles, property images, and reviews.

User Profiles

Allow users to create and edit their profiles. This will make the app more personalized and trustworthy.

				
					php artisan make:controller ProfileController
				
			
				
					// ProfileController.php
public function edit()
{
    return view('profile.edit', ['user' => auth()->user()]);
}

public function update(Request $request)
{
    $user = auth()->user();
    $user->name = $request->name;
    $user->email = $request->email;
    $user->save();

    return redirect('/profile')->with('status', 'Profile updated!');
}

				
			

Profile Edit Blade Template

				
					<!-- resources/views/profile/edit.blade.php -->
<!DOCTYPE html>
<html>
<head>
    <title>Edit Profile</title>
</head>
<body>
    <h1>Edit Profile</h1>
    <form action="/profile" method="POST">
        @csrf
        @method('PUT')
        <input type="text" name="name" value="{{ $user->name }}" placeholder="Name">
        <input type="email" name="email" value="{{ $user->email }}" placeholder="Email">
        <button type="submit">Update Profile</button>
    </form>
</body>
</html>

				
			

Property Images

Add the ability to upload and display property images. This enhances the visual appeal of your listings.

Image Upload Form

				
					<!-- resources/views/add-property.blade.php -->
<form action="/properties" method="POST" enctype="multipart/form-data">
    @csrf
    <input type="text" name="title" placeholder="Title">
    <textarea name="description" placeholder="Description"></textarea>
    <input type="text" name="price" placeholder="Price">
    <input type="text" name="location" placeholder="Location">
    <input type="file" name="image">
    <button type="submit">Add Property</button>
</form>

				
			

Handling Image Uploads

Update the store method in the PropertyController to handle image uploads:
				
					// PropertyController.php
public function store(Request $request)
{
    $property = new Property;
    $property->title = $request->title;
    $property->description = $request->description;
    $property->price = $request->price;
    $property->location = $request->location;
    $property->user_id = auth()->id();

    if ($request->hasFile('image')) {
        $path = $request->file('image')->store('images', 'public');
        $property->image_path = $path;
    }

    $property->save();

    return redirect('/');
}

				
			

Displaying Property Images

Update the home page template to display property images:
				
					<!-- resources/views/home.blade.php -->
@foreach ($properties as $property)
    <div>
        <h3>{{ $property->title }}</h3>
        <p>{{ $property->description }}</p>
        <p>{{ $property->price }}</p>
        @if ($property->image_path)
            <img decoding="async" src="{{ asset('storage/' . $property->image_path) }}" alt="Property Image">
        @endif
    </div>
@endforeach

				
			

Reviews and Ratings

Implement a review and rating system for properties. This helps users make informed decisions and adds credibility to your listings.

Reviews Table

ColumnTypeDescription
idIntegerPrimary key
property_idIntegerForeign key referencing Properties
user_idIntegerForeign key referencing Users
ratingIntegerRating out of 5
commentTextReview comment
created_atTimestampTimestamp of creation
updated_atTimestampTimestamp of update

Creating Reviews Model and Migration

				
					php artisan make:model Review -m

				
			
				
					// Migration file for reviews table
Schema::create('reviews', function (Blueprint $table) {
    $table->id();
    $table->foreignId('property_id')->constrained()->onDelete('cascade');
    $table->foreignId('user_id')->constrained()->onDelete('cascade');
    $table->integer('rating');
    $table->text('comment');
    $table->timestamps();
});

				
			

Defining Relationships

				
					// Property.php
public function reviews()
{
    return $this->hasMany(Review::class);
}

// Review.php
public function property()
{
    return $this->belongsTo(Property::class);
}

public function user()
{
    return $this->belongsTo(User::class);
}

				
			

Adding Review Form

				
					<!-- resources/views/property.blade.php -->
<h2>Reviews</h2>
<form action="/properties/{{ $property->id }}/reviews" method="POST">
    @csrf
    <input type="number" name="rating" min="1" max="5" placeholder="Rating (1-5)">
    <textarea name="comment" placeholder="Your review"></textarea>
    <button type="submit">Submit Review</button>
</form>

@foreach ($property->reviews as $review)
    <div>
        <p>Rating: {{ $review->rating }}</p>
        <p>Comment: {{ $review->comment }}</p>
        <p>By: {{ $review->user->name }}</p>
    </div>
@endforeach

				
			

Handling Review Submissions

Create a controller method to handle review submissions:
				
					php artisan make:controller ReviewController

				
			
				
					// ReviewController.php
public function store(Request $request, Property $property)
{
    $review = new Review;
    $review->property_id = $property->id;
    $review->user_id = auth()->id();
    $review->rating = $request->rating;
    $review->comment = $request->comment;
    $review->save();

    return redirect('/properties/' . $property->id);
}

				
			

Conclusion

Building a property listing app with Laravel is a rewarding project that combines creativity with technical skills. By following this guide, you can create a powerful, user-friendly app that meets the needs of both property seekers and real estate agents. Remember, the key to a successful app is continuous improvement and listening to user feedback. Happy coding!

Written By,

Picture of Md Monayem Islam

Md Monayem Islam

Hey, I'm Md Monayem Islam. I’m a Full Stack Developer with extensive expertise in Laravel (PHP), Vue.js (TypeScript), and API development. Over the years, I’ve honed my skills in building dynamic and scalable web applications. Previously, I worked on a variety of projects, creating robust solutions and enhancing the user experience for clients worldwide. Now, I’m here to share my knowledge and help you develop web applications.

Want a FREE Consultation?

I am here to assist with your queries. Schedule now!
Share the Post:

Let's Connect!

Have a question? Contact me and I’ll get back to you soon.

Do you Need a developer for your project?