How to get an attribute from a relation in Laravel? - laravel

This is part of my code:
$form = new Form(new Shop());
$form->tab('terminal', function (Form $form) use ($id) {
$form->hasMany('shopterminal', '', function (Form\NestedForm $form) {
$form->text('terminal_num', 'terminal number')->required();
$form->select('poz_type', 'POS type')->options(['static' => 'one', 'dynamic' => 'two'])->required();
$form->select('psp_id', 'POZ name')->options(Psp::pluck('name', 'id'))->required();
$form->text('sheba', 'sheba number');
$form->text('account_num', 'account number')->required();
$form->select('bank_id', 'bank name')->options(Bank::pluck('name', 'id'))->required();
dd($form);
});
Here is the result of dd($form):
I need to get the value of terminal_image item (which is 15841949062134.png). Any idea how can I get it?
Noted that, neither of below syntax works:
$form->get('terminal_image')
$form->select('terminal_image')
$form->terminal_image
$form()->terminal_image
$form->relation->terminal_image

For your specific example:
$form->form->model->shopterminal[0]->terminal_image

you can use :
$form->form->model->relations['shopterminal']->items[0]->attributes['terminal_image']

Related

How can i rewrite the following code with switchmap

Hi I have a small code snippet using rxjs library as follows. It is working fine . However I would like to re write it using switchmap . I tried all option however i am getting an error . was wondering if someone could help me out .
this.campaignControl.valueChanges.subscribe(
(value) => {
this.flightsService.getUnpaginatedFlightsWithinRange(
{
campaignId : value,
jobStatuses: this.flightStatusIds,
rangeStartDate:
(this.rangeControl.value[0]).toISOString(),
rangeEndDate: (this.rangeControl.value[1]).toISOString()
}
).subscribe(
(flightsUnpaginated) => {
this.flights = flightsUnpaginated;
}
);
}
);
thank you
I believe you're looking for something like this:
this.campaignControl.valueChanges.pipe(
switchMap(value => this.flightsService.getUnpaginatedFlightsWithinRange({
campaignId : value,
jobStatuses: this.flightStatusIds,
rangeStartDate: (this.rangeControl.value[0]).toISOString(),
rangeEndDate: (this.rangeControl.value[1]).toISOString()
}),
tap(flightsUnpaginated => this.flights = flightsUnpaginated)
).subscribe();

Cypress how to select option containing text

In Cypress, the select('option-text') command will only work if there is an exact match. How can I tell it to select an option that contains the text instead ?
Through aliasing you can make it work:
cy.get('select')
.find('option')
.contains('YOUR TEXT')
.as('selectOption')
.then( () => {
cy.get('select')
.select(`${this.selectOption.text()}`)
})
A slightly improved variant of Can Akgun user is as following (bonus adding as cypress command):
/**
* Select an option containing part of string in its text body
* {String} elementSelector
* {String} optionTextPart
*/
Cypress.Commands.add('selectOptionContaining', (elementSelector, optionTextPart) => {
cy.get(elementSelector)
.find('option')
.contains(optionTextPart)
.then($option => {
cy.get(elementSelector).select($option.text());
});
});
In this way we do not need global variables.
I haven't found anything like that in the api so far, nor in the github issues.
So I resorted to adding a custom command in my project:
// cypress/support/commands.ts
Cypress.Commands.add('selectContaining', {prevSubject: 'element'}, (subject, text, options) => {
return cy.wrap(subject).contains('option', text, options).then(
option => cy.get('select#report-selection').select(option.text().trim())
);
});
// cypress/support/commands_type.ts
declare namespace Cypress {
interface Chainable<Subject = any> {
requestApi(method: HttpMethod, url: string, body?: RequestBody): Chainable<Cypress.Response>;
selectContaining(text: string | string[], options?: Partial<SelectOptions>): Chainable<Subject>;
}
}

Swiftmailer Symfony 3 : 1 email sent but nothing appear

I'm trying to send an email with symfony 3, swiftmailer and twig.
I'm doing a form with formbuilder and when someone click on the submit button, it send the mail. I don't have an error, only a "1 spooled message".
What I've tried : I did : php bin/console swiftmailer:email:send
It returns : [OK] 1 emails were successfully sent. (also here i've no mail).
In my two boxes I have allowed less restrictions for connexions.
My code looks like :
For the controller :
/**
* #Route("testmail", name="testmail")
*/
public function testmail(Request $request)
{
$societe = null;
$form = $this->createFormBuilder()
->add('Societe', TextType::class, array('label' => 'Société'), array('constraints' => array(new NotBlank(array()),
new Length(array('min' => 2,
'max' => 25)))))
-> add('send',SubmitType::class, array('label' => 'Cotation'))
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
if ($request->isMethod('POST')) {
$societe = $form["Societe"]->getData();
$message = \Swift_Message::newInstance()
->setSubject('Etude Financiere')
->setFrom('wolffvianney#gmail.com')
->setTo('vianney.wolff#yahoo.fr')
->setCharset('utf-8')
->setContentType('text/html')
->setBody($this->render('#gkeep/Finance/email.html.twig', array('Societe' => $societe)));
$this->get('mailer')->send($message);
}
}
return $this->render('#gkeep/Finance/finance.html.twig', array('form' =>$form->createView(),
'Societe'=>$societe));
}
the config.yml :
swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }
the parameters.yml :
mailer_transport: mail
mailer_host: smtp.gmail.com
mailer_user: wolffvianney#gmail.com
mailer_password: *mypasswordforwolffvianney#gmail.com*
secret: thesecret
the Finance/email.html.twig
<html>
hi
société : {{ Societe }}
</html>
if anyone has any advices or help, I can send other files if needed. I don't understand what is wrong, thanks for advance. (I also tried to delete the line spool{type:memory
it says then that 1 email has been sent but here also, I don't receive any mails.
Vianney
Try
php bin/console swiftmailer:spool:send --env={your_env}

angular2-notifications with i18n translate

I'm using https://www.npmjs.com/package/angular2-notifications this package to get notification it works fine but it works at ts file like;
saveUser(user){
//some process then notification will work.
this.notif.success(
'Yeahhh successfull create notification',
{
timeOut: 3000,
showProgressBar: true,
pauseOnHover: false,
clickToClose: true,
maxLength: 50
}
)
}
works fine but I'm using translate (i18n) and want to give these parameters by the language.And the package says it has a html function but I tried and couldn't do that which is
Thank you
I guess img can't be seen , it was the code of html
this.notif.html(`<p translate > {{ 'City' | translate }} Success</p>`)
You can use the TranslateService to get your translation values.
First import the service.
import {TranslateService} from '#ngx-translate/core';
Then inject and use it like so:
export class YourComponent {
constructor(translate: TranslateService) {
translate.get('CITY').subscribe((res: string) => {
console.log(res);
//=> 'Whatever your translation is for "city"'
});
}
}
Further documentation can be found here.

Laravel Validator / Uploaded File Fails At Required

"dd" output of Input::all() in postController:
array(8) {
["_token"]=>
string(40) "6WZ87M1LCiVCsaUS9HbjZckRibXfF2RP69LCpW7K",
...
...
["svg"]=>
object(Symfony\Component\HttpFoundation\File\UploadedFile)#9 (7) {
["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
bool(false)
["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(39) "Screenshot from 2013-06-18 17:07:27.png"
["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(9) "image/png"
["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(29747)
["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(0)
["pathName":"SplFileInfo":private]=>
string(14) "/tmp/phpdRTDU7"
["fileName":"SplFileInfo":private]=>
string(9) "phpdRTDU7"
}
}
Validation:
$rules = array('svg' => 'required');
$check = Validator::make(Input::except('_token'), $rules);
if($check->fails()){
return Redirect::back()->withErrors($check);
}else{
return Redirect::back()->with('message', 'No problem');
}
And I get the error message:
Error message:
The svg field is required.
Even if I upload file as you see on dd output, it shows that error always.
Thanks,
user2413500 found that the problem was using Input::except('_token') which did not include the file object. However, Input::all() does include the file object. This seems to be a bug which I'll report, but the definition of Input::except is "all" minus the items you don't want.
But what seems to be happening is "all" minus the items you don't want, minus your file!
Itrulia and Taylor say this is not a bug.
However, these are confusingly not identical statements when you have a $_FILE posted...
$params = Input::except('_token'); // Missing file inputs!
$params = array_except(Input::all(), '_token'); // The current solution.
Be on guard! :)

Resources