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..

2
  • Do you mean to return them as JSON in an API? Commented Aug 14, 2018 at 11:44
  • I'm not using as an api. its just like an ajax. Right now. it pass correctly but my page only views the datas of the object. Commented Aug 14, 2018 at 13:02

3 Answers 3

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];
Sign up to request clarification or add additional context in comments.

7 Comments

I'm having issue to display it on my pages. It only view the object by json string
Do you have always the problem bro ?
My object json string doens't load. i make a few changes
stackoverflow.com/q/51842364/4555497 this link. this is my issue..
pls take a look and see whats the problem.
|
1

You can send data to view as like below :

return view('index', ['Data_One'=>$data, 'Data_Two'=>$data1]); 

1 Comment

I don't to return it as a view, i just want to return only the data
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,
]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.