So, going into the problem straight away. someone told me that we dont need to make a pivot table
if we only want to have ids
of the table. laravel can itself handle this situation. I dont know how this works. I have a table community
and another table idea
. relation is like this;
One community can contain many ideas and an idea can be found in many communities.
Relation in idea Model:
public function community() {
return $this->belongsToMany('App\Community')->withTimestamps();
}
Relation in community Model:
public function idea() {
return $this->belongsToMany('App\idea');
}
Now i want to fetch all the records related to a single community to show on its page Let's say the community is Arts
.
Here is Controller function:
public function showCommunities($id) {
$community = Community::findOrFail($id)->community()->get();
return view('publicPages.ideas_in_community', compact('community'));
}
When i attach ->community()->get()
to the Community::findOrFail($id)
Then it throws the error
SQLSTATE[42S02]: Base table or view not found laravel
Any help would be appreciated.
Edit:
Logically, this piece of code Community::findOrFail($id)->community()->get()
should be like this Community::findOrFail($id)->idea()->get()
. Now it is true but it has little issue. it throws an error
Fatal error: Class 'App\idea' not found