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

Shuvoo's avatar

ErrorException Undefined variable: limit

not working limit and id ,, i want to insert is manually

  public function import(Request $request)
{
    $id= $request->id;
    $limit = $request->limit;
    $path = $request->file('file')->store('uploads');
    $products = (new FastExcel)->import('public/' . $path, function ($line) {
        return Data::create([

            'uname' => $line['name'],
            'username' => $line['username'],
            'limit' => $limit,
            'group_id' =>$id 
        ]);
    });


    flash(__('Your products imported successfully'))->success();
    return redirect()->route('products.index');
}
0 likes
3 replies
Nakov's avatar
Nakov
Best Answer
Level 73

When you want to use them in a callback function you have to pass them using use in PHP:

$id= $request->id;
$limit = $request->limit;
$path = $request->file('file')->store('uploads');
$products = (new FastExcel)->import('public/' . $path, function ($line) use ($id, $limit) {
        return Data::create([

            'uname' => $line['name'],
            'username' => $line['username'],
            'limit' => $limit,
            'group_id' =>$id 
        ]);
});
Nakov's avatar

@shuvoo happy to hear that :) Please mark the answer as "Best Answer" then :)

Please or to participate in this conversation.