How to store Contact form data in Database in PyroCMS 3.x - pyrocms

How do I store the data in the Database for a website using pyrocms.
I currently use the contact plugin and need to store the data in mysql.
If anyone has done this, I would appreciate the help!

The contact plugin does not capture data, but you can indeed save the input by slightly extending it.
You will need to have a table made to put it in. Otherwise, you could make a Stream to stuff the data into as well.
The easiest way is likely to use the posted callback on the ContactFormBuilder.
Something like this:
use \Anomaly\ContactPlugin\Form\ContactFormBuilder;
app(ContactFormBuilder::class)->listen('posted', function(ContactFormBuilder $builder) {
YourStreamModel::create($builder->getFormInput());
}

Related

Is it necessary to use the form to transfer data to the server?

I'm new to backend programming. I chose the laravel framework. Already learned the basics. During the study, the question arose: is it necessary to use the form to transfer data to the server ?. For example: the deletion route looks like this to me
Delete.
If I leave it, will it be a mistake? Maybe advise an article or something. Thanks in advance
Short answer is no, it's not necessary, but you should (if you're bound to HTML only).
The HTTP standard has different methods for different purposes. Using an anchor tag will always make a HTTP GET request to the link/server, which is not ideal, GET request should never change the remote (server) state, that's a job other methods (POST, PUT, DELETE, PATCH), you should try to use the method that better describe what you're trying to do: in your case I suppose you're trying to delete a complaint, so a DELETE or POST is what you're looking for.
The only way to use make a non GET request in plain HTML* is to use <form>. Also if you're planning to use a method different from POST you should take a look at Laravel's #method here
Mind that if you can and want to use JavaScript to perform your request you totally can, dropping the requirement to have use form (docs and docs).

What is the purpose of API Resources?

I've read the docs and watched the Laracast. I'm still left wondering why you would use them?
I get that you can map different data to different names if your field names were to change but you want to keep a consistent public API. But surly you can just do the same on the model with the toArray() method and change the mappings there?
If I were to do:
return User::find(1);
I get a response like:
{"id":1,"name":"Ova Parker"}
If I do:
return new UserResource(User::find(1));
I get a response like:
{"data":{"id":1,"name":"Ova Parker"}}
Is there a significance in wrapping it with a data tag? I am just guessing but is this a standard JSON format for API's? Why would you not just do return User::find(1); instead of using an API resource, if this is under API routes then it'll return it as JSON anyway automatically.
You kind of answer the question by yourself. The idea behind API Resources or Transformers (like the ones from Fractal) is to hide the db field names from the client. With return User::find(1) you expose your whole db structure which might not a good idea security-wise and also bad for the release process. If you need to change a db field name, you have to change the API too. With Resources you have a mapping between your application and the consumer of you API.
It seems more work in the beginning, but once you started it, you won't wanna miss it anymore.
There is no toArray() method in PHP, which gets magically called like __toString(). You have to write you own and call it by yourself. Resources are built-in by Laravel and will be resolved automatically.

Adding custom data based attributes based on Response object

Halo ! I'm trying to implement dropzonejs in a very specific way. Actually I follow the standard implementation described on the official page. Everything works perfectly.
But I'm willing to attach the server's generated URI for each uploaded file directly when uploaded : when uploading it's creating a database entry with some stuff like a page uri with title etc. This mean that the server would return as a response the id of the database saved file in order to attach the href attribute with its value to the the element in front.
This is quite ok to do this when only one file is uploaded, but it becomes trickier when bulk uploading.
So maybe I didn't understand the documentation well (and I'm quite sure I didn't), but is there any way to add custom data-dz-like attributes based on my server's response ? I'd like something like data-dz-url where the url points to a database entity (not the file itself).
Or if not if there is an "easy way" to handle this.
Thanks a lot
Here is the answer :
myDropzone.on('success', (file, response) => {
file.previewElement.href = "/admin/media/"+response.id+"/show/"
})
file is reference to the current uploaded element. It's possible to extend it's html attributes through previewElement. Setting the data-type attribute in the template before, then assigning it the right value works aswell.
Hope this will help some.

CRUD with localization in laravel 5?

I want to create, read, update and destroy data with localization in laravel 5. please help me some suggestions thanks!
Localization comes build in, check out the official docs:
https://laravel.com/docs/5.3/localization
Honestly, for inserting and updating data, I don't think you can choose to localise your data: you have to insert ALL contents of ALL languages because you are saying you support these languages, right?
But for reading and displaying data, you can
1) Define columns with suffix. For example in post table you have columns called body_en, body_cn, body_jp etc.
2) When users choose to change languages, use a controller to respond to that action, save a language suffix like '_en' into a cookie, maybe called 'cookie_language'.
3) When users navigate in your site, for every request, use a middleware check whether there is a language cookie, get its value, save it into session, and call App::setLocale() accordingly.
4) When you retrieve data from post table, get the language value out of session, use it as suffix, like Post::select(['body'.$suffix])->get(). Localised data got!
5) Because you have set locale, now you can use trans() in all your views. View translated!
Hope this helps!

How can I use Parse to send data to specific user

I'm creating an online game which have to send data between users. I chose Parse as the backend service. How can I use Parse to do this task?
Query for the user that you want to send data to and send data
Take a look a the Parse Docs. What you want to do is documented very well there.
https://parse.com/docs/android_guide
Take a look at saving and retrieving objects.
Also make sure your console (where you can see the "User" class and all your other classes) is set up with the objects you need.

Resources