2

I have a laravel application with a database. I want to make the rows in the database unique so that if someone tries to insert anything into the database when the website is running, an error is shown. The site is about TV's. I have a condition, TV and colour column in the data table. If someone inputs another exact row, I want the application to throw an error because I get duplicate data in the table. I believe I require a query looking like this: SELECT where 'TV', 'condition', 'colour', like INPUT get () Any help would be appreciated

Andy
  • 21
  • 1
  • Better to use a unique constraint on the database table – Mark Baker Apr 30 '17 at 19:00
  • How would I go about doing that? I have tried doing it but it only works for one column. I need it to work for all three at once – Andy Apr 30 '17 at 19:23
  • By setting a [unique, composite key](http://stackoverflow.com/questions/635937/how-do-i-specify-unique-constraint-for-multiple-columns-in-mysql) – Mark Baker Apr 30 '17 at 20:06
  • I also have a foreign key from another table in my TV table. How can I do it so that the foreign key from another table is also unique with the rows? – Andy May 01 '17 at 13:03

1 Answers1

1

first create model for your table and start work in your controller.

use App\Models\YourModelName;

$table_row = YourModelName::where('color', 'value')->first();

if($table_row->count()){
// do whatever you want
}else{
// Work with your error...
}

Hope this concept will work for you to get a unique row. You can also add more condition by using laravel's builtin function orWhere();

Giulio Bambini
  • 4,290
  • 4
  • 18
  • 34