Home > database >  Checking the existence of a record in multiple tables with Laravel
Checking the existence of a record in multiple tables with Laravel

Time:10-05

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");
    }
}

CodePudding user response:

To do this, you would need the validation rule unique which is the opposite of exists. 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.

CodePudding user response:

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");
    }
}
  • Related