Reputation: 21
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
Upvotes: 2
Views: 943
Reputation: 4755
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();
Upvotes: 2