Reputation: 597
I have seen some of the question here but none of them have solved my problem. I am confused. I have a many to many relation between community and idea model. it is saving values in pivot table perfectly but I am unable to fetch data to view.
Idea Model:
public function communities() {
return $this->belongsToMany('App\Community', "community_idea");
}
Community model:
public function ideas() {
return $this->belongsToMany('App\Idea', "community_idea");
}
Store Post Function:
//function for creating Idea
public function storePost(IdeaRequest $request)
{
if ($request->hasFile('idea_image')) {
$filename = $request->file('idea_image')->getClientOriginalName();
$moveImage = $request->file('idea_image')->move('images/', $filename);
}
$idea = new Idea();
$idea->idea_title = $request->input('idea_title');
$idea->user_id = Auth::id();
$idea->idea_image = $moveImage;
$idea->idea_info = $request->input('idea_info');
$idea->idea_location = $request->input('idea_location');
$idea->idea_goal = $request->input('idea_goal');
$idea->idea_description = strip_tags($request->input('idea_description'));
$idea->save();
$idea->communities()->attach($request->input('selection'));
Session::set('idea_id',$idea->id);
session()->flash('flash_message', 'Your idea has been submitted for Review, Kindly Fill this Work BreakDown of You Work');
return redirect()->route('create_wbs');
}
VIEW: When I try to use this, it throws:
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$communities` error
@foreach($ideas->communities as $idea)
@if($idea->status == 1)
<div class="grid_3">
<div class="project-short sml-thumb">
<div class="top-project-info">
<div class="content-info-short clearfix">
<a href="{{URL::to('showidea', array($idea->id))}}" class="thumb-img">
<img src="{{asset($idea->idea_image)}}" alt="{{$idea->idea_title}}">
</a>
<div class="wrap-short-detail">
<h3 class="rs acticle-title"><a class="be-fc-orange" href="{{URL::to('showidea', array($idea->id))}}"><q>{{$idea->idea_title}}</q></a></h3>
<p class="rs tiny-desc">by <a href="#" class="fw-b fc-gray be-fc-orange">{{$idea->User->name}}</a></p>
<p class="rs title-description">{{$idea->idea_info}}</p>
<p class="rs project-location">
<i class="icon iLocation"></i>
{{$idea->idea_location}}
</p>
</div>
</div>
</div>
</div>
</div><!--end: .grid_3 > .project-short-->
@endif
@endforeach
Edit:
public function showCommunities($id)
{
$community = Community::findOrFail($id);
$communities = Community::all();
$ideas = Idea::paginate(15);
// $ideas->communities()->ideas;
return view('publicPages.ideas_in_community', compact(['ideas', 'community', 'communities']));
}
EDIT 2:
When i var_dump
on ideass
it displays following data.
When i var_dump
on $idea
it displays following data
View Code
@foreach($ideas as $idea)
@foreach($idea->communities as $ideass)
@if($ideass->status == 1)
<div class="grid_3">
<div class="project-short sml-thumb">
<div class="top-project-info">
<div class="content-info-short clearfix">
<a href="{{URL::to('showidea', array($idea->id))}}" class="thumb-img">
<img src="{{asset($ideass->idea_image)}}" alt="{{$ideass->idea_title}}">
</a>
<div class="wrap-short-detail">
<h3 class="rs acticle-title"><a class="be-fc-orange" href="{{URL::to('showidea', array($idea->id))}}"><q>{{$ideass->idea_title}}</q></a></h3>
<p class="rs tiny-desc">by <a href="#" class="fw-b fc-gray be-fc-orange">{{$ideass->User->name}}</a></p>
<p class="rs title-description">{{$ideass->idea_info}}</p>
<p class="rs project-location">
<i class="icon iLocation"></i>
{{$ideass->idea_location}}
</p>
</div>
</div>
</div>
</div>
</div><!--end: .grid_3 > .project-short-->
@endif
@endforeach
@endforeach
Upvotes: 0
Views: 273
Reputation: 9749
When you do $ideas = Idea::paginate(15);
you get a paginator with many models, like a collection, so you can't do $ideas->communities
. You have to loop all the Ideas and then to get the Communities of each one.
$ideas = Idea::with('communities')->paginate(15);
foreach ( $ideas as $idea ) {
foreach ( $idea->communities as $community ) {
//DO STUFF
}
}
EDIT
$ideas = Idea::paginate(15);
contains a collection with 15 records from the DB and each record has a belongsToMany relation with App\Community, which you have defined in the model. When you try $ideas->communities
it doesn't work because $ideas
is a collection, 15 records, and communities
is the relation to a single Idea model, the collection doesn't know what communities
is. So when you loop foreach ( $ideas as $idea ) {
the variable $idea
is a single Idea model and it has access to the belongsToMany relation with App\Community, so you can say $idea->communities
.
Upvotes: 0