I have a function that returns data that I need to manipulate within the view. The data is linked from various tables based on relationships:
Function:
$auditResults = Audit::where('audit_id', $id)
->with('question', 'question.auditQuestion')
->get();
View:
#foreach($auditResults as $answer)
<p>{{$answer}}</p>
#endforeach
Output of $answer:
{
"id":1,
"audit_id":1,
"audit_questions_details_id":2,
"question":{
"id":2,
"audit_question_id":2,
"question_number":1,
"comment":1,
"header":0,
"created_at":"2017-03-27 12:16:50",
"updated_at":"2017-03-27 12:16:50",
"audit_question":{
"id":2,
"audit_detail_id":1,
"question":"THIS IS WHAT I WOULD LIKE TO OUTPUT",
"created_at":"2017-03-27 12:16:50",
"updated_at":"2017-03-27 12:16:50"
}
},
"score":null,
"comment":null,
"created_at":null,
"updated_at":null
}
Within the foreach loop, how can I output the question parameter of each result?
Many thanks.
<p>{{$answer->question->audit_question->question}}</p>
getting question number
<p>{{$answer->question->question_number}}</p>
Hope it helps
If $answer is really an JSON (string), so you must decode it first:
#foreach($auditResults as $answer)
<?php $answer = json_decode($answer) ?>
<!-- Now you have access to an PHP StdObject with your data -->
<p>{{$answer->question}}</p>
#endforeach
Do this:
{{ $answer->question->comment }}
Related
when I watched youtube Laravel From Scratch [Part 6] - Fetching Data With Eloquent , and I saw him pass data to view without using if statement and foreach, I has been tried but not working
public function show(todo $todo)
{
$todo=todo::find($todo);
return view('demo')->with('todo',$todo);
}
my view without if statement and foreach
#extends('layouts.app')
#section('content')
{{$todo->note}}
#endsection
my view when using if statement and foreach
#extends('layouts.app')
#section('content')
#if (count($todo) > 0)
#foreach ($todo as $item)
{{$item->note}}
#endforeach
#endif
#endsection
and I recievied an error
Property [note] does not exist on this collection instance
https://www.youtube.com/watch?v=emyIlJPxZr4&list=PLillGF-RfqbYhQsN5WMXy6VsDMKGadrJ-&index=6
The reason the property doesn't exist is because the result is a collection instead of an array (found in the op comments)
So you're trying to get note from an collection that looks like this:
[
{
"id":1,
"note":"to do one",
"created_at":"2020-04-12 08:25:00",
"updated_at":"2020-04-13 07:20:54",
"description":"description for todo one"
}
]
When you call $todo->note you're searching this line:
[
{ # <-- You're searching this line
"id":1,
"note":"to do one",
"created_at":"2020-04-12 08:25:00",
"updated_at":"2020-04-13 07:20:54",
"description":"description for todo one"
}
]
So your code is returning a collection instead of an array. An array would look like this:
{ # <-- Starts with open curly bracket instead of open square bracket
"id":1,
"note":"to do one",
"created_at":"2020-04-12 08:25:00",
"updated_at":"2020-04-13 07:20:54",
"description":"description for todo one"
}
You need to figure out why it's sending a collection.
From the look of your code, I find a potential issue with this:
public function show(todo $todo) # <- What is 'todo $todo'?
{
$todo=todo::find($todo);
return view('demo')->with('todo',$todo);
}
What is todo $todo, are you calling the show function somewhere? By default Laravel sends that ID via the web route. So try updating it to this:
public function show($id) #<-- change this to '$id'
{
$todo = Todo::find($id); #<-- Change this to '$id'
return view('demo')->with('todo',$todo);
}
Let me know if that resolves it.
Edit: And you really need to fix your capitalization.
$todo=todo::find($todo)->first();
I would like to retrieve an element by the id.
inside my blade file:
{{ $auctionStatuses }}
This outputs:
[
{
"id":1,
"name":"Awaiting customer action",
"created_at":"2018-10-04 10:14:08",
"updated_at":"2018-10-04 10:14:08"
},
{
"id":2,
"name":"Transfer in progress",
"created_at":"2018-10-04 10:15:11",
"updated_at":"2018-10-04 10:15:11"
},
{
"id":3,
"name":"Completed",
"created_at":"2018-10-04 10:15:14",
"updated_at":"2018-10-04 10:15:14"
}
]
I would like to output the name when referencing by the id field
Due to some data coming from mongodb, i can't use the standard relationships
I have tried this {{ $auctionStatuses['3'] }}
but that doesn't exist, obviously.
i guess ideally, i would like to do something like this
{{ $auctionStatuses[*]->id['3']->name }}
Is there a way to do what i need, without looping through the json array?
With one line:-
$auctionStatuses->toArray()[array_search(3,array_column($auctionStatuses->toArray(),'id'))]['name']
Explanation
toArray() Method converting a laravel collection to an array.
array_search Will searching for a array element and returning the index
array_column will returning values with specify column
You can use :
$auctionStatuses->firstWhere('id',3)->name;
https://laravel.com/docs/5.7/collections#method-first-where
I'm new to laravel
Just being curious,
Suppose I make an array in a view page
myView.blade.php
Suppose:
$array = [
1, 2, 3
];
in the same page, I want to loop it with "blade" foreach
suppose:
#foreach($array as $value)
<span id="{{$value}}">{{$value}}</span>
#endforeach
but, I get an error like this.
ErrorException in 1ed42d9dadecab7c54e086f573c4cbad6576e7c3.php line 63:
Trying to get property of non-object...
what's happened? what type of variable actually blade converts?
Any question will highly appreciated! :)
First of all, it's better if you just pass the data to your view files from controller.
But, if you still want to do that, and you are using Laravel 5.3, you can do it like this:
#php
$array = [
1, 2, 3
]
#endphp
and then loop it:
#foreach($array as $value)
<span id="{{$value}}">{{$value}}</span>
#endforeach
Make, sure that you declare the $array array before the loop.
Have a look at the Service Injection and View Composers on Larave's documentation aswell.
Actually, I don't know what do you want? But recommendly, you should init your array at controller, through it to view and foreach.
In controller,
return view('your view path', ['array' => $array]);
In blade
#foreach($array as $value)
<span id="{{$value}}">{{$value}}</span>
#endforeach
Hope it could help, thanks.
I am trying to figure out how I can give my data output in the blade file, the Laravel look;
like $data->name
But I can't get the output to be casted as an object. I think I have to make an array of the data before I can loop it proper in a foreach but this doesn't feel like the right way.
I am relatively new to Laravel and I want to do this the nice way, can someone point me in the right direction? Thanks in advance
Controller:
$data = collect($this->api->organization->index())->toArray();
return View::make('pages.organization.index', array('data' => $data[0]));
View:
#foreach($data as ((object)$organization))
{{ $organization->name }}
#endforeach
I know this will not work, but I think it illustrates my question a little bit.
EDIT
What I didn't realize is that $data = collect($this->api->organization->index()); is returning an array with all the data arrays inside because I didn't name it in my return like this:
return (object)['all' => $data];
After adding all I could reference the code inside my view like I wanted to. I know this is not a very detailed answer, if you run into the same problem message me I'll edit the answer.
Object:
$data = collect($this->api->organization->index());
#foreach($data as $organization))
{{ $organization->name }}
#endforeach
Array:
$data = collect($this->api->organization->index())->toArray();
#foreach($data as $organization))
{{ $organization['name'] }}
#endforeach
I'm working in Laravel 5 using Blade as motor of templates. I'm passing an array from the controller to the view, and I noticed that when I loop on it using the foreach clausule and the array is empty it gives error, exactly this:
Invalid argument supplied for foreach()
I had the same error in the controller and I fix it temporaly making:
if(count($student)!=0)
I said temporaly because I don't think it this the best way to do it.
The code in the controller is:
foreach($students as $student){
if(count($student->contracts)!=0)
foreach($student->contracts as $contract){
//something
}//end foreach
}//end foreach
I made some operations over the arrays, and then I send them to the view:
return view('myview')->with(['students'=>$students]);
The array is passing to the view correctly. I said is the foreach, beacause earlier I had the database full of registers and it worked fine, but now I have some students that doesn't have contracts and then I got that error. But, in the view I have the same error. So, it's normal? how could I fix it in a better way? why when the array is empty the foreach clausule gives that error?
PHP will not return that warning if the array contained at $student->contracts is empty. It will return it if it is of an invalid type (i.e. not an array).
Rather than checking the count() of $student->contracts, you'd be better to check if it's actually an array, as follows:
foreach($students as $student)
{
// Make sure that $student->contracts is actually an array (to bypass errors):
if( is_array($student->contracts) )
{
// Now loop through it:
foreach( $student->contracts as $contract)
{
// Do something here
}
}
}
Try this
$people = [
"Person A", "Person B", "Person C"
];
return view ('pages', compact('people'));
and loop through it like this:
#if (count($people))
<h3>People:</h3>
<ul>
#foreach($people as $person)
<li>{{ $person }}</li>
#endforeach
</ul>
#endif