Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gitwithravish's avatar

Online Hotel Booking project - Need some logic building tips to prevent code duplication

Project: Online Hotel Booking portal Page: User dashboard. Here user can add property and details (basic details, rooms, amenities, etc.)

Problem : In the User Dashboard page, we ave several pages (Basic details, policies, room details, images, etc). One controller for each controller. For each controller, I am using a middleware VerifyPropertyUserMiddleware. Now I require fetching Property model in every single controller.

$ppt = PropertyMaster::findOrFail($pID);

Solutions that I can think of

  1. Create this instance in middleware and pass it to the controller. (but I think middlewares are not supposed to do this job)
  2. Create a trait, inside that trait create a method getProperty (but code duplication is still a problem as I will have to call the method in every controller)
  3. Use sessions. But still, code duplication is still there because in every controller I will have to check whether the laravel session has $ppt variable or not.
0 likes
2 replies
mushood's avatar
mushood
Best Answer
Level 41

Have a base controller

class BaseController {
protected $ppt = null;
public function __construct(Request $request)
{
if ($isset(request->pID)
    $this->ppt = PropertyMaster::findOrFail($request->pID);

//use request or session
}
}


class SubController {


public function __construct {
parent::__construct();
}

// use $this->ppt since it is inherited from base controller 
}
1 like
jlrdw's avatar

If you go to Google and punch in the following in fact you can simply cut and paste

site:laracasts.com hotel booking

You would not believe but there are several good past answers and discussions on this very topic.

Please or to participate in this conversation.