Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

movepixels's avatar

Query 2 tables

What I am trying to do is at registration I need check 2 tables for the 'slug' for the user so obviously it has to be unique to the user table, and I have a restrictions table so user can also user banned words, offensive and general stuff like that.

User query looks like:

$valid = !DB::table('profiles')->where('slug', $value['value'])->select([$same])->first();
return response()->json(['valid' =>  $valid ]);

But how can I add on the same thing except different table and where('name', 'like', '%' . $value['value'] . '%')

Thanks,

Dave

0 likes
2 replies
lostdreamer_nl's avatar
Level 53

How about this:

// check that the slug does not exist in the profiles table
$valid = !DB::table('profiles')->where('slug', $value['value'])->select([$same])->first();
// check that the slug does not exist in the restrictions table
if($valid) {
    $valid = !DB::table('restrictions')->where('slug', $value['value'])->select([$same])->first();
}

dd($valid);

If it exists in the profiles table, it wont query the restrictions table.

If it does nto exist in the profiles table, it will query the restrictions table to see if it is still valid.

1 like
movepixels's avatar

Works for me! Thanks for your time and assistance.

Dave

Please or to participate in this conversation.