Laravel Eloquent query to check how many siblings student have
i have two tables
**students :**
id
family_id
name
**family :**
id
father_name
father_civil_id
contact_no
both tables are connected using family_id, i want to get how many Brothers/Sisters each students have(with name of sibling) using eloquent.
can you please help me, with controller/model/view.
laravel eloquent
add a comment |
i have two tables
**students :**
id
family_id
name
**family :**
id
father_name
father_civil_id
contact_no
both tables are connected using family_id, i want to get how many Brothers/Sisters each students have(with name of sibling) using eloquent.
can you please help me, with controller/model/view.
laravel eloquent
you can use relationship
– Dhruv Raval
Nov 23 '18 at 8:30
Are Brothers/Sisters students too?
– TsaiKoga
Nov 23 '18 at 8:44
@TsaiKoga yes all are in student table.
– iMatt
Nov 23 '18 at 9:09
add a comment |
i have two tables
**students :**
id
family_id
name
**family :**
id
father_name
father_civil_id
contact_no
both tables are connected using family_id, i want to get how many Brothers/Sisters each students have(with name of sibling) using eloquent.
can you please help me, with controller/model/view.
laravel eloquent
i have two tables
**students :**
id
family_id
name
**family :**
id
father_name
father_civil_id
contact_no
both tables are connected using family_id, i want to get how many Brothers/Sisters each students have(with name of sibling) using eloquent.
can you please help me, with controller/model/view.
laravel eloquent
laravel eloquent
asked Nov 23 '18 at 8:22
iMattiMatt
37
37
you can use relationship
– Dhruv Raval
Nov 23 '18 at 8:30
Are Brothers/Sisters students too?
– TsaiKoga
Nov 23 '18 at 8:44
@TsaiKoga yes all are in student table.
– iMatt
Nov 23 '18 at 9:09
add a comment |
you can use relationship
– Dhruv Raval
Nov 23 '18 at 8:30
Are Brothers/Sisters students too?
– TsaiKoga
Nov 23 '18 at 8:44
@TsaiKoga yes all are in student table.
– iMatt
Nov 23 '18 at 9:09
you can use relationship
– Dhruv Raval
Nov 23 '18 at 8:30
you can use relationship
– Dhruv Raval
Nov 23 '18 at 8:30
Are Brothers/Sisters students too?
– TsaiKoga
Nov 23 '18 at 8:44
Are Brothers/Sisters students too?
– TsaiKoga
Nov 23 '18 at 8:44
@TsaiKoga yes all are in student table.
– iMatt
Nov 23 '18 at 9:09
@TsaiKoga yes all are in student table.
– iMatt
Nov 23 '18 at 9:09
add a comment |
2 Answers
2
active
oldest
votes
Let's say you have a Student and Family model. Then you have several ways to do it.
Here are the two easiest ones I can guess with the information you provided.
Without relationship
Controller
Student::where('family_id', $family_id)->get();
HasMany relationship
Family model
class Family extends Model
{
// Since Laravel will expect your table to be 'families'
protected $table = 'family';
public function students()
{
return $this->hasMany(Student::class);
}
}
Controller
$family = Family::with('students')->inRandomOrder()->first();
$siblings = $family->students;
can you please tell me how to call $sibling names in view?
– iMatt
Nov 23 '18 at 9:41
return view('your-view')->with('siblings, $siblings);should do the trick. You can see more on Laravel's view documentation
– P. Ellul
Nov 23 '18 at 9:46
please check; Controller: $showdata = Student::find($id); //to find all students $sibling=Student::where('family_id', $family_id)->get(); return View('students.profile')->with('showdata', $showdata)->with('sibling', $sibling); ** i got error $family_id not defined, can you please tell me why?
– iMatt
Nov 23 '18 at 9:53
Because you don't have$family_iddefined in your code. It was just for the example :)
– P. Ellul
Nov 23 '18 at 9:54
why you have declared $siblings = $family->students; in has many controller?
– iMatt
Nov 23 '18 at 10:32
|
show 1 more comment
In Students Model:
public function family() {
return $this->belongsTo('AppModelsFamily');
}
public function getSiblings() {
return $this->family->students;
}
So you can call it by your students:
$student->getSiblings();
can you please tell me ; $student->getSiblings(); how to save this in controller and show in view?
– iMatt
Nov 23 '18 at 10:33
with your code i get this error, please check; Class 'AppModelsFamily' not found"
– iMatt
Nov 23 '18 at 10:45
the class depend on your family model namespace, I think yours are 'AppFamily'
– TsaiKoga
Nov 23 '18 at 15:18
how to access sibling if there is any on each student profile in view?
– iMatt
Nov 23 '18 at 15:23
In your controller actionprofileyou can use$student = Student::find($id);, and return to renderreturn view("profile", compact('student'));. And in your profile view, you can just use$student->getSiblings()
– TsaiKoga
Nov 23 '18 at 15:40
|
show 6 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442945%2flaravel-eloquent-query-to-check-how-many-siblings-student-have%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Let's say you have a Student and Family model. Then you have several ways to do it.
Here are the two easiest ones I can guess with the information you provided.
Without relationship
Controller
Student::where('family_id', $family_id)->get();
HasMany relationship
Family model
class Family extends Model
{
// Since Laravel will expect your table to be 'families'
protected $table = 'family';
public function students()
{
return $this->hasMany(Student::class);
}
}
Controller
$family = Family::with('students')->inRandomOrder()->first();
$siblings = $family->students;
can you please tell me how to call $sibling names in view?
– iMatt
Nov 23 '18 at 9:41
return view('your-view')->with('siblings, $siblings);should do the trick. You can see more on Laravel's view documentation
– P. Ellul
Nov 23 '18 at 9:46
please check; Controller: $showdata = Student::find($id); //to find all students $sibling=Student::where('family_id', $family_id)->get(); return View('students.profile')->with('showdata', $showdata)->with('sibling', $sibling); ** i got error $family_id not defined, can you please tell me why?
– iMatt
Nov 23 '18 at 9:53
Because you don't have$family_iddefined in your code. It was just for the example :)
– P. Ellul
Nov 23 '18 at 9:54
why you have declared $siblings = $family->students; in has many controller?
– iMatt
Nov 23 '18 at 10:32
|
show 1 more comment
Let's say you have a Student and Family model. Then you have several ways to do it.
Here are the two easiest ones I can guess with the information you provided.
Without relationship
Controller
Student::where('family_id', $family_id)->get();
HasMany relationship
Family model
class Family extends Model
{
// Since Laravel will expect your table to be 'families'
protected $table = 'family';
public function students()
{
return $this->hasMany(Student::class);
}
}
Controller
$family = Family::with('students')->inRandomOrder()->first();
$siblings = $family->students;
can you please tell me how to call $sibling names in view?
– iMatt
Nov 23 '18 at 9:41
return view('your-view')->with('siblings, $siblings);should do the trick. You can see more on Laravel's view documentation
– P. Ellul
Nov 23 '18 at 9:46
please check; Controller: $showdata = Student::find($id); //to find all students $sibling=Student::where('family_id', $family_id)->get(); return View('students.profile')->with('showdata', $showdata)->with('sibling', $sibling); ** i got error $family_id not defined, can you please tell me why?
– iMatt
Nov 23 '18 at 9:53
Because you don't have$family_iddefined in your code. It was just for the example :)
– P. Ellul
Nov 23 '18 at 9:54
why you have declared $siblings = $family->students; in has many controller?
– iMatt
Nov 23 '18 at 10:32
|
show 1 more comment
Let's say you have a Student and Family model. Then you have several ways to do it.
Here are the two easiest ones I can guess with the information you provided.
Without relationship
Controller
Student::where('family_id', $family_id)->get();
HasMany relationship
Family model
class Family extends Model
{
// Since Laravel will expect your table to be 'families'
protected $table = 'family';
public function students()
{
return $this->hasMany(Student::class);
}
}
Controller
$family = Family::with('students')->inRandomOrder()->first();
$siblings = $family->students;
Let's say you have a Student and Family model. Then you have several ways to do it.
Here are the two easiest ones I can guess with the information you provided.
Without relationship
Controller
Student::where('family_id', $family_id)->get();
HasMany relationship
Family model
class Family extends Model
{
// Since Laravel will expect your table to be 'families'
protected $table = 'family';
public function students()
{
return $this->hasMany(Student::class);
}
}
Controller
$family = Family::with('students')->inRandomOrder()->first();
$siblings = $family->students;
edited Nov 23 '18 at 9:23
answered Nov 23 '18 at 9:15
P. EllulP. Ellul
608718
608718
can you please tell me how to call $sibling names in view?
– iMatt
Nov 23 '18 at 9:41
return view('your-view')->with('siblings, $siblings);should do the trick. You can see more on Laravel's view documentation
– P. Ellul
Nov 23 '18 at 9:46
please check; Controller: $showdata = Student::find($id); //to find all students $sibling=Student::where('family_id', $family_id)->get(); return View('students.profile')->with('showdata', $showdata)->with('sibling', $sibling); ** i got error $family_id not defined, can you please tell me why?
– iMatt
Nov 23 '18 at 9:53
Because you don't have$family_iddefined in your code. It was just for the example :)
– P. Ellul
Nov 23 '18 at 9:54
why you have declared $siblings = $family->students; in has many controller?
– iMatt
Nov 23 '18 at 10:32
|
show 1 more comment
can you please tell me how to call $sibling names in view?
– iMatt
Nov 23 '18 at 9:41
return view('your-view')->with('siblings, $siblings);should do the trick. You can see more on Laravel's view documentation
– P. Ellul
Nov 23 '18 at 9:46
please check; Controller: $showdata = Student::find($id); //to find all students $sibling=Student::where('family_id', $family_id)->get(); return View('students.profile')->with('showdata', $showdata)->with('sibling', $sibling); ** i got error $family_id not defined, can you please tell me why?
– iMatt
Nov 23 '18 at 9:53
Because you don't have$family_iddefined in your code. It was just for the example :)
– P. Ellul
Nov 23 '18 at 9:54
why you have declared $siblings = $family->students; in has many controller?
– iMatt
Nov 23 '18 at 10:32
can you please tell me how to call $sibling names in view?
– iMatt
Nov 23 '18 at 9:41
can you please tell me how to call $sibling names in view?
– iMatt
Nov 23 '18 at 9:41
return view('your-view')->with('siblings, $siblings); should do the trick. You can see more on Laravel's view documentation– P. Ellul
Nov 23 '18 at 9:46
return view('your-view')->with('siblings, $siblings); should do the trick. You can see more on Laravel's view documentation– P. Ellul
Nov 23 '18 at 9:46
please check; Controller: $showdata = Student::find($id); //to find all students $sibling=Student::where('family_id', $family_id)->get(); return View('students.profile')->with('showdata', $showdata)->with('sibling', $sibling); ** i got error $family_id not defined, can you please tell me why?
– iMatt
Nov 23 '18 at 9:53
please check; Controller: $showdata = Student::find($id); //to find all students $sibling=Student::where('family_id', $family_id)->get(); return View('students.profile')->with('showdata', $showdata)->with('sibling', $sibling); ** i got error $family_id not defined, can you please tell me why?
– iMatt
Nov 23 '18 at 9:53
Because you don't have
$family_id defined in your code. It was just for the example :)– P. Ellul
Nov 23 '18 at 9:54
Because you don't have
$family_id defined in your code. It was just for the example :)– P. Ellul
Nov 23 '18 at 9:54
why you have declared $siblings = $family->students; in has many controller?
– iMatt
Nov 23 '18 at 10:32
why you have declared $siblings = $family->students; in has many controller?
– iMatt
Nov 23 '18 at 10:32
|
show 1 more comment
In Students Model:
public function family() {
return $this->belongsTo('AppModelsFamily');
}
public function getSiblings() {
return $this->family->students;
}
So you can call it by your students:
$student->getSiblings();
can you please tell me ; $student->getSiblings(); how to save this in controller and show in view?
– iMatt
Nov 23 '18 at 10:33
with your code i get this error, please check; Class 'AppModelsFamily' not found"
– iMatt
Nov 23 '18 at 10:45
the class depend on your family model namespace, I think yours are 'AppFamily'
– TsaiKoga
Nov 23 '18 at 15:18
how to access sibling if there is any on each student profile in view?
– iMatt
Nov 23 '18 at 15:23
In your controller actionprofileyou can use$student = Student::find($id);, and return to renderreturn view("profile", compact('student'));. And in your profile view, you can just use$student->getSiblings()
– TsaiKoga
Nov 23 '18 at 15:40
|
show 6 more comments
In Students Model:
public function family() {
return $this->belongsTo('AppModelsFamily');
}
public function getSiblings() {
return $this->family->students;
}
So you can call it by your students:
$student->getSiblings();
can you please tell me ; $student->getSiblings(); how to save this in controller and show in view?
– iMatt
Nov 23 '18 at 10:33
with your code i get this error, please check; Class 'AppModelsFamily' not found"
– iMatt
Nov 23 '18 at 10:45
the class depend on your family model namespace, I think yours are 'AppFamily'
– TsaiKoga
Nov 23 '18 at 15:18
how to access sibling if there is any on each student profile in view?
– iMatt
Nov 23 '18 at 15:23
In your controller actionprofileyou can use$student = Student::find($id);, and return to renderreturn view("profile", compact('student'));. And in your profile view, you can just use$student->getSiblings()
– TsaiKoga
Nov 23 '18 at 15:40
|
show 6 more comments
In Students Model:
public function family() {
return $this->belongsTo('AppModelsFamily');
}
public function getSiblings() {
return $this->family->students;
}
So you can call it by your students:
$student->getSiblings();
In Students Model:
public function family() {
return $this->belongsTo('AppModelsFamily');
}
public function getSiblings() {
return $this->family->students;
}
So you can call it by your students:
$student->getSiblings();
answered Nov 23 '18 at 9:51
TsaiKogaTsaiKoga
438310
438310
can you please tell me ; $student->getSiblings(); how to save this in controller and show in view?
– iMatt
Nov 23 '18 at 10:33
with your code i get this error, please check; Class 'AppModelsFamily' not found"
– iMatt
Nov 23 '18 at 10:45
the class depend on your family model namespace, I think yours are 'AppFamily'
– TsaiKoga
Nov 23 '18 at 15:18
how to access sibling if there is any on each student profile in view?
– iMatt
Nov 23 '18 at 15:23
In your controller actionprofileyou can use$student = Student::find($id);, and return to renderreturn view("profile", compact('student'));. And in your profile view, you can just use$student->getSiblings()
– TsaiKoga
Nov 23 '18 at 15:40
|
show 6 more comments
can you please tell me ; $student->getSiblings(); how to save this in controller and show in view?
– iMatt
Nov 23 '18 at 10:33
with your code i get this error, please check; Class 'AppModelsFamily' not found"
– iMatt
Nov 23 '18 at 10:45
the class depend on your family model namespace, I think yours are 'AppFamily'
– TsaiKoga
Nov 23 '18 at 15:18
how to access sibling if there is any on each student profile in view?
– iMatt
Nov 23 '18 at 15:23
In your controller actionprofileyou can use$student = Student::find($id);, and return to renderreturn view("profile", compact('student'));. And in your profile view, you can just use$student->getSiblings()
– TsaiKoga
Nov 23 '18 at 15:40
can you please tell me ; $student->getSiblings(); how to save this in controller and show in view?
– iMatt
Nov 23 '18 at 10:33
can you please tell me ; $student->getSiblings(); how to save this in controller and show in view?
– iMatt
Nov 23 '18 at 10:33
with your code i get this error, please check; Class 'AppModelsFamily' not found"
– iMatt
Nov 23 '18 at 10:45
with your code i get this error, please check; Class 'AppModelsFamily' not found"
– iMatt
Nov 23 '18 at 10:45
the class depend on your family model namespace, I think yours are 'AppFamily'
– TsaiKoga
Nov 23 '18 at 15:18
the class depend on your family model namespace, I think yours are 'AppFamily'
– TsaiKoga
Nov 23 '18 at 15:18
how to access sibling if there is any on each student profile in view?
– iMatt
Nov 23 '18 at 15:23
how to access sibling if there is any on each student profile in view?
– iMatt
Nov 23 '18 at 15:23
In your controller action
profile you can use $student = Student::find($id); , and return to render return view("profile", compact('student')); . And in your profile view, you can just use $student->getSiblings()– TsaiKoga
Nov 23 '18 at 15:40
In your controller action
profile you can use $student = Student::find($id); , and return to render return view("profile", compact('student')); . And in your profile view, you can just use $student->getSiblings()– TsaiKoga
Nov 23 '18 at 15:40
|
show 6 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442945%2flaravel-eloquent-query-to-check-how-many-siblings-student-have%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you can use relationship
– Dhruv Raval
Nov 23 '18 at 8:30
Are Brothers/Sisters students too?
– TsaiKoga
Nov 23 '18 at 8:44
@TsaiKoga yes all are in student table.
– iMatt
Nov 23 '18 at 9:09