Laravel multiple relations, save either one or the other - php

So I am wondering about the best approach for this issue. I have an appointments table and two relations, a contact relation and a company relation. In my appointments.create form, I want to be able to either select a contact, or a company, not both. I was thinking of two approaches but I hope there is a better way to accomplish this.
I can enter a question in my form, asking if the user wants to add a contact or a company, and if they select contact, I will display a contact dropdown, and the same for when the user selects company. In my appointments table I will have two fields, a contact_id and a company_id. I save whichever the user selected and set the other to either 0 or null. Then in my controller I select which was filled, and then grab the correct relation.
I can use the same field in my database for both, for example relation_id and then add another field to which I can save the text contact or company, and based on that I grab the correct relation in my views.
But both of these solutions seem like non solutions to me, and I am hoping there is a better way to achieve this, and since I have struggled with this a lot in the past as well, I thought it would be good to ask it here since I haven't been able to find a solution.

Related

User database structure

Im not a webdev, Im learning all this stuff on the go and I stuck with this stage with my project.
I have a website people can register to use it, so I can store users already. I have a table with different items (item_id, item_name, item_owned). My problem is, every users should have the same list of item, but they can select if they have this item or not on thier own profile.
I could do it make the db and the items are selectable, the db shows 0 if you dont have it or 1 if you have it. But every user sees the same item_owned data and I need it to be user specific.
How can achive this? What structure should I use with the database? Do I need any special libraies beside HTML, PHP, SQL?
Thank you!

PHP - Insert as draft

I'm building a CRM and I have a list of customers.
When I create a customer, I usually insert it as a draft as soon as the user clicks on "Insert button". Why? Because I want the user to be able to insert photos and documents on the insertion form. This way, I have the customer ID already and I can process the upload using that ID.
The problem here is that we can have multiple drafts at a certain moment.
My question is: is there any better method to do this? Without creating the draft? For instance, using a temp folder where the photos and documents are temporarily saved?
EDIT:
the issue woth having multiple drafts is that at certain point will may have a lot of trash.
I mean, this works for me ok? And it's a solution I have, I just wanted to know if there's a better solution instead of using drafts.
It depends on what exactly you are using to save the customer in the database.
If you are using an ORM such as Eloquent or Doctrine you just need to have the correct relationships between Entities and you won't need to save the Customer as draft, you can just flush all of the information at once.
If you aren't using an ORM, you can still do it in one request, just insert the Customer record first and get the last inserted ID , for example if using PDO, you can use PDO::lastInsertId https://www.php.net/manual/en/pdo.lastinsertid.php .
After you have the ID just set it to the other columns where you need it.

Tag-like skills in a user profile with a HABTM relationship

In the web application I'm building a user has a profile where they can list their skills. I would like the kind of functionality StackOverflow has when making posts, where you can type tags into the tag input and select ones that already exist, and create them if they don't already exist.
At the moment, I've got a select box appearing on the page with with the id of the users current skills as values. I'm achieving this by doing:
// ProfilesController.php
$skills = $this->Profile->ProfilesSkill->find('list');
// edit.ctp
<?php echo $this->Form->input('Skill', array('value' => $skills)); ?>
I've got no idea how to progress further, though. First of all, the name field for the skills should be shown instead of their id, which I'm confused about because by Cake's convention it will use the name field by default, even though it's not. And secondly when I enter my skills into my profile Cake should automatically make all the required entries in the profiles_skills table. How can I make that work?
The solution is pretty straight forward, though it's pretty much work.
First you'll need a Tag-System. You could build one on your own (like any habtm-relation), or use a plugin like https://github.com/CakeDC/tags
For the second part, the function is called "Autocomplete". It's basically a ajax call every time you enter a letter in a form field. There are a bunch of tutorials out there, e.g: http://blogfreakz.com/cakephp/cakephp-jquery-autocomplete-tutorial/
Hope this points you in the right direction

symfony2 forms and many-to-many relations

I thought I'd see if anyone could point me in the right direction here - I have a users table and a contents table, and I want to keep track of what user is authorized to edit what content.
Thinking in a relational database way, a many-to-many relation table is the solution, adding rows of user_id & content_id for each authorization.
Many users can be authorized to edit a content piece, and a user is also of course able to have access to more than one content piece.
Now, the biggest issue I see (being a Symfony2 novice) is the admin form where an admin must be able to set these permissions, preferrably with a list of contents and checkboxes next to them, and have the form processor take care of the adding/deleting rows to that relationsl table.
Is the collection form field the right way to go here?
Any help is much appreciated.
I think the Doctrine2 documentation on Many-to-Many relations already gives you an answer on that question how to map the data.
The form issue is already explained in another SO question.

What's the best way for the user to add multiple details to single area of input?

In my project the user has to detail his management expenses in four different areas. At first we had a simple table (this one) where he had to write all the data grouping all the expenses description in a textarea, now we want him to detail each area, so i have to give the user the possibility to add "rows" to each area. That means that if his "Impegni" expenses consists of three entries he must enter three "rows".
My idea is to use jquery to let the user add rows and modify the rowspan of the first column when the user adds/remove a row (that is, if you need to enter another row for "Impegni" i create a new row under the third row and add "colspan=2" to the "impegni" cell) but maybe there is a better way to handle this kind of input.
I try to explain it even better: the four rows that are present in my table are the "master" areas and the user must add details to those areas. Creating new "sub-rows" for each area it's an idea, but maybe there is a better way to handle it!
EDIT - another idea is to have four different tables (one for each one of the "master" rows) and show hide them with tabs (look at example five on this page) but i don't know how to handle the sum of all the rows
What do you suggest?
My friend has made a plugin for it, you can look here.
I think jquery is the best way, you can access your database with simple ajax command from jquery, like .load()
:)

Categories