Show Dynamic image data to the img html element - Laravel, Collective - laravel

I am dealing with 2 variables here. 1st being $xyz which contains all the values I would like to bind to the form and 2nd being $fields which contains all the input fields.
I am looping through $fields->field_name to show all the form fields from the database.
But 1 of the fields being an image, which is a different case. I am simply trying to bind manually the image url to the img element here.
To successfully bind an image. I need 2 things, 1 being the field name which I can get it from $fields->field_name and second is the url which is in $xyz.
so, I plan to do something like: $xyz->get($fields->field_name). This means, I try to access $xyz object and get dynamic image field name. but not sure why my below code throws error:
<img src=" {{ url($xyz->get($fields->field_name)) }}" />
code like above works completely fine in Controller, not sure how to achieve this in Blade front-end Views.
error:
Type error: Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array, string given

I found the solution by myself:
below works, instead of using get:
{{ $xyz[$fields->field_name] }}

Related

How to get the property value in Laravel Blade that was transformed by Eloquent API Resource?

Currently, my solution to display an encoded ID is like that:
<p v-text='"Reservation code: "+ #json($orderJson).id'></p> //R. code: wYeyjo6l42
But I would prefer to use it like:
<p>#json($orderJson->id)</p> //but returns: 8 (not encoded)
How can I get the transformed attributes?
p.S. Yes I know it's used for API, but I'm even using it to handle objects to Vue via Blade.
Well, I dug up in the code and found out you can use resolve().
So one solution would be:
<p>Reservation code: {{ $orderJson->resolve()['id'] }}</p>

Laravel: variable as form value

First Laravel Project.
I want to make an "edit screen" where the "old values" are the predefined default value of the form input field.
I tried this:{{ Form::text('brand', '$product[0]->brand') }}
But I got back
$product[0]->brand
Instead of
Test brand
What I did wrong? What's the good syntax?
Usually, Laravel should reuse the last entry the user used if the form isn't validated as you can read here :
Also, please note that the value will first come from Flash Session Input, only secondly will the value argument be used. This means if your previous request was this form it will automatically display the value the user last entered.
But, if you want, can't you use something like
{{ Form::text('brand', $product[0]->brand) }}
Because you were saying you wanted that specific string by putting ' around your variable.
please use it:
{{Form::text('brand',null,array('class'=>'form-control'))}

pyrocms files plugin variables

I want to pull a url segment variable into the pyrocms file plugin call. it would look like
{{files:listing folder="[segment(2)]"}}
or something of the sort. What is the trick for embedding
{{url:segments..}}
inside
{{files:listing folder="…}}
I am trying to setup this up for a conditional query for a photo gallery
If you take a look at the PyroCMS Tags documentation you will see this clearly documented in the "Tag Attributes" section.
You may also use the output from other tags as attribute values in your tags. For example if you wanted the url segment to default to the slug of the currently viewed page you could do this:
{{ url:segments segment="1" default=page:slug }}
Here is an example showing the proper use of quotes and braces when the tag used as the attribute value has an attribute itself.
{{ url:segments segment="1" default={foo:bar value="baz"} }}
Tip: Omit quotes and braces when using tags as attribute values. The only exception is when the tag you are using as the attribute value has its own attributes.
So you can do that easily with:
{{ files:listing folder={url:segments segment="2"} }}
Basically you don't need to pretend it's a string if it's not. You can just send the foo:bar arguments through, but it if has attributes you can "group" the attributes with the call via a single { and }.
Simple right? :)

Printing repeated Django form fields individually

I have a Django forms dilemma. When rendering a form in a template, I usually prefer to print the form fields individually, in stead of using the form.as_p annotation. However, in the form I am working on I have a set of fields that are repeated an unknown number of times. For example, if there are 3 items being edited the fields would be:
field_name_1
field_email_1
field_name_2
field_email_2
field_name_3
field_email_3
But the number of items in not known to me beforehand. To solve this I am creating the fields in a loop in the init function, no problems there. But I can't figure out a way to print the fields individually in the template, since I don't know the whole name of each field. If this were my only problem I could just use the form.as_p syntax, no biggie. But I would also like to add some html to the beginning of each set of fields, so that the result would be:
Add values 1:
field_name_1
field_email_1
Add values 2:
field_name_2
field_email_2
Add values 3:
field_name_3
field_email_3
However, I haven't found a way to insert arbitrary html into a form in Django code, similar to #markup in Drupal forms. So:
Is there a way to print the fields individually so I can put the extra html in the template? (What I am missing here is the exact syntax)
If not, how do I insert the extra html into the form?
Do you mean something like this in the template?:
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
(from the Django documentation: https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#looping-over-hidden-and-visible-fields )
You could render the header with something like:
Add values {{forloop.counter}}:

django form field - how to present the value in a form template

I have a model with a FileField which is shown below. I use this model in a model form.
receipt = models.FileField(upload_to='receipt/%m-%Y/', max_length=255)
I can save the object and the receipt field contains the file url.
Now when I present this object with the model form the file url is not presented, instead a message "no file chosen" is presented.
This is the html template I m using for the form:
<table cellspacing="5">
{{ cash_expenditure_form.as_table }}
</table>
and the form that I'm using:
class CashExpenditureForm(forms.ModelForm):
class Meta:
model = CashExpenditure
exclude = ('project','paid', 'paid_check','invoice','cash_expenditure_selection')
When I check this object in the admin the field url is presented as expected. Thus I think something is missing in the html form template.
Solution:
AdminFileWidget does the trick ;-) easy as always..
I'm not sure you can pre-fill file selection field.
The file input type creates a field through which users can upload files from their local computer or network. The VALUE attribute specifies the name of the initial file, but it is typically ignored by browsers as a security precaution. The ACCEPT attribute gives a comma-separated list of media types accepted, allowing the browser to filter out inappropriate files. Current browsers generally ignore the ACCEPT attribute.
http://www.htmlhelp.org/reference/html40/forms/input.html

Resources