Laravel Tip: Refactoring to lookup tables

March 7, 2021 · 2 min read

Laravel development with lookup tables
As Laravel developers, we often deal with messy code full of many if statements, especially in controllers. This mess of conditions makes it hard to keep the code clean and can make changes tough, especially when projects get more complicated. In this article, we’ll show you how to fix this common problem, using what we learned from a recent project. We’ll help you make your code better by using lookup tables, making it easier to read and take care of. Let’s make confusing code simple and work better.
Matching views to model types
On a recent project, we had to show three different views depending on the model type. At first, our code looked like this:
public function create(Report $report, $type)
{
if ($type == 1)
return view('items.inventory.create', ['report' => $report]);
elseif ($type == 2)
return view('items.stock.create', ['report' => $report]);
elseif ($type == 3)
return view('items.special.create', ['report' => $report]);
}While this code works, it’s not ideal. If we have to duplicate this logic in other methods, it will become a burden to refactor in the future.
The Solution: Introducing Constants in Our Model
The first step in our refactoring process is to introduce constants in our Item model. This makes the code more readable and easier to manage for the three different types. Here’s how we did it:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
use HasFactory;
const INVENTORY = 1;
const STOCK = 2;
const SPECIAL = 3;
}Now that we have the constants in our model file we can go back to the controller and introduce a new method that handles the correct routing based on the item type.
Refactoring the Controller
Inside our controller we need a new protected function that will return the route based on the type, this function will only accept a $type. Then, inside the route of our create a function in the controller we can replace all the if statements with a single line of code.
namespace App\Http\Controllers;
use App\Models\Report;
use App\Models\Item;
use Illuminate\Http\Request;
class ItemController extends Controller
{
public function create(Report $report, $type)
{
return view($this->filterItems($type), ['report' => $report]);
}
protected function filterItems($type)
{
$items = [
Item::INVENTORY => 'items.inventory.create',
Item::STOCK => 'items.stock.create',
Item::SPECIAL => 'items.special.create',
];
return $items[$type];
}
}And there you have it! Now, whenever we navigate to the create route with a specific type parameter, our function, filterItems, smartly figures out the type, be it 1, 2, or 3, and quickly takes us to the right view.
This method is really helpful when you’re dealing with tricky if/else statements and want to make your code easier to read. By adopting this technique, you can simplify complex logical processes and maintain a clean, elegant codebase.
Bring Your Ideas to Life 🚀
If you need help with a Laravel project let’s get in touch.
Lucky Media is proud to be recognized as a leading Laravel Development Agency
Technologies

Stay up-to-date
Be updated with all news, products and tips we share!

