Laravel eloquent one to many relationship data not showing











up vote
0
down vote

favorite












i have two tables,
Student table
which have column (id, family_id,name,class,section)
Family table
which have column (family_id, mobile_no, profession)



--
i have created two models.
student model:



class student extends Model
{

}


and
family model



class family extends Model
{
public function student()
{
return $this->hasMany('Appstudent');

}
}


--
i am able to show all data from student table,
my controller is:



public function index()
{
$finddata = student::orderBy('id', 'asc')->get();
return view('students.index')->with('finddata', $finddata); }


--
what i tried
in family model:



return $this->hasMany('Appstudents');



what i want;




  • i want to connect family model with student model..

  • index page will have all students name only. which i have already done

  • when i click on student name it should show, all information about particular student, and his family information. right now it's only showing information from student table.










share|improve this question






















  • define relationship function in Student model: public function family() { return $this->belongsTo('AppFamily'); }
    – Ali
    Nov 13 at 6:42















up vote
0
down vote

favorite












i have two tables,
Student table
which have column (id, family_id,name,class,section)
Family table
which have column (family_id, mobile_no, profession)



--
i have created two models.
student model:



class student extends Model
{

}


and
family model



class family extends Model
{
public function student()
{
return $this->hasMany('Appstudent');

}
}


--
i am able to show all data from student table,
my controller is:



public function index()
{
$finddata = student::orderBy('id', 'asc')->get();
return view('students.index')->with('finddata', $finddata); }


--
what i tried
in family model:



return $this->hasMany('Appstudents');



what i want;




  • i want to connect family model with student model..

  • index page will have all students name only. which i have already done

  • when i click on student name it should show, all information about particular student, and his family information. right now it's only showing information from student table.










share|improve this question






















  • define relationship function in Student model: public function family() { return $this->belongsTo('AppFamily'); }
    – Ali
    Nov 13 at 6:42













up vote
0
down vote

favorite









up vote
0
down vote

favorite











i have two tables,
Student table
which have column (id, family_id,name,class,section)
Family table
which have column (family_id, mobile_no, profession)



--
i have created two models.
student model:



class student extends Model
{

}


and
family model



class family extends Model
{
public function student()
{
return $this->hasMany('Appstudent');

}
}


--
i am able to show all data from student table,
my controller is:



public function index()
{
$finddata = student::orderBy('id', 'asc')->get();
return view('students.index')->with('finddata', $finddata); }


--
what i tried
in family model:



return $this->hasMany('Appstudents');



what i want;




  • i want to connect family model with student model..

  • index page will have all students name only. which i have already done

  • when i click on student name it should show, all information about particular student, and his family information. right now it's only showing information from student table.










share|improve this question













i have two tables,
Student table
which have column (id, family_id,name,class,section)
Family table
which have column (family_id, mobile_no, profession)



--
i have created two models.
student model:



class student extends Model
{

}


and
family model



class family extends Model
{
public function student()
{
return $this->hasMany('Appstudent');

}
}


--
i am able to show all data from student table,
my controller is:



public function index()
{
$finddata = student::orderBy('id', 'asc')->get();
return view('students.index')->with('finddata', $finddata); }


--
what i tried
in family model:



return $this->hasMany('Appstudents');



what i want;




  • i want to connect family model with student model..

  • index page will have all students name only. which i have already done

  • when i click on student name it should show, all information about particular student, and his family information. right now it's only showing information from student table.







laravel eloquent






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 at 6:35









iMatt

37




37












  • define relationship function in Student model: public function family() { return $this->belongsTo('AppFamily'); }
    – Ali
    Nov 13 at 6:42


















  • define relationship function in Student model: public function family() { return $this->belongsTo('AppFamily'); }
    – Ali
    Nov 13 at 6:42
















define relationship function in Student model: public function family() { return $this->belongsTo('AppFamily'); }
– Ali
Nov 13 at 6:42




define relationship function in Student model: public function family() { return $this->belongsTo('AppFamily'); }
– Ali
Nov 13 at 6:42












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










In the student model:



class student extends Model
{
public function family(){
return $this->belongsTo(Family::class,'family_id');
}

}


Then you can access family information in Blade by:



{{$student->family->mobile_no}}
{{$student->family->profession}}


You should better use artisan commands to generate models and controllers, so that you get automatic templates that follow naming conventions, such as uppercase Class names.






share|improve this answer





















  • i tried your solution, but i got this error, "Undefined variable: $student.. can you please suggest correct revision. for controller and blade. please keep in mind i was able to show all data from student table.
    – iMatt
    Nov 13 at 7:02












  • @iMatt, in your blade, what is the variable for a single student? I assume $studeng but you should use the variable in your blade loop. If possible please paste your working blade template.
    – hktang
    Nov 13 at 7:06










  • ok here is my controller, and blade. ___ Controller: public function index() { $finddata = Student::orderBy('id', 'asc')->get(); return view('students.index')->with('finddata', $finddata); } ________________ Blade: for index: <a href="student/{{$finddata->id}}">{{$finddata->name}}</a> <p class="card-text">{{$finddata->id}}</p> above blade is showing all students name and id... when i click on any name it is showing more information like class,section etc. in show blade... there in show blade want to see family information too.
    – iMatt
    Nov 13 at 7:14










  • Then it should be $finddata->family->mobile_no etc. Again, I suggest you use standard names like students, student etc to avoid confusion in the future.
    – hktang
    Nov 13 at 7:17






  • 1




    Thank you, it worked. :)))) cheers
    – iMatt
    Nov 13 at 7:42


















up vote
1
down vote













You should try this



class student extends Model
{

public function familyId(){
return $this->belongsTo(Family::class,'family_id');
}

}


when you click on student name it will redirect to student/{id}



route



Route::get('student/{id}', 'StudentController@show');



student controller



public function show(Request $request, $id)
{
$student = student::find($id);
return view('student.show')->with('student', $student);
}


student.show.blade file



Mobile no: {{ $student->familyId->mobile_no }}
Profession: {{ $student->familyId->profession }}





share|improve this answer























  • i already have controller to find data which i mentioned in question, i just want to show family information, when i click on student name. can you please suggest code for controller and blade?
    – iMatt
    Nov 13 at 7:05










  • @iMatt answer updated please check
    – Saurabh Dhariwal
    Nov 13 at 7:12











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275119%2flaravel-eloquent-one-to-many-relationship-data-not-showing%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








up vote
0
down vote



accepted










In the student model:



class student extends Model
{
public function family(){
return $this->belongsTo(Family::class,'family_id');
}

}


Then you can access family information in Blade by:



{{$student->family->mobile_no}}
{{$student->family->profession}}


You should better use artisan commands to generate models and controllers, so that you get automatic templates that follow naming conventions, such as uppercase Class names.






share|improve this answer





















  • i tried your solution, but i got this error, "Undefined variable: $student.. can you please suggest correct revision. for controller and blade. please keep in mind i was able to show all data from student table.
    – iMatt
    Nov 13 at 7:02












  • @iMatt, in your blade, what is the variable for a single student? I assume $studeng but you should use the variable in your blade loop. If possible please paste your working blade template.
    – hktang
    Nov 13 at 7:06










  • ok here is my controller, and blade. ___ Controller: public function index() { $finddata = Student::orderBy('id', 'asc')->get(); return view('students.index')->with('finddata', $finddata); } ________________ Blade: for index: <a href="student/{{$finddata->id}}">{{$finddata->name}}</a> <p class="card-text">{{$finddata->id}}</p> above blade is showing all students name and id... when i click on any name it is showing more information like class,section etc. in show blade... there in show blade want to see family information too.
    – iMatt
    Nov 13 at 7:14










  • Then it should be $finddata->family->mobile_no etc. Again, I suggest you use standard names like students, student etc to avoid confusion in the future.
    – hktang
    Nov 13 at 7:17






  • 1




    Thank you, it worked. :)))) cheers
    – iMatt
    Nov 13 at 7:42















up vote
0
down vote



accepted










In the student model:



class student extends Model
{
public function family(){
return $this->belongsTo(Family::class,'family_id');
}

}


Then you can access family information in Blade by:



{{$student->family->mobile_no}}
{{$student->family->profession}}


You should better use artisan commands to generate models and controllers, so that you get automatic templates that follow naming conventions, such as uppercase Class names.






share|improve this answer





















  • i tried your solution, but i got this error, "Undefined variable: $student.. can you please suggest correct revision. for controller and blade. please keep in mind i was able to show all data from student table.
    – iMatt
    Nov 13 at 7:02












  • @iMatt, in your blade, what is the variable for a single student? I assume $studeng but you should use the variable in your blade loop. If possible please paste your working blade template.
    – hktang
    Nov 13 at 7:06










  • ok here is my controller, and blade. ___ Controller: public function index() { $finddata = Student::orderBy('id', 'asc')->get(); return view('students.index')->with('finddata', $finddata); } ________________ Blade: for index: <a href="student/{{$finddata->id}}">{{$finddata->name}}</a> <p class="card-text">{{$finddata->id}}</p> above blade is showing all students name and id... when i click on any name it is showing more information like class,section etc. in show blade... there in show blade want to see family information too.
    – iMatt
    Nov 13 at 7:14










  • Then it should be $finddata->family->mobile_no etc. Again, I suggest you use standard names like students, student etc to avoid confusion in the future.
    – hktang
    Nov 13 at 7:17






  • 1




    Thank you, it worked. :)))) cheers
    – iMatt
    Nov 13 at 7:42













up vote
0
down vote



accepted







up vote
0
down vote



accepted






In the student model:



class student extends Model
{
public function family(){
return $this->belongsTo(Family::class,'family_id');
}

}


Then you can access family information in Blade by:



{{$student->family->mobile_no}}
{{$student->family->profession}}


You should better use artisan commands to generate models and controllers, so that you get automatic templates that follow naming conventions, such as uppercase Class names.






share|improve this answer












In the student model:



class student extends Model
{
public function family(){
return $this->belongsTo(Family::class,'family_id');
}

}


Then you can access family information in Blade by:



{{$student->family->mobile_no}}
{{$student->family->profession}}


You should better use artisan commands to generate models and controllers, so that you get automatic templates that follow naming conventions, such as uppercase Class names.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 at 6:52









hktang

8701329




8701329












  • i tried your solution, but i got this error, "Undefined variable: $student.. can you please suggest correct revision. for controller and blade. please keep in mind i was able to show all data from student table.
    – iMatt
    Nov 13 at 7:02












  • @iMatt, in your blade, what is the variable for a single student? I assume $studeng but you should use the variable in your blade loop. If possible please paste your working blade template.
    – hktang
    Nov 13 at 7:06










  • ok here is my controller, and blade. ___ Controller: public function index() { $finddata = Student::orderBy('id', 'asc')->get(); return view('students.index')->with('finddata', $finddata); } ________________ Blade: for index: <a href="student/{{$finddata->id}}">{{$finddata->name}}</a> <p class="card-text">{{$finddata->id}}</p> above blade is showing all students name and id... when i click on any name it is showing more information like class,section etc. in show blade... there in show blade want to see family information too.
    – iMatt
    Nov 13 at 7:14










  • Then it should be $finddata->family->mobile_no etc. Again, I suggest you use standard names like students, student etc to avoid confusion in the future.
    – hktang
    Nov 13 at 7:17






  • 1




    Thank you, it worked. :)))) cheers
    – iMatt
    Nov 13 at 7:42


















  • i tried your solution, but i got this error, "Undefined variable: $student.. can you please suggest correct revision. for controller and blade. please keep in mind i was able to show all data from student table.
    – iMatt
    Nov 13 at 7:02












  • @iMatt, in your blade, what is the variable for a single student? I assume $studeng but you should use the variable in your blade loop. If possible please paste your working blade template.
    – hktang
    Nov 13 at 7:06










  • ok here is my controller, and blade. ___ Controller: public function index() { $finddata = Student::orderBy('id', 'asc')->get(); return view('students.index')->with('finddata', $finddata); } ________________ Blade: for index: <a href="student/{{$finddata->id}}">{{$finddata->name}}</a> <p class="card-text">{{$finddata->id}}</p> above blade is showing all students name and id... when i click on any name it is showing more information like class,section etc. in show blade... there in show blade want to see family information too.
    – iMatt
    Nov 13 at 7:14










  • Then it should be $finddata->family->mobile_no etc. Again, I suggest you use standard names like students, student etc to avoid confusion in the future.
    – hktang
    Nov 13 at 7:17






  • 1




    Thank you, it worked. :)))) cheers
    – iMatt
    Nov 13 at 7:42
















i tried your solution, but i got this error, "Undefined variable: $student.. can you please suggest correct revision. for controller and blade. please keep in mind i was able to show all data from student table.
– iMatt
Nov 13 at 7:02






i tried your solution, but i got this error, "Undefined variable: $student.. can you please suggest correct revision. for controller and blade. please keep in mind i was able to show all data from student table.
– iMatt
Nov 13 at 7:02














@iMatt, in your blade, what is the variable for a single student? I assume $studeng but you should use the variable in your blade loop. If possible please paste your working blade template.
– hktang
Nov 13 at 7:06




@iMatt, in your blade, what is the variable for a single student? I assume $studeng but you should use the variable in your blade loop. If possible please paste your working blade template.
– hktang
Nov 13 at 7:06












ok here is my controller, and blade. ___ Controller: public function index() { $finddata = Student::orderBy('id', 'asc')->get(); return view('students.index')->with('finddata', $finddata); } ________________ Blade: for index: <a href="student/{{$finddata->id}}">{{$finddata->name}}</a> <p class="card-text">{{$finddata->id}}</p> above blade is showing all students name and id... when i click on any name it is showing more information like class,section etc. in show blade... there in show blade want to see family information too.
– iMatt
Nov 13 at 7:14




ok here is my controller, and blade. ___ Controller: public function index() { $finddata = Student::orderBy('id', 'asc')->get(); return view('students.index')->with('finddata', $finddata); } ________________ Blade: for index: <a href="student/{{$finddata->id}}">{{$finddata->name}}</a> <p class="card-text">{{$finddata->id}}</p> above blade is showing all students name and id... when i click on any name it is showing more information like class,section etc. in show blade... there in show blade want to see family information too.
– iMatt
Nov 13 at 7:14












Then it should be $finddata->family->mobile_no etc. Again, I suggest you use standard names like students, student etc to avoid confusion in the future.
– hktang
Nov 13 at 7:17




Then it should be $finddata->family->mobile_no etc. Again, I suggest you use standard names like students, student etc to avoid confusion in the future.
– hktang
Nov 13 at 7:17




1




1




Thank you, it worked. :)))) cheers
– iMatt
Nov 13 at 7:42




Thank you, it worked. :)))) cheers
– iMatt
Nov 13 at 7:42












up vote
1
down vote













You should try this



class student extends Model
{

public function familyId(){
return $this->belongsTo(Family::class,'family_id');
}

}


when you click on student name it will redirect to student/{id}



route



Route::get('student/{id}', 'StudentController@show');



student controller



public function show(Request $request, $id)
{
$student = student::find($id);
return view('student.show')->with('student', $student);
}


student.show.blade file



Mobile no: {{ $student->familyId->mobile_no }}
Profession: {{ $student->familyId->profession }}





share|improve this answer























  • i already have controller to find data which i mentioned in question, i just want to show family information, when i click on student name. can you please suggest code for controller and blade?
    – iMatt
    Nov 13 at 7:05










  • @iMatt answer updated please check
    – Saurabh Dhariwal
    Nov 13 at 7:12















up vote
1
down vote













You should try this



class student extends Model
{

public function familyId(){
return $this->belongsTo(Family::class,'family_id');
}

}


when you click on student name it will redirect to student/{id}



route



Route::get('student/{id}', 'StudentController@show');



student controller



public function show(Request $request, $id)
{
$student = student::find($id);
return view('student.show')->with('student', $student);
}


student.show.blade file



Mobile no: {{ $student->familyId->mobile_no }}
Profession: {{ $student->familyId->profession }}





share|improve this answer























  • i already have controller to find data which i mentioned in question, i just want to show family information, when i click on student name. can you please suggest code for controller and blade?
    – iMatt
    Nov 13 at 7:05










  • @iMatt answer updated please check
    – Saurabh Dhariwal
    Nov 13 at 7:12













up vote
1
down vote










up vote
1
down vote









You should try this



class student extends Model
{

public function familyId(){
return $this->belongsTo(Family::class,'family_id');
}

}


when you click on student name it will redirect to student/{id}



route



Route::get('student/{id}', 'StudentController@show');



student controller



public function show(Request $request, $id)
{
$student = student::find($id);
return view('student.show')->with('student', $student);
}


student.show.blade file



Mobile no: {{ $student->familyId->mobile_no }}
Profession: {{ $student->familyId->profession }}





share|improve this answer














You should try this



class student extends Model
{

public function familyId(){
return $this->belongsTo(Family::class,'family_id');
}

}


when you click on student name it will redirect to student/{id}



route



Route::get('student/{id}', 'StudentController@show');



student controller



public function show(Request $request, $id)
{
$student = student::find($id);
return view('student.show')->with('student', $student);
}


student.show.blade file



Mobile no: {{ $student->familyId->mobile_no }}
Profession: {{ $student->familyId->profession }}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 7:23

























answered Nov 13 at 6:45









Saurabh Dhariwal

92212




92212












  • i already have controller to find data which i mentioned in question, i just want to show family information, when i click on student name. can you please suggest code for controller and blade?
    – iMatt
    Nov 13 at 7:05










  • @iMatt answer updated please check
    – Saurabh Dhariwal
    Nov 13 at 7:12


















  • i already have controller to find data which i mentioned in question, i just want to show family information, when i click on student name. can you please suggest code for controller and blade?
    – iMatt
    Nov 13 at 7:05










  • @iMatt answer updated please check
    – Saurabh Dhariwal
    Nov 13 at 7:12
















i already have controller to find data which i mentioned in question, i just want to show family information, when i click on student name. can you please suggest code for controller and blade?
– iMatt
Nov 13 at 7:05




i already have controller to find data which i mentioned in question, i just want to show family information, when i click on student name. can you please suggest code for controller and blade?
– iMatt
Nov 13 at 7:05












@iMatt answer updated please check
– Saurabh Dhariwal
Nov 13 at 7:12




@iMatt answer updated please check
– Saurabh Dhariwal
Nov 13 at 7:12


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53275119%2flaravel-eloquent-one-to-many-relationship-data-not-showing%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

How to send String Array data to Server using php in android

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Is anime1.com a legal site for watching anime?