4

I'm just new in laravel. I want to know. how to return multiple data/value.

public function readItems() {
    $data1 = Data1::all ();
    $data = Data::all ();

    return $data;
}

I'm quite confuse how to do it. I don't to return it as a view, i just want to return only the data. i hope someone could help. thanks a lot..

chimichi004
  • 79
  • 2
  • 13

3 Answers3

6

You could return an array like :

return [data1, $data];

In the other side read it like :

$response = readItems();

$data1 = $response[0];
$data  = $response[1];
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
1

You can send data to view as like below :

return view('index', ['Data_One'=>$data, 'Data_Two'=>$data1]); 
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
The Coder
  • 11
  • 1
  • 5
0

you can return value like this

[
            'couponName' => $coupon->couponName,
            'couponCode' => $coupon->couponCode,
            'qty' => $coupon->qty,
            'startDate' => $coupon->startDate,
            'endDate' => $coupon->entDate,
            'userId' => $coupon->userId,
]