1

Laravel user id hard coded in

enter image description here

I currently have the id hard coded in but I want to pull it in from the current logged in user's id.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Niamh Flannery
  • 81
  • 1
  • 2
  • 8

1 Answers1

1

If you are using laravel authentication you can try this. Auth::id();

Jigs1212
  • 773
  • 8
  • 20
  • This worked but now I get an error : "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '12' for key 'PRIMARY' (SQL: insert into `files` (`id`, `name`, `size`, `updated_at`, `created_at`) values (12, Screen Shot 2018-03-10 at 12.01.22.png, 10106, 2018-03-10 12:07:31, 2018-03-10 12:07:31)) " – Niamh Flannery Mar 10 '18 at 12:08
  • are u storing the 12 in th id field – Jigs1212 Mar 10 '18 at 12:09
  • the error you are getting is because of the field id is a primary key and the table already has a value 12 in the id filed. try to change the table format – Jigs1212 Mar 10 '18 at 12:11
  • Yes, so the current user id is 12 but because I have already uploaded something for user id 12 I don't think it will let me upload multiple times for one user? – Niamh Flannery Mar 10 '18 at 12:11
  • files table with the following fields 1 id primary key 2 user_id (store user ids here) 3 name 4 updated_at 5 created_at – Jigs1212 Mar 10 '18 at 12:12
  • so should I remove the id field as a primary key? – Niamh Flannery Mar 10 '18 at 12:12
  • change the table as i mentioned above – Jigs1212 Mar 10 '18 at 12:13
  • that will give you unique record id for files and it will allow you to store multiple users files – Jigs1212 Mar 10 '18 at 12:13
  • sorry but can u see what I'm doing wrong here? I tried to create a new table but it will not generate random ids and gave me the same error again. – Niamh Flannery Mar 10 '18 at 12:21
  • CREATE TABLE `files` ( `id` int(10) NULL DEFAULT NULL, `users_id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `size` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), FOREIGN KEY (users_id) REFERENCES users (id) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; – Niamh Flannery Mar 10 '18 at 12:21
  • Give auto increment to the id and not user_id – Jigs1212 Mar 10 '18 at 12:43
  • Happy it helped you do up vote comments and mark the answer – Jigs1212 Mar 10 '18 at 12:54