I am trying to pass only the posts created by the logged in user into a laravelcollective/html select drop down menu within a form.
In my code I have two examples. Using the variable example shows how I can get the dropdown select menu to show all results from the posts table. Using the variable posts in a foreach loop shows how I can return just the posts created by the logged user, but not in a select menu.
I need to have the dropdown menu function as in the form using example but displaying results of the foreach posts loop.
Controller
public function createPost()
{
$example = Post::pluck('title', 'id')->all();
$posts = Posts::all();
return view('post.create', compact('posts', 'example'));
}
Example View
<div class="form-group">
{!! Form::label('example', 'Posts:') !!}
{!! Form::select('example', ['' => 'Select'] + $example, null) !!}
</div>
Foreach Loop Posts View
@foreach($posts as $post)
@if(Auth::user()->id == $post->user_id)
{{ $post->title }} <br>
@endif
@endforeach
try $posts = Posts::where('user_id',\Auth::id())->get()->pluck('title','');
. It will return only posts of logged in user.
{{ Form::select('example', $posts) }}
You are using select box wrong.
@foreach($posts as $post)
@if(Auth::user()->id == $post->user_id)
{{ $post->title }} <br>
@endif
@endforeach