Are you getting values in $activities in create page trying dd() or dumping it.
Trying to get property of non-object - Laravel Form
I am creating a form with a many-many relationship. I have a posts table and an activities table. There is a many to many link using pivot table. I am creating a form to add one or more activities to the posts. I am receiving an 'ErrorException' - Trying to get property of non-object. I cannot understand why this is not working.
I would be grateful for any assistance you can offer me.
My relevant code is below.
//Posts/create.blade.php
{!!Form::open(['action' => 'PostController@store','method' => 'POST', 'class'=>'form-group'])!!}
{{Form::bsText('title','',['placeholder' => 'Trip Name'])}}
{{Form::bsTextarea('body','',['placeholder' => 'Trip Description'])}}
{{Form::bsSubmit('submit')}}
{{Form::label('activities', 'Activity:') }}
<select class="form-control select2-multi" name="activities" multiple="multiple">
@foreach($activities as $activity)
<option value="{{ $activity->id }}">{{ $activity->activity_name}}
</option>
@endforeach
</select>
{!! Form::close() !!}
// PostsController
public function create()
{
$activities = Activity::all();
return view('posts.create')->withActivities($activities);
$posts = Post::all();
}
public function store(Request $request)
{
// Create a new post using the request data
// Save it to the database
$this->validate(request(), [
'title' => 'required',
'body' => 'required',
]);
$post = Post::create([
'title' =>request('title'),
'body' =>request('body'),
'user_id' => auth()->id(),
'activity_id' => id()
]);
// And then redirect to somewhere in application
return redirect()->route('posts.show', $post->id);
}
Collection {#267 ▼ #items: array:2 [▼ 0 => "Skiing" 1 => "Climbing" ] }
The problem is that $activities is coming through as a simple array with no keys, just values, thus {{ $activity->id } and {{ $activity->activity_name}}, which reference nested keys from an expected object does not exist in the $activities value.
Not sure why that's the case here as your controller seems correct and I don't see where it's converting to an array. Your first dd from inside the controller shows the correct object for Activity with id, activity_name, created_at and updated_at, yet your dd from the view shows a simple array.
In any case, here's a work around solution that's a bit more code but simple enough. Note that I coded this from memory, not in an editor, so there might be some minor syntax or other issues.
Try something like this in your PostController:
public function create()
{
$activities = Activity::all();
$activitiesArray = [];
foreach($activities as $activity) {
$activitiesArray[$activity->id] = $activity->activity_name;
}
return view('posts.create')->with('activitiesArray', $activitiesArray);
}
Then in posts/create.blade.php change the @foreach section to:
@foreach($activitiesArray as $key => $activity)
<option value="{{ $key }}">{{ $activity }}
</option>
@endforeach
Please or to participate in this conversation.