I am trying to get this problem fixed for hours now and I just don't get what I am doing wrong.
I want to get all entries of my tables where my id of posts equals the id of tables bewerbungens.
So I get all applications of every user existing with the corresponding title of the post the user applied to.
I asked a question here before and got resources to add relationships, which I did, as seen here:
Bewerbungen Model:
protected $fillable = [
'bewerber_email',
'Stellenanzeigen_ID',
'is_Accepted',
'accept_Date',
'is_Canceled',
'cancel_Date',
'is_Received',
'receive_Date',
'is_Canceled_Bewerber',
'cancel_Date_Bewerber',
'lebenslauf_File',
'zeugnis_File',
'anschreiben_File',
'weitere_Doks',
];
public function post() {
return $this->belongsTo('Post', 'Stellenanzeigen_ID', 'Bewerbung_ID');
}
and here in my Post model:
protected $fillable = [
'titel',
'startdate',
'enddate',
'beschreibung',
'standort',
'type_name',
'abteilung_name',
'kontakt',
'isActive',
'lebenslauf',
'zeugnisse',
'anschreiben',
'weitere_Doks',
'is_Permitted',
'job_start',
];
public function bewerbungen() {
return $this->belongsToMany(Bewerbungen::class);
}
Here is my Controller code where I try to get the entries:
$dummy = Post::with('Bewerbungen.post')->get();
But I get the error: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'portal.bewerbungen_post' doesn't exist (SQL: select bewerbungens
.*, bewerbungen_post
. post_id
as pivot_post_id
, bewerbungen_post
. bewerbungen_id
as pivot_bewerbungen_id
from bewerbungens
inner join bewerbungen_post
on bewerbungens
. id
= bewerbungen_post
. bewerbungen_id
where bewerbungen_post
. post_id
in (1, 2, 3, 4))
I also tried this instead:
$dummy = Post::where('id', 'Stellenanzeigen_ID')->get();
But it just returns an empty set.
How can I fix this?
The SQL Error said that there's no bewerbungen_post
table, since you use belongsToMany
with only one argument. As mentioned here , if your relationship's intermediate table name is different with the Laravel Behavior, then add the relationship's intermediate table name to the second argument, or Laravel will join the two table for the relation table's name. For example:
public function bewerbungen() {
return $this->belongsToMany(Bewerbungen::class, 'bewerbungen_post');
}
I've checked your previous question, and I think that the Post
model joined directly to the Bewerbungen
model, so the relation in the Post
Model should looks like this:
public function bewerbungens(): HasMany
{
// It's mean that `Post` has many `Bewerbungen` where `Stellenanzeigen_ID` = `posts.id`
return $this->hasMany(Bewerbungen::class, 'Stellenanzeigen_ID', 'id');
}
And in the Bewerbungen model:
public function post(): BelongsTo
{
// It's mean that `Bewerbungen` owned by Post as `Stellenanzeigen_ID` = `posts.id`
return $this->belongsTo(Post::class, 'Stellenanzeigen_ID', 'id');
}
If you want to get Post
with all Bewerbungen
owned by Posts
:
// You will get All Post with all Bewerbungen each Post
$posts = Post::with('bewerbungens')->get();
Vice versa:
// You will All Bewerbungen and One Post for every Bewerbungen
$bewerbungen = Bewerbungen::with('post')->get();
All details about relationship could be found in Laravel Documentation .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.