Laravel Relationship not working on server - laravel

My project is working fine on local host, but on the server, getting error only on lesson page.
Subject Model is
class Subjects extends Model
{
public function category()
{
return $this->belongsTo('Lea\Category');
}
public function lesson()
{
return $this->hasMany('Lea\Lessons');
}
public function chapter()
{
return $this->hasMany('Lea\Chapters');
}
public function users()
{
return $this->belongsToMany('Lea\User', 'subject_user', 'subject_id', 'user_id');
}
}
And Lesson Model is
class Lessons extends Model
{
public function chapter()
{
return $this->belongsTo('Lea\Chapters');
}
public function subject()
{
return $this->belongsTo('Lea\Subjects', 'subject_id');
}
public function category()
{
return $this->belongsTo('Lea\Category');
}
}
My controller lessons has method index like below.
public function index()
{
$lessons = lessons::orderBy('id', 'asc')->paginate(5);
return view('admin.lessons.index')->withLessons($lessons);
}
The way i am calling subject name is show below. first subject is relationship subject and 2nd subject is the field name where subject name saved in db. using foreach to get data like:
#foreach ($lessons as $lesson)
{{$lesson->subject->subject}}
#endforeach
subjects table schema is
id
subject
admin_id
users_id
category_id
created_at
updated_at
lessons table schema is
id
title
slug
category_id
subject_id
chapter_id
users_id
content
image

That's more like it ;)
Just edit how you are passing the object to the following:
...
return view('admin.lessons.index')->with(compact('lessons'));
What this is doing:
compact() essentially takes the object/variable by it's name ($lessons) and passes it to the view with the same name.
An alternative way is to write it as:
...
return view('admin.lessons.index')->with('lessons', $lessons);
Cheers!

The reason you're getting the error in your server but not your local machine is that your server database is empty (or at least has fewer records).
{{$lesson->subject->subject}}
The lesson has no subject, so it's null. And your tryting to get a proprty subject of a null.
If you're using Laravel 5.5, you can wrap the subject in optional() method.
{{optional($lesson->subject)->subject}}
< Laravel 5.5
{{$lesson->subject ? $lesson->subject->subject : ''}}

Related

Laravel query builder with the User and Auth

How to List all rows from a table (agendas) in my DB where records are saved by the connected user.I'm using default Auth from Laravel.
public function index ($id = null)
{
$agendas = Agenda::where('id', Auth::user()->id)->get();
$users = User::all();
return view('admin.agendas.index', compact('agendas','users','id'));
}
My controller here.
Need help
Assuming
agendas table (containing records for Agenda model) has a column user_id which references the id column on users table
User hasMany Agenda
Agenda belongTo User
class User extends Model
{
public function agendas()
{
return $this->hasMany(Agenda::class);
}
//... rest of class code
}
class Agenda extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
//... rest of class code
}
public function index($id = null)
{
$user = User::with('agendas')->findOrFail(auth()->id());
return view('admin.agendas.index', [
'user' => $user,
'agendas' => $user->agendas,
'id' => $id
]);
}
Your User model should have a relationship:
public function agendas()
{
return $this->hasMany(Agenda::class);
}
In your controller, you could then simplify the use as such:
public function index (Request $request, $id = null)
{
$agendas = $request->user()->agendas;
$users = User::all();
return view('admin.agendas.index', compact('agendas','users','id'));
}
if you wanna get data related to another data , you have to join those tables together by a field. in this case i guess you have a column in Agenda table named 'user_id'.
you have two way :
join tables in your Model.
search in Agenda table in your controller
if you want to use joining your tables from model :
// in App\User.php
...
class User ...{
...
public function Agenda(){
return $this->hasMany(Agenda::class);
}
...
}
...
then you can access to all of "Agenda" from everywhere like this :
Auth()->user()->agenda
if you want to search in table from your controller you can do :
Agenda::where('id', Auth::user()->id)
you can read more about eloquent-relationships in : https://laravel.com/docs/8.x/eloquent-relationships

laravel model controller retrieve data

I'am new to laravel and had a hard time for eloquent ORM, I like to retrieve Favourite items from the table with specific user id, but seem I don't find a way to display the data correctly.
I refer and copied the favourite model code from here
Model: Favourite
class Favourite extends BaseModel
{
public function favouritable()
{
return $this->morphTo();
}
}
Model: Course
public function favourites()
{
return $this->morphMany(Favourite::class, 'favouritable');
}
public function myFavourite()
{
return $this->favourites()->where('favor_user', '=', auth()->user()->id);
}
Controller: FavouriteController#index
public function index()
{
$course = new Course();
$myfavs = $course->myFavourite();
//dd($myfavs);
$this->vdata(compact('myfavs'));
return view('front.favorites', $this->vdata);
}
In blade view, but return empty/blank data:
#foreach($myfavs as $myfav)
{{ $myfav->$course_name }}
#endforeach
When i dd($myfavs) it display pretty bunches of data that i couldn't find the data that is stored in the favourites table.
I think you have added the conditions but forgot get the results with one of the methods to do so.
Try :
$myfavs = $course->myFavourite()->get();
Or :
$myfavs = $course->myFavourite()->take(<N>)->get;
Or
::all()
etc. take a look at : https://laravel.com/docs/5.8/eloquent#retrieving-models

How to retrieve details of second level tables in laravel relationships

I have 3 tables
bank(id, title),
employee(id, name, bank_id),
payroll(id, employee_id, salary).
Now I want to retrieve bank title of employee_id in payroll table.
I have set model relationships
class Bank extends Model
{
public function employees()
{
return $this->hasMany('App\Employee');
}
}
class Employee extends Model
{
public function bank()
{
return $this->belongsTo('App\Bank');
}
public function payrolls()
{
return $this->hasMany('App\Payroll');
}
}
class Payroll extends Model
{
public function employee()
{
return $this->belongsTo('App\Employee');
}
}
I have tried to retrieve using $payroll->employee->bank->title. But it did not help me
Sorry it was my mistake. some employee ids are not referencing bank. That was why I got error.
I resolved the issue by using isset method to verify reference value set or not.
Thanks.
try to change your code a little bit and try again:
public function bank()
{
return $this->hasOne('App\Bank');
}
Read Laravel Official Documentation
/** We can define the inverse of a hasOne relationship using the belongsTo method: **/

Showing data through foreign keys

I am trying to get some data through relationships and foreign keys but have gotten myself in a tangle.
I have a Batsmen who user can comment on,the comments can also be edited by the users, the comments have their own page aswell with the option to delete or edit the comment. This is where my problem comes, i am trying to show the nationality of the batsmen but can only retrieve the ID of his nationallity and not the name of the country.
Here are my tables
Batsmen: ID, Name, bio, Nationality_ID,
Nationality: ID, country_name,
User ID, name ,
Comment: ID, comment, Batsmen_ID, User_ID
My Relationships
Comment:
public function batsmen(){
return $this->belongsTo('App\Batsmen');
}
public function author()
{
return $this->belongsTo('App\User','user_id');
}
Controller:
public function show($id)
{
$comment = Comment::find($id);
return view('comments.show')->with('comment', $comment);
}
View :
<h4>{{$comment->batsmen->Nationality_id}}</h4>
So how would i get the country name to display instead of the nationality_ID?
Make another relationship method on your Batsman model class as -
public function nationality(){
return $this->belongsTo('App\Nationality','Nationality_id');
}
And then in your view-
<h4>{{$comment->batsmen->nationality->country_name}}</h4>
try this
public function show($id)
{
$comment = Comment::with('batsmen')->find($id);
return view('comments.show')->with('comment', $comment);
}
You need to add the relationship between bastman and nationality
class Comment extends Model
{
public function nationality(){
return $this->belongsTo('App\Nationality',Nationality_id,id);
}
}
Then you can easily get his country
<h4>{{$comment->batsmen->Nationality->country_name}}</h4>

Laravel Nested Relationship Where 5.5 Return All Records

I'm having some trouble when trying to fetch records from database. Here's the table schema:
users
--------------
id
username
password
email
divisions
--------------
id
name
employee
--------------
name
birth_date
status
class
division_id
user_id
projects
--------------
id
title
body
user_id
So, for the relationship explanations:
relationship
Okay, i'm trying to fetch project based on division_id on table employee with the following code:
# query code
$division_id = 10;
$items = Project::with(['user.employee.division' => function($query) use ($division_id) {
$query->where('id', $division_id);
}])->get();
I've added the required belongsTo, hasMany or hasOne to the models.
# User.php
class User extends Authenticatable
{
public function employee()
{
return $this->hasOne('App\Employee', 'user_id');
}
public function projects()
{
return $this->hasMany('App\Project', 'user_id');
}
}
# Division.php
class Division extends Model
{
public function employee()
{
return $this->hasMany('App\Employee', 'division_id');
}
}
# Employee
class Employee extends Model
{
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
public function division()
{
return $this->belongsTo('App\Division', 'division_id');
}
}
# Project.php
class Project extends Model
{
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
}
So, what's the problem?
Here's the thing, when i run the query code i'm getting all the records and the division object on employe relationship returning null.
If anyone thinks my code is wrong, please enlighten me.
Thanks.
So after some digging, i found an answer. The query code should change to whereHas:
# query code
$division_id = 10;
$items = App\Project::whereHas('user.employee.division',
function($query) use ($division_id) {
$query->where('divisions.id', $division_id);
})
->with(['user.employee.division']) // N+1 problem
->get();
reference: Laravel 5.3 Constraining Eager Loads not working

Resources