Laravel Nova upload image and save url to database - laravel

I have been attempting to upload an image to cloudinary, which is pretty easy. My problem is how do I go about saving the url to the database instead of the image? I was suppose to use https://github.com/Silvanite/nova-field-cloudinary, but the documentation is pretty slim. Also, I would like to save the image with original file name (storeOriginalName).
CloudinaryImage::make('Featured Image')
Nova's version:
Image::make('Featured Image')
->disk('cloudinary')
https://nova.laravel.com/docs/2.0/resources/file-fields.html#images
https://cloudinary.com/documentation/php_image_and_video_upload#upload_response
https://laravel.com/docs/5.7/requests#storing-uploaded-files
Update. This works for storing original file name, but still not sure how to grab url and save it to featured_image column:
CloudinaryImage::make('Featured Image')
->storeAs(function (Request $request) {
return $request->featured_image->getClientOriginalName();
}),

You shouldn't need to store the remote URL with Cloudinary. The public id returned by the component is used to generate the final URL when you output the image somewhere using one of the ways described in the documentation ...
// Using the helper (with transformation)
return cloudinary_image($this->featured_image, [
"width" => 200,
"height" => 200,
"crop" => "fill",
"gravity" => "auto",
])
// Using the Storage Facade (without transformation)
return Storage::disk('cloudinary')->url($this->featured_image);
// Using the Storage Facade (with transformation)
return Storage::disk('cloudinary')->url([
'public_id' => $this->featured_image,
'options' => [
"width" => 200,
"height" => 200,
"crop" => "fill",
"gravity" => "auto",
],
])
Or you could generate the URL yourself as per the Cloudinary documentation https://cloudinary.com/documentation/image_optimization
It would be helpful if you could expand on why you need to save the full URL as there may be an alternative solution.

The upload response contains an url field. Here is an example:
{
public_id: 'sample',
version: '1312461204',
width: 864,
height: 564,
format: 'jpg',
created_at: '2017-08-10T09:55:32Z',
resource_type: 'image',
tags: [],
bytes: 9597,
type: 'upload',
etag: 'd1ac0ee70a9a36b14887aca7f7211737',
url: '<url>',
secure_url: '<secure_url>',
signature: 'abcdefgc024acceb1c1baa8dca46717137fa5ae0c3',
original_filename: 'sample'
}

Related

How Do You Display Sample Code in API-Platform v3

I've seen a few examples of code samples being included in Redoc documentation and even tested other openapi.json files within API-Platform document file to prove that it can generate them.
What I cannot figure out is where to put x-code-samples inside the openapiContext to generate the Request samples.
This is what I would like to see in redoc.
The answer is I need to use x-codeSamples and not x-code-samples
#[ApiResource(
shortName: 'common-data',
operations: [
new Get(
uriTemplate: '/common-data/{id}',
openapiContext: [
'summary' => 'Returns commonly used static data for dropdowns etc',
'x-codeSamples' => [
['lang' => 'php', 'source' => 'php sample code'],
],
],
name: 'common-data_item',
),
],
)]
Then the sample will show up in the redocs.

Why do inline/IRRE records get sys_language_uid=0?

When creating a tt_content record in sys_language_uid = 2 (no translation/l10n_parent = 0) in backend and inserting fields in a type => 'inline' column tx_foo_slider_slides (definition below), the newly created records in tx_foo_domain_model_slide are created with sys_language_uid = 0. Adding an image to the slide's image field creates a sys_file_reference with sys_language_uid = 2. Is this correct? If not, how do I change this? I would expected to have all records (tt_content, tx_foo_slider_slides, sys_file_reference) created with sys_language_uid = 2. It seems that records created before the update do have sys_language_uid set to 2, but I am not sure what has changed between 8 and 9: is it a core change? Or my site/language configuration? sys_language_uid had a default of 0 in TCA, but removing that and creating additional records did not show any different behaviour.
When trying to load records through an ExtBase repository where I seemingly have to use setRespectSysLanguage(false):
I do get no results with setLanguageOverlayMode(false)
I do get results with setLanguageOverlayMode(true), but the image field is NULL.
... but if I also manually change the tx_foo_domain_model_slide.sys_language_uid to 2 it looks fine in BE and FE/ExtBase (image is a working FileReference)
What could be going wrong? What might need to be changed? I think creating records in non-default languages is a supported case? For me, the root cause seems to be that records are created with the wrong language set.
My configuration:
TYPO3 9.5.5 (updated from 8)
config.tx_extbase.features.consistentTranslationOverlayHandling = 1 (but tested 0, too)
config.sys_language_overlay = 0 (I don't think 1 or hideNonTranslated changed any behaviour)
Sites (and multi-site). This specific site comes with two languages:
Language 1, German (languageId: '0') is disabled.
Language 2, English (languageId: '2') is enabled; fallbackType: strict
A custom table tx_foo_domain_model_slide including language fields and an image column:
'image' => [
'label' => $ll.'tx_foo_domain_model_slide.image',
'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
'image',
[
'appearance' => [
'createNewRelationLinkTitle' => 'LLL:EXT:cms/locallang_ttc.xlf:images.addFileReference',
],
'overrideChildTca' => [
// types ...
],
'minitems' => 1,
'maxitems' => 1,
],
$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'] .',m4v,mp4v,mp4'
),
],
A column tx_foo_slider_slides in tt_content:
[
'label' => $ll . 'slider.slides',
'config' => [
'type' => 'inline',
'appearance' => [
'collapseAll' => true,
'expandSingle' => true,
],
'foreign_field' => 'slider',
'foreign_sortby' => 'sorting',
'foreign_table' => 'tx_foo_domain_model_slide',
'maxitems' => 100,
'minitems' => 0,
],
],
The problem in this case is, that tx_foo_domain_model_slide.sys_language_uid is defined as:
'sys_language_uid' => [
'config' => [
'type' => 'passthrough',
'default' => '',
],
],
According to the TCA docs:
FormEngine does not render anything for passthrough types by default. But it can be combined with a custom renderType to make it render something. A user type is better suited for such use cases, though.
Type passthrough field values are usually also not rendered at other places in the backend.
This leads to sys_language_uid not being set (and staying at 0). So, sys_language_uid has to be defined as 'type => 'select'` or similar. To hide it, one can put it in a hidden palette, as described here.

Add/modify text between parentheses

I'm trying to make a classified text, and I'm having problem turning
(class1 (subclass1) (subclass2 item1 item2))
To
(class1 (subclass1 item1) (subclass2 item1 item2))
I have no idea to turn text above to below one, without caching subclass1 in memory. I'm using Perl on Linux, so any solution using shell script or Perl is welcome.
Edit: I've tried using grep, saving whole subclass1 in a variable, then modify and exporting it to the list; but the list may get larger and that way will use a lot of memory.
I have no idea to turn text above to below one
The general approach:
Parse the text.
You appear to have lists of space-separated lists and atoms. If so, the result could look like the following:
{
type => 'list',
value => [
{
type => 'atom',
value => 'class1',
},
{
type => 'list',
value => [
{
type => 'atom',
value => 'subclass1',
},
]
},
{
type => 'list',
value => [
{
type => 'atom',
value => 'subclass2',
},
{
type => 'atom',
value => 'item1',
},
{
type => 'atom',
value => 'item2',
},
],
}
],
}
It's possible that something far simpler could be generated, but you were light on details about the format.
Extract the necessary information from the tree.
You were light on details about the data format, but it could be as simple as the following if the above data structure was created by the parser:
my $item = $tree->{value}[2]{value}[1]{value};
Perform the required modifications.
You were light on details about the data format, but it could be as simple as the following if the above data structure was created by the parser:
my $new_atom = { type => 'atom', value => $item };
push #{ $tree->{value}[1]{value} }, $new_atom;
Serialize the data structure.
For the above data structure, you could use the following:
sub serialize {
my ($node) = #_;
return $node->{type} eq 'list'
? "(".join(" ", map { serialize($_) } #{ $node->{value} }).")"
: $node->{value};
}
Other approaches could be available depending on the specifics.

multilingual model attributes in sails js advice

I'm about to start a project that's going to require some attributes on a model to be translated and not sure what the best approach would be.
One option would be to create a JSON type attribute and store the translations as
{
title: [{ "en": "cheese" }, {"de": "Käse"}, {"es": "queso"}, etc... ]
}
but I'm also wondering if would be better to store these values in a separate collection and create an association, then when getting the parent model I could just populate with the appropriate language. So something like
Product Model
module.exports = {
attributes: {
sku: 'string',
values:{
collection: 'productValues',
via: 'product'
}
}
}
Product Values Model
module.exports = {
attributes: {
title: 'string',
body: 'string',
language: 'string',
product:{
model: 'product'
}
}
}
I would just add a JSON for translations to the model.
module.exports = {
attributes: {
defaultValue: 'string',
translations: 'json'
}
}
Then you could simply work with the translation object. Delete translations, add new ones etc.
Model.findById(id).then(function(record){
var translations = record.translations;
translations.en = 'Hello';
delete translations.fr;
Model.update({id: id},{translations: translations}, function(){});
});
(Just an example code, didnt test it)
But if you want 1 translation to work with multiple records than it is insufficient. And then you could create seperate collection for translations and reference records of it as needed.

The merge tags in mandrill don't work in codeigniter

I use Mandrill plugin for Codeigniter.
I created HTML template through Mandrill account, named fess1 with merge tag FNAME, after I published it.
Example:
...
<p>
<span>Hi *|FNAME|*,<br></span>
</p>
....
Now I try to send mail from codeigniter like:
private function sendMailMandrill($owner_name,$business_name,$owner_email){
$message = array('dest_mail' => $owner_email);
$message['to'] = array(array('email' => 'mim#wefi.com'));
$mergeVars[] = array(
'rcpt' => array(array('email' => 'mim#wefi.com')),
'vars' => array(
array(
'name' => 'FNAME',
'content' => 'Fessy'
)
)
);
$message['merge'] = true;
$template_name = 'fess1';
$template_content = array( // I don't know what I need to provide here, left it empty
array(
'name' => 'example name',
'content' => 'example content'
)
);
$message['merge_vars'] = $mergeVars;
return $this->mandrill->messages_send_template($template_name, $template_content, $message);
}
The result:
I get the mail, based on fess1 template, but with the tag *|FNAME|*.
Sounds like Mandrill didn't recognize the merge tag.
I used mandrill->messages_send_template but since my template stored into Mandrill account I have no clue what I need to provide for $template_content.
So I wrote dummy info there.
Did I miss something?
Thank you,
[EDIT]
From logs this is what I send:
{
"template_name": "fess1",
"template_content": [
{
"name": "example name",
"content": "example content"
}
],
"message": {
"owner_name": "עידו",
"business_name": "פלאפל מוסקו",
"dest_mail": "maxim#wifi.com",
"to": [
{
"email": "maxim#wifi.com"
}
],
"merge": "true",
"merge_vars": [
{
"rcpt": [
{
"email": "maxim#wifi.com"
}
],
"vars": [
{
"name": "FNAME",
"content": "Fessy"
}
]
}
]
},
"key": "xxxxxxxxxxxxxxxx"
}
You can provide blank information for the template_content parameter. That parameter allows you to use mc:edit regions in your template. It is a required parameter, but a blank array will suffice if all of the content is in your template in Mandrill.
As for whether the merge_vars were recognized, the first thing we recommend is inspecting the API Logs for your account (Settings > API Logs) since that will show you the JSON that Mandrill received. You can then compare that to the expected JSON format from the Mandrill API docs: https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template
It looks like your arrays may not be nested as expected. Once you view the JSON that's being generated as compared with the expected format, you can also view the PHP documentation for the Mandrill PHP client. It may not be identical to the CodeIgniter plugin, but should give you an idea of how the merge_vars parameter would be structured in PHP: https://mandrillapp.com/api/docs/messages.php.html
In mergeVars you created array instead key:value. Change it to:
'rcpt' => 'mim#wefi.com',

Resources