Reputation: 25
I am developing a multi author blog in laravel 5.5. My scenario is such: Whenever one wants to join the blog website as an author he/she has to send an application form. These applications are to be approved/rejected by one of the editors (amongst the many editors in the system). The Users table contain the editors.
So whenever an application is made, it can be viewed by multiple editors. One of the editors will need to book the application for processing. This is being done in order to prevent multiple editors to be reviewing the same application. Once an editor books the application, he can approve/reject the author application. If he chooses not to work on the application after he has booked the same, he has to release it so that other editors may book it. Thus one application is not mapped to multiple users/editors at the same time.
Now my requirement is to find all the applications booked by the editor who is currently logged in. I am unable to form the correct relations.
My tables are:
user (represent the editors)
Id, name, email, role_id, password
author_requests (represents the applications submitted by interested editors)
name, password, email, phone, city, country, profile_pic, bio, display_name, sample_article_link1, sample_article_link2, sample_article_link3, status
request_bookings (represent the bookings made by the editors)
id, item_id, item_type, user_id, status, status_date
In the request_bookings table, item_id contains the application id and item_type contains the model type that is “auhor”. These fields have been kept because like booking the author applications, moving ahead the same needs to be done with the suggested post titles and post articles.
My models are:
class User extends Authenticatable
public function bookings(){
return $this->hasMany("App\RequestBooking");
}
}
class AuthorRequest extends Model{
public function bookings(){
return $this->hasMany("App\RequestBooking");
}
}
class RequestBooking extends Model{
public function user(){
return $this->belongsTo("App\User");
}
public function authorReq(){
return $this->belongsTo("App\AuthorRequest");
}
}
When I try: auth()->user()->bookings->authorReq
It gives me error: Exception Property [authorReq] does not exist on this collection instance.
How can I get a list of all author applications that have been booked by the logged in editor?
Upvotes: 1
Views: 52
Reputation: 191
By default, laravel uses the column with method name suffixed with _id
, which according to your method name, looks for authorReq_id
.So, you either create a column on author_requests
table, if you dont have any column that defines the relation. Or, if you already have a column but with different name then you can pass a second parameter to belongsTo
method, Like so:
public function authorReq()
{
return $this->belongsTo("App\AuthorRequest", 'column_that_references_foreign_key');
}
Hope it helps.
Upvotes: 1