I am sure is easy but I can't figure it out!
array(2) {
[0]=>
object(stdClass)#354 (7) {
["id"]=>
int(1)
["id_user"]=>
int(1)
["DATE_FORMAT(questions.date, '%Y-%m-%d')"]=>
string(10) "2020-04-09"
["title"]=>
string(16) "Prueba del dia 9"
["description"]=>
string(28) "Prueba del dia 9 descripcion"
["first_name"]=>
string(6) "Pinazo"
["last_name"]=>
string(4) "Guna"
}
[1]=>
object(stdClass)#351 (7) {
["id"]=>
int(1)
["id_user"]=>
int(1)
["DATE_FORMAT(questions.date, '%Y-%m-%d')"]=>
string(10) "2020-04-09"
["title"]=>
string(17) "Prueba del dia 92"
["description"]=>
string(29) "Prueba del dia 92 descripcion"
["first_name"]=>
string(6) "Pinazo"
["last_name"]=>
string(4) "Guna"
}
}
I want to do a foreach and get all the values from each object.
@foreach($preguntas as $pregunta)
nvalid argument supplied for foreach()
I am doing this and it doesn't work.
This is the code in the php
$preguntas = Question::select('questions.id', 'questions.id_user', DB::raw("DATE_FORMAT(questions.date, '%Y-%m-%d')"), 'questions.title', 'questions.description', 'users.id', 'users.first_name', 'users.last_name')
->join('users', 'users.id', 'questions.id_user')
->whereRaw("DATE(questions.date) = CURDATE()")
->get()
->skip($saltar)
->take($cantidad)
->toJson();
return view('preguntasRespuestas')->with('preguntas', json_decode($preguntas));
Thank you for your time! I return preguntas to the view preguntasRespuestas
maybe try this? you do not need to do tojson and decode json, laravel does it for you.
$preguntas = Question::select('questions.id', 'questions.id_user', DB::raw("DATE_FORMAT(questions.date, '%Y-%m-%d')"), 'questions.title', 'questions.description', 'users.id', 'users.first_name', 'users.last_name')
->join('users', 'users.id', 'questions.id_user')
->whereRaw("DATE(questions.date) = CURDATE()")
->get()
->skip($saltar)
->take($cantidad)
->all();
return view('preguntasRespuestas')->with('preguntas', $preguntas);
Ok, lets say it is like this:
$first = '{"id":1,"title":"Prueba del dia 1","description":"Prueba del dia 1 descripcion"}';
$second = '{"id":2,"title":"Prueba del dia 2","description":"Prueba del dia 2 descripcion"}';
So $perguntas would look like this:
$perguntas =
[
json_decode($first),
json_decode($second)
];
And foreach:
foreach($perguntas as $perg) {
print_r($perg->id);
echo "<br>";
print_r($perg->title);
echo "<br>";
print_r($perg->description);
echo "<br>";
echo "<br>";
}
Result:
1
Prueba del dia 1
Prueba del dia 1 descripcion
2
Prueba del dia 2
Prueba del dia 2 descripcion
Also with your query you are returning an object, not an array, so either you later cast it as array (array)$perguntas
or:
$preguntas = Question::select('questions.id', 'questions.id_user', DB::raw("DATE_FORMAT(questions.date, '%Y-%m-%d')"), 'questions.title', 'questions.description', 'users.id', 'users.first_name', 'users.last_name')
->join('users', 'users.id', 'questions.id_user')
->whereRaw("DATE(questions.date) = CURDATE()")
->get()
->skip($saltar)
->take($cantidad)
->toJson();
return view('preguntasRespuestas')->with('preguntas', json_decode($preguntas, true));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.