Fetching Data from another table using foreign key laravel
Fetching Data from another table using foreign key laravel
I have 2 tables attendance and student. I am trying to retrieve my stud_name from student table by using the student foreign key in attendance table. I have a controller that return the view of all the results from attendance model. I have also added the relationship in both student and attendance model but whenever i try accessing the view i got an error exception of Trying to get property 'stud_name' of non-object. Anyone able to help me?
attendance
student
stud_name
student
student
attendance
attendance
student
attendance
AttendanceController
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppAttendance;
class GenerateReportController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
$attendance = Attendance::with('student')->get();
return view('generate')->with('attendance',$attendance);
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function show($id)
{
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param IlluminateHttpRequest $request
* @param int $id
* @return IlluminateHttpResponse
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function destroy($id)
{
//
}
}
Student.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Student extends Model
{
//Table
protected $table = 'students';
//Primary Key
public $primaryKey = 'id';
//Timestamps
public $timestamp = true;
public function programme(){
return $this->belongsTo('AppProgramme');
}
public function attendance(){
return $this->hasMany('AppAttendance');
}
}
Attendance.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Attendance extends Model
{
//Table
protected $table = 'attendance';
//Primary Key
public $primaryKey = 'id';
//Timestamps
public $timestamp = true;
protected $fillable = [
'att_status','date','time',
];
public function student()
{
return $this->belongsTo('AppStudent','s_id');
}
Blade File
@foreach($attendance as $att)
<tr>
<td>{{$att->stud_Id->stud_name}}</td>
//Other Data
</tr>
@endforeach
ADDITIONAL INFOMATION
Student Table
Attendance Table
The reason all the primary key is named 'id' is because i am using voyager and it doesnt allow overriding of primary key name.
dd($attendance)

2 Answers
2
I think you need to make changes in this line of code
public function index()
{
$attendance = Attendance::with('student')->get();
return view('generate')->with('attendance',$attendance);
}
@foreach($attendance as $att)
<tr>
<td>{{$att->student->stud_name}}</td>
//Other Data
</tr>
@endforeach
Attendance.php
public function student()
{
return $this->belongsTo('AppStudent','stud_id');
}
Please let me know if you have any queries.
@DexterSiah Sorry my mistake, I have update the answer, please check and let me know
– Avinash Singh Rathi
Jun 30 at 2:39
hi unfortunately i am still getting the error "Trying to get property 'stud_name' of non-object.". I have added some addtional pictures to show what is inside my student and attendance table hope u can help me thanks
– Dexter Siah
Jun 30 at 5:33
@DexterSiah I've done the changes, please check and let me know
– Avinash Singh Rathi
Jun 30 at 6:42
nope...it still doesnt work... any ideas why?
– Dexter Siah
Jun 30 at 6:50
I think you wanna try:
@foreach($attendance as $att)
<tr>
<td>{{$att->student->stud_name}}</td>
//Other Data
</tr>
@endforeach
Also check out the docs.
In addition: I personally wouldn't call the property on the model student that holds the name stud_name. I'd call it name: the fact that it's a students name should be clear from the model name in combination with the name name.
student
stud_name
name
name
@Nielles sorry but im ensure of what u meant by not naming it stud_name instead of name. Because i am using laravel + voyager i am worried that if any changes i made in the model might affect my other tables
– Dexter Siah
Jun 30 at 2:37
@DexterSiah First of all, did this solution work? 2nd: What I meant is: you have a model called
student. This student has a name, for me it would be logical to call this property (both in the model and in the database) name and not stud_name. It should be clear from the fact that it's a students name that this is a stud(ents)_name. It's more of a style thing, that might be nice to keep in mind for the future. It might also have too many implications for your current code to change this... although you should be able to refactor most automatically with a decent IDE.– Niellles
Jun 30 at 7:09
student
name
stud_name
stud(ents)_name
@Nielles unfortunately no the solution didnt work for me i still had the same error stating "Trying to get property of non-object". Alright understood i will change into
name in my database but how would i change it in the model? Do i need to declare my column names in all my model too? Sorry for asking such a question im still new and doing this for an assignment– Dexter Siah
Jun 30 at 8:08
name
You don't necessarily need to declare these names in your model, eloquent (part of Laravel) does this automatically... The properties that you can access in a retrieved model will change though. I am thinking there's something wrong with your database scheme (i.e. migration), more specifically that the foreign keys aren't correctly defined, causing eloquent to not correctly define the relationship. Can you post the relevant parts of
dd($att) as well as the way you created your database table(s) (migration, probably/hopefully).– Niellles
Jun 30 at 8:31
dd($att)
Additionally: "Remember, Eloquent will automatically determine the proper foreign key column on the Comment model. By convention, Eloquent will take the "snake case" name of the owning model and suffix it with _id. So, for this example, Eloquent will assume the foreign key on the Comment model is post_id." may be an interesting part from the docs, as well as the rest of the documentation on relationships.
– Niellles
Jun 30 at 8:33
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
hi i have tried the changed of code u added but it seems like im getting an error "Method IlluminateDatabaseQueryBuilder::all does not exist."
– Dexter Siah
Jun 30 at 2:35