posting data to view

Posted 1 year ago by Rymercyble

my controller

public function people()
    {
        $min_age = Request::has('min_age') ? Request::get('min_age') : null;
        $max_age = Request::has('max_age') ? Request::get('max_age') : null;

        if (isset($min_age) && isset($max_age))
        {
            $carbon = Carbon::today();
            $carbon_max = Carbon::today();
            $year_min = $carbon->year - $min_age;
            $year_max = $carbon_max->year - $max_age;
            $month = $carbon->month;
            $day = $carbon->day;
            $carbon_min = $carbon->setDate($year_min, $month, $day)->toDateString();
            $carbon_max = $carbon_max->setDate($year_max, $month, $day)->toDateString();
            $users = DB::table('users')->whereBetween('date_of_birth', [$carbon_min, $carbon_max])->get();
        }
        return view('people', compact('users'));
    }

my view

<?php foreach ($users as $user) { ?>
                <div class="people_container">
                    <a href="/people/<?php echo $user->nick_name; ?> "><?php echo $user->nick_name; ?></a><br>
                    <?php echo $user->first_name; ?>
                    <?php echo $user->last_name; ?>
                </div>
            <?php } ?>

it gives

ErrorException Undefined variable: users in foreach in view

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

Reply to

Use Markdown with GitHub-flavored code blocks.