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.
laravel eloquent
add a comment |
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.
laravel eloquent
define relationship function in Student model: public function family() { return $this->belongsTo('AppFamily'); }
– Ali
Nov 13 at 6:42
add a comment |
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.
laravel eloquent
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
laravel eloquent
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
add a comment |
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
add a comment |
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.
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$studengbut 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_noetc. 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
add a comment |
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 }}
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
add a comment |
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.
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$studengbut 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_noetc. 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
add a comment |
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.
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$studengbut 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_noetc. 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
add a comment |
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.
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.
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$studengbut 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_noetc. 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
add a comment |
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$studengbut 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_noetc. 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
add a comment |
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 }}
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
add a comment |
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 }}
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
add a comment |
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 }}
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 }}
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
add a comment |
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
add a comment |
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%2f53275119%2flaravel-eloquent-one-to-many-relationship-data-not-showing%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
define relationship function in Student model: public function family() { return $this->belongsTo('AppFamily'); }
– Ali
Nov 13 at 6:42