I have a form for users to sign up. This form has a file input which will upload images in a directory in server by ajax instantly as user selects a file. There's also a button which will submit the form and saves user info in user table in database.
Each user can have multiple images. Ajax also save address of images in photos table when file uploads. this table has user_id column. but since the user_id will be generated when submit button is hit and user is not registered yet, I can't fill user_id column in photos table. The id of user is generated when whole form is submitted.
so should I update photos table when form submits by adding id of user to user_id column?
or I must save photos with ajax in temporary_photos table and then when submit is hit save them in main photos table and delete them from temporary table?
or is there another approach?
what if user uploads files, but closes the window without hitting submit button? how I find out the window is close and registration is canceled so i will delete images?
You are trying to solve a problem that you created. If user_id is a dependency for the photo to be saved, let the user be saved first.If you do it the other way , it you will over complicate the workflow.
So, i suggest you register the user first, save the user id in session (or log the user in) and then upload the photo.
If you must do it together, then let the ajax request register the user and then save the photo and save the user_id in a session variable. Next time you upload a photo you could get the user from session variable and save the photo.
function savePhoto(Request $input){
if(Session::has('user_id'){
$user = User::find(Session::get('user_id'));
}
else{
$user = User::create($input->all());
}
$user->photo->save($input->all());
}
You can try this way-
Since an user can have multiple images that means the relation is one to many between. So better if you use a images table.
Table: Images
Column(s): id, user_id, image, uploaded_at
Make user_id field is nullable and image column will save the name of uploaded images. Upon successful uploading and adding iamges record to database save last inserted ids in a session array. Once the user submit the form, create a record to user table, grab the last insert it, check if there are any data in session array, if found, loop through the array and select those images record by id and update user_id field. This will solve your problem.
You also asked for a way to delete images for which users didn't submit the form. To do this, you can run a cronjob/laravel task schedular. A script that will remove images and delete records for those images which were uploaded 5 mins (for example) ago and user_id is still null.
Related
have this problem.
Have small web app. There are 10 users.
Every user has own form and he must fill it every day. Inserting into mysql works fine.
Problem is every user has his own numbering. YEAR-MONTH-CUSTOM-NUMBER
User 1: 202111121, 202111122, 202111103 etc
User 2: 202111221, 202111222, 202111203 etc
user 3: 202111321, 202111322, 202111303 etc
My question: how can I define inserting records via form for users into mysql?
For example today User1 save html form so it'll insert record into mysql with id 202111121.
Today the same user will fill form again and he must insert record with id 202111122
etc.
Thank you
After user logging to the system(Auth::attempt success)
How to add additional data to the Auth session for user ?
I have user all data but there is no profile image data of user.Profile image of user is save into another table like user_profile_image.
I have to set profile image data in Auth::user()->file_name till when user logout.
I tried this way:
Auth::user()->setAttribute('key','value');
and
Auth::user()->file_name=$filename;
but it doesn't work.
If you want to just save the user picture:
add a migration that adds the picture column in the users table
Use the field to save the picture
$u = User::find($userId);
$u->picture = $pathToPicture;
$u->save();
then access it with
$u->picture;
or
Auth::user()->picture;
if you want to add fields to registration form: http://www.easylaravelbook.com/blog/2015/09/25/adding-custom-fields-to-a-laravel-5-registration-form/
I want to allow users to enter up to 5 tags tag like a company name, or charity their associated with. When a user clicks on that tag, it will take them to a page with all the users associated with that tag.
example: If somebody enters CBS, it will show all users associated with CBS.
I know I can add a unique index to keep users from entering duplicate content like stated here , but won't that keep users from being able to enter a company they are associated with if it already exist in the database.
I don't wan't duplicates in the database, but do wan't to allow users the choice to pick companies that may already exists in the database submitted from other users.
This database will hold only data submitted from the users.
I am using larval 5
my database looks like this:
Users
id|username|email
Tags
id|tags
usertags
id|userId|tagId
You can use the validate in request file of laravel
Use Unique based on the tag(and/or)comment & user id
It will help you to get data what you are expecting
refer laravel document also
I don't really understand properly the concept behind uploading multiple files with preview. I checked DropzoneJS and it looks nice and I feel I can do a lot of stuff with it.
I have this example: I have a user (id: 801) that creates a new post (id: TBD since the post is not yet created). I have a table image_post which holds the images of the specific post, and an image table which holds the image details. Now, I have the Dropzone form which is supposed to upload images to a folder that I specify in the .php file that is implemented to deal with the ajax request coming from Dropzone. Let's say the user uploads 2 images and I store them into an temp folder. The user submits the creating of the new post, it receives an ID: 10001. The temporary files are on the disk (but should they be stored in the DB as well?) but don't see how to link the post to the images.
What are the exact steps that are required to be able to (after the files were uploaded with Dropzone ajax request) link those images to the actual post id?
Your system will need to create a database record for the post prior to them uploading images. Consider the following table structure, where an i prefix indicates an INT column, s a VARCHAR/TEXT column, and dt a DATETIME.
Table posts: iPostId | iUserId | iStatus | sComments | dtCreated
Table photos: iPhotoId | iPostId | dtUploaded
When a user begins to start a new post, create a record in the posts table with their user id, an empty comments, a datetime such as '0000-00-00 00:00:00', and a status of 0, and then add a hidden form input to the photo upload form so that the assigned post id is known to your photo upload script, e.g.:
<input type="hidden" name="iPostId" value="22" />
The photo upload script could create a new record in the photos table using the post id and then save the photo in a particular folder using the post id in the filename, for example if the photo_id for the record created was 2821 you would save the file as 2821.jpg in your chosen folder.
When the user finishes their post and clicks to save/submit, you could use an UPDATE query to modify the original record on the posts table, assigning their entered text to the sComments column, the current date and time to the dtCreated column, and changing the iStatus to 1.
On your pages where you display a list of posts simply modify your query to something like this to exclude those currently in a draft status (0): SELECT * FROM posts WHERE status=1;, and then when viewing the post you can run a query such as SELECT * FROM photos WHERE post_id=22; to get an array of the photos to be displayed.
Obviously this can be expanded upon but hopefully gives you a good starting point.
I have problem with making event table in php, i don't know even how to start, so it would be great if someone help me with this.
I have table users and cars, now i need to make table event that will follow which user is entered new car in database.
Can someone help me with making this table that will automatically populate with user_id and car_id that this entered(before user can enter data he must login, so i know which user is entering data, but i don't know how to take his id and car_id which he entered and put in other table(event_table))?
Thanks
You can store user id in PHP session when user loged in.
so when user make any update or insert in car. you can fetch user id from session and insert in event table.