How can I print variable in twig - php

How can I Use {{dump(variable_name)}}
Here if result is a variable
<pre>
{{dump(result)}}
</pre>

php
->get('field_example') and ->field_example
are the same and so is TWIG
.get('field_example') and .field_example
both get the field object (field item list) from which you can get the field value(s).
{{ content.field_example }}
hope answer the questions

Related

Impossible to access an attribute on a integer variable

I am trying to access the value in twig from:
image of data dump
But keep getting an error:
Impossible to access an attribute ("startDate") on a integer variable ("2").
And in my twig it's:
{% for item in data %}
...
{{ item.decoded.startDate }}
...
In Controller:
foreach ($data as $d) {
$d->decoded = json_decode($d->getValue());
}
Can somebody help skipping the error when the value is integer or null?
Try to dump item.decoded to see if startDate is available

Twig - getting text filed key and value

I hav text filed where I stored some json data.
I am getting in my twig template on dump like:
"{"birthDate":"2021-09-01", "amount":8}"
I want to access keys so I can extract it's values.
Tried with json_encode or:
{% for key, value in item.value %}
but nothing seems to work.
Is the problem with the output data?
It seems that your item is neither object nor array it seems to be a string.
Try to convert your data to object or array json as below:
$objJson = json_decode($yourDBArray['menu']);
$arrJson = json_decode($yourDBArray['menu'],true);
Then you can use to show as below
{% for key,value in menu %}
Key : {{ key }}
Value : {{ value }}
{% endfor %}

Empty array don't appear as empty in Laravel blade

Right now I'm trying to check when the view receives an empty array.
#if(! empty($array))
// Section content goes here...
#foreach($array as $value)
// All table data goes here...
#endforeach
#endif
The code as it is above seems to still run when the $array is empty and causes an exception.
When I try to dump the array with {{ dd($array) }} I get $array = [].
Sounds like you might be having a collection. You could simply do count($array) to check amount of records in the array. It would look something like this:
#if(count($array))
// Section content goes here...
#foreach($array as $value)
// All table data goes here...
#endforeach
#endif
The section should now be hidden. If you wish to only skip the foreach when there is nothing in the array you could do this:
// Section content goes here...
#forelse($array as $value)
// All table data goes here...
#empty
// Optional message if it's empty
#endforelse
Which would output the section content and check if the array has any values before it foreach it.
You can read more about loops within blade files in the Laravel documentation.
Is it possible your array is a collection?
Try using a #forelse, this will check if the array or collection is empty and display another block instead. For example:
#forelse($array as $value)
{{ $value }}
#empty
array is empty
#endforelse

Twig - Use variable key for object

I am using Twig and I have a problem.
I have a problem when I want to use a variable index for an object.
Here is my code:
{% for label, field in params.fields %}
{{ dump(data.field) }}
{% endfor %}
data is an object containing {'email': 'test#test.fr', 'name': 'John'}.
Field is an array of string containing ['email', 'name']
I can't show the value my object dynamically.
{{ dump(data.email) }} works.
How can I use dynamic indexes?
In Twig syntax, data.field is equal to $data['field'] in PHP. In other words, Twig use field as the array key name instead of taking the value of the field variable and use it as a key name.
If you want something similar to $data[$field], you can use the attribute() function:
The attribute function can be used to access a "dynamic" attribute of a variable:
Example:
{{ dump(attribute(data, field)) }}
{# or simply #}
{{ attribute(data, field) }}

How to display textbox value dynamically which is get from database using laravel

I got value from database and then i need to set those value to textbox. I have created a controller file with the method name of edit look like below
userdata.blade.php:
public function edit($id)
{
echo "You have clicked edit link".$name;
$editdata = DB::table('newuser')->where('Id','=',$id)->get();
return View::make('editdata',array('list' => $editdata));
}
I have passed array of value as parameter to the view file.now i need to diaplay the value of name to textbox.how to do that in html page using laravel. My html page look like below
editdata.blade.php:
<html>
<head></head>
<body>
<div>
{{Form::open(array('url' => 'login', 'method' => 'post'))}}
{{Form::label('name','Name',array('id'=>'label-name'))}}
{{Form::text('name',{{$list->Name}}}}
{{ Form::close() }}
</div>
</body>
</html>
can anyone tell me that what mistake i did.Thanks in advance
Just remove the curly brackets, you are already "inside" PHP code and don't need them:
{{ Form::text('name',$list->Name) }}
Also you get a collection from your controller you probably want to do:
$editdata = DB::table('newuser')->where('Id','=',$id)->first();
Or even:
$editdata = DB::table('newuser')->find($id);
get() returns a collection (multiple rows) and not the model itself. You can use User::find($id) which gives you direct access to the model with the specified Id.
When not using eloquent just replace get() with first()

Categories