I am new to Laravel and I'm developing a simple application and ran into a problem, I searched for similar questions but did not find the right answer.
I have three tables with the names of ads, companies, and companies_blacklist. I want to check when creating and updating an ad if the advertiser's company ID is present in the Companies table and is not in the companies_blacklist. I would also like to show an error message if the advertiser's company is in companies_blacklist.
I wanted to do this using Laravel validation rules, but I couldn't, so I manually looked in the database. How can I do these things using only Laravel validation rules?
AdRequest rules:
public function rules()
{
if ($this->isMethod('post')) {
return [
'company_id' => 'required|exists:companies,id',
'job_id' => 'required|exists:jobs,id',
'salary' => 'required|numeric|in:1,2,3,4,5,6',
'seniority' => 'required|numeric|in:1,2,3,4,5',
'work_type' => 'required|numeric|in:1,2,3,4',
'ad_url' => 'required|min:5|max:255',
'explanation' => 'nullable|min:25|max:255',
];
} else {
return [
'company_id' => 'required|exists:companies,id',
'job_id' => 'required|exists:jobs,id',
'salary' => 'required|numeric|in:1,2,3,4,5,6',
'seniority' => 'required|numeric|in:1,2,3,4,5',
'work_type' => 'required|numeric|in:1,2,3,4',
'ad_url' => 'required|min:5|max:255',
'explanation' => 'nullable|min:25|max:255',
];
}
}
AdController:
public function store(AdRequest $request)
{
$inputs = $request->all();
$inputs['user_id'] = 1;
$inputs['publish_status'] = 2;
$isCompanyInBlacklist = CompanyBlacklist::where("company_id", "=", $inputs['company_id'])->exists();
if ($isCompanyInBlacklist) {
return back()->withInput()->withErrors(['company_id' => "this company is in blacklist"]);
} elseif (!$isCompanyInBlacklist) {
$ad = Ad::create($inputs);
return redirect()->route("admin.ads.index");
}
}
To do this, you would need the validation ruleunique
which is the opposite ofexists
. This makes sure that your input does not exist in a specified database column and returns an error if it exists.
basic syntax is
'item' => 'unique:table,column'
check the docs for more information on it.
Try this.
public function store(AdRequest $request)
{
$inputs = $request->all();
$inputs['user_id'] = 1;
$inputs['publish_status'] = 2;
$isCompanyInBlacklist = CompanyBlacklist::where("company_id",$inputs['company_id'])->get();
if (count($isCompanyInBlacklist) > 0) {
return back()->withInput()->withErrors(['company_id' => "this company is in blacklist"]);
} elseif (!$isCompanyInBlacklist) {
$ad = Ad::create($inputs);
return redirect()->route("admin.ads.index");
}
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.