sander106
sander106

Reputation: 71

Laravel How to get an id from another table without a 1 to 1 relation

So i have a Registrations and a Gender table. And i want to get the id from Gender and use it in the RegistrationController. The Registration doesn't have a gender_id, but it does have a category_id, and the Category table has a gender_id. But i don't want to use the gender_id in the Categories because this check I'm making is for those gender_ids in the Categories.

This is what i have right now in the RegistrationController:

$gender = Gender::all();
$gender_id = $gender->gen_id;
dd($gender_id);

here it says 'gen_id' doesn't exist.. Thanks in advance!

Upvotes: 1

Views: 74

Answers (2)

Arash Dehghani
Arash Dehghani

Reputation: 36

Gender::all(); => fetch all the rows from table. So it's not helped you to find exact rows. so you have two different way to find, the first is Gender::first() => it's get the first of the row from table and Gender::find($id) => you can find the exact record.

Upvotes: 1

Dilip Hirapara
Dilip Hirapara

Reputation: 15296

Use pluck method for get all gen_ids

$gen_ids = Gender::pluck('gen_id')->toArray();
echo '<pre>';
print_r($gen_ids);
exit;

It'll give you all gender id in array.

Upvotes: 2

Related Questions