1 year ago

How can I optimize this code? LARAVEL 5.6

Posted 1 year ago by cac

Before I let the user fill a form I must verify a few things. I did a very simple verification but it doesn't seem quite right to me. I feel like there might be a better way to do it. First I thought of doing a bunch of if/else for each variable but it seemed very amateur. So I came up with this:

public function create()
        $payment_options = PaymentOption::all();
        $employees = Employee::all();
        $batches = Batch::all();
        $tanks = Tank::all();
        $buyers = Buyer::all();

        if (count($payment_options) < 1 || count($employees) < 1 ||
            count($batches) < 1 || count($tanks) < 1 || count($buyers) < 1) {
            $msg = 'Please make sure you have at least on of: payment options, 
            employees, batches, tanks and buyers';

            return view('home', compact('msg'));

        return view('sales.create', ['payment_options' => $payment_options,
            'employees' => $employees, 'batches' => $batches, 'tanks' => $tanks,
            'buyers' => $buyers]);


The problem of my solution is that it doesn't show the user which variable exactly failed the verification. How can I achieve that in an elegant manner?

Please sign in or create an account to participate in this conversation.