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
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 have key and value datas. key is an array. i want to foreach this key data. I use laravel 5. my json_decoded array like :
Collection {#1288 ▼
#items: array:4 [
"{"id":1,"title":"abc","path":"abc-path"}" => 19
]
}
But i can not fetch key data like i wanted my code :
#foreach($trendings as $key => $value)
{{ $key->id }}
#endforeach
it gives ' Trying to get property of non-object ' error. but if write code like :
#foreach($trendings as $key => $value)
{{ $key }}
#endforeach
it gives me
{"id":1,"title":"abc","path":"abc-path"}
but i want them use in my html. how can i fetch them ?
In my opinion, view should not execute a json_decode.
I expect view to get already decoded variables, i think that if your data is not completly decoded something is missing before the view.
Of course i would rather work on fixing what's wrong before than patching your view.
I'm trying to get a specific value using an array key, but I can't seem to figure out how to make it work.
$Array = Array(
"key1" => "value1",
"key2" => "value2"
);
Let's say I want to get the value of "key1" only, and I return the array with the view.
return view("myview")->with("arraytoprint", $Array);
And I try it with blade but I get Trying to get property of non-object...
#foreach($arraytoprint as $arr)
{{ $arr->key1 }}
#endforeach
How can this be achieved?
The error tells you that you are trying to get property of non-object but you are passing in an array. The -> notation is used for accessing properties in objects, not arrays.
You access keys in PHP arrays using the square bracket notation instead, as below:
$arr['key1']
In your example you also have no need for the foreach loop if you want to access the keys directly, just simply do:
{{ $arraytoprint['key1'] }}
If you do want to loop over the values then you can just do the below:
#foreach($arraytoprint as $value)
{{ $value }}
#endforeach
And for completeness sake, if you want the keys too you can do:
#foreach($arraytoprint as $key => $value)
{{ $key }} : {{ $value }}
#endforeach
In Laravel 5, passing data to the view is now done like this :
return view("myview", ["arraytopoint"=>$Array]);
and access in blade view like this:
{{$arraytopoint['key1']}} inside #foreach loop
or test value of variable like this :
<?php print_r($arraytopoint['key1']); ?>
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 }}
I'm trying to figure out how I can structure my Laravel select menu so that it shows up as this for a final render. Has anyone done such a thing.
The location is a property of the arena object.
<option value="arena_id">Arena Name - Location</option>
{{ Form::select('arena_id', [ null => 'Please Select'] + $arenas, null, ['id' => 'arena_id']) }}
I asssume $arenas comes from something like Arena::where('foo', bar)->get(), but with get() you will get an instance of Illuminate\Database\Eloquent\Collection instead of an actual array which is what you want in Form::select.
So what you need to do is to use lists($field, $key), it will fetch you rows and return it as an array.
$arenas = Arena::where('foo', bar)->lists('name', 'id');
There is a code example here with some comments from users if you want to learn more.
You can use pluck function for getting the results as array
refer https://laravel.com/docs/5.1/collections#method-pluck
$select = $this->all()->pluck('title', 'id');
Then you can use below sample code for creating select box with selected option in blade template
{{ Form::select('name',$select,'selected option id',['class' => 'form-control']) }}