Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

forteirp's avatar

Form select box {!!Form::select()!!} Laravel 5.8.9

I having a few issues with my values not matching up and my dropdown is also giving me the values as an array...

Here's what I have in my PostsController...

public function edit(Post $post)
{
    $categories = Category::pluck('title', 'id');

    return view('posts.edit')->withPost($post)->withCategories($categories);

and here's my edit.blade...

{{ Form::label('category_id', 'Category :')}}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}

I have tried the few different ways suggested on other posts, but I'm still having these issues, "need little help"?

Here's the value issue I was talking about: enter image description here- https://i.stack.imgur.com/Ew4j2.jpg

Here's the array issue I was talking about: enter image description here- https://i.stack.imgur.com/RM5kx.jpg

0 likes
13 replies
Nakov's avatar

You don't need to convert the pluck to an array, so the collection is just fine for the select, try this instead:

$categories = Category::all()->pluck('title', 'id');

After your edit, seems like the categories are fetched from another place. Do you share any global variable with your views?

You can try to debug this like this:

  • In your view add this instead:
{!! Form::select('category_id', [ 1 => 'Testing' ], null, ['class' => 'form-control']) !!}

Make sure that this is what appears.

  • Or in your controller share the variables like this:
return view('posts.edit', compact('post', 'categories'));
Nakov's avatar

@FORTEIRP - Yes, I gave you that for debugging purposes, so it means that the element works, but there is something wrong with your data. Try the second step, passing to the view as I gave it to you, or try to debug the array that you get from the categories, in your controller test like this:

dd($categories);

Make sure that the collection is okay, meaning you get id => title array from your categories.

forteirp's avatar

You say "Try the second step, passing to the view as I gave it to you" do you mean "{!! Form::select('category_id', [ 1 => 'Testing' ], null, ['class' => 'form-control']) !!}"?

If so I'm currently doing and Here's what I have... https://i.stack.imgur.com/L41l1.jpg

I'm very new at Laravel, so I'm not sure how to "debug the array that you get from the categories"?

right now this is what I have in PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Post;
use App\Category;
use Session;


class PostsController extends Controller
{
        public function __construct(){
         $this->middleware('auth')->except(['category', 'show']);
        }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
     
    protected $limit = 3;
     
    public function index()
    {
        
        $posts = Post::with('author')
                    ->latestFirst()
                    ->published()
                    ->paginate($this->limit);
                    
        return view('posts.index', compact('posts'));
    }
public function category(Category $category)
    {
        $categoryName = $category->title;

        $posts = $category->posts()
                           ->with('author')
                           ->latestFirst()
                           ->published()
                            ->paginate($this->limit);
                    
        return view('posts.index', compact('posts', 'categoryName'));
    }
    

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = Category::all();
        
        return view('posts.create')->withCategories($categories);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, array(
            'title'         => 'required|max:255',
            'slug'          => 'required|alpha_dash|min:5|max:255|unique:posts,slug',
            'category_id'   => 'required|integer',
            'body'          => 'required'
            
        ));
        //create Post
        $post = new Post;
        
        $post->title = $request->title;
        $post->slug = $request->slug;
        $post->category_id = $request->category_id;
        $post->body = $request->body;
        $post->author_id = auth()->user()->id;
        
        $post->save();
        
        return redirect('/posts')->with('success', 'Your post created created successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Post $post)
    {   
    
    return view('posts.show', compact('post'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Post $post)
{
    
    //$post = Post::find($id);
    // Check for correct user
        /*if(auth()->user()->id !==$post->author_id){
            return redirect('/posts')->with('error', 'Unauthorized Page');
            }
            
    $post = Post::where('id', $post)
            ->orWhere('slug', $post)
            ->firstOrFail();*/
            
    
    $categories = Category::all()->pluck('title', 'id');
            
    return view('posts.edit', compact('post', 'categories'));
    
}

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        /*$updated = Category::findorFail($id);
        $categories = $request->all();
        $category_id = $request->get('category_id');
        $updated->fill($categories)->save();
        
        return redirect('/dashboard')->with('success', 'Your post created updated successfully');*/
        
        $this->validate($request, [
            'title' => 'required|max:255',
            'slug' => 'required|alpha_dash|min:5|max:255',
            'body' => 'required'
            
        ]);
        //create Post
        $post = Post::find($id);
        $post->title        = $request->input('title');
        $post->slug         = $request->input('slug');
        $post->category_id  = $request->input('category_id');
        $post->body         = $request->input('body');
        $post->author_id = auth()->user()->id;
        $post->save();
        
        return redirect('/dashboard')->with('success', 'Your post created updated successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Post $post)
    {
         $posts = Post::with('author');
    
    // Check for correct user
        if(auth()->user()->id !==$post->author_id){
            return redirect('/posts')->with('error', 'Unauthorized Page');
            }
        
        $post->delete();
        
        return redirect('/dashboard')->with('success', 'Your post has been "Delete" successfully');
    }
}

Were do I use the "dd($categories);" that you give me?

Nakov's avatar

So try replacing your edit method with this:

public function edit(Post $post)
{
            
    $categories = Category::all()->pluck('title', 'id');
    dd($categories);
            
    // I was talking about this line as a second step for debugging. I see you are using it now.
    return view('posts.edit', compact('post', 'categories')); 
    
}

This will display the array on your screen, and make sure it is the correct one.

Nakov's avatar
Nakov
Best Answer
Level 73

@FORTEIRP - Then I don't see why it shouldn't work. Last try is to use different variable in case you have something overriding the categories to something else. So in your method, try this:

$cats = Category::all()->pluck('title', 'id');

return view('posts.edit', compact('post', 'cats'));

And in your view:

{!! Form::select('category_id', $cats, null, ['class' => 'form-control']) !!}
forteirp's avatar

Great.... that worked! thanks for working with me, you're a real lifesaver...

do you think it could have been the "public function category" in the index function?

public function category(Category $category)
    {
        $categoryName = $category->title;

        $posts = $category->posts()
                           ->with('author')
                           ->latestFirst()
                           ->published()
                            ->paginate($this->limit);
                    
        return view('posts.index', compact('posts', 'categoryName'));
    }

What do you think?

Anyway, things are working now and I "thank you again so very much for all your time and all your help!

Nakov's avatar

@FORTEIRP - Great that it worked. I don't think that that's the problem, but who knows. I don't have the codebase to test.

forteirp's avatar

I wanted to know if you could look into this new issue that I'm now having. Could you tell me "Why is the ID variable now undefined" in my update function?

This was working before we fixed the other issue...but now not so much...

Here's my update function:

public function update(Request $request)
    {
        /*$updated = Category::findorFail($id);
        $categories = $request->all();
        $category_id = $request->get('category_id');
        $updated->fill($categories)->save();
        
        return redirect('/dashboard')->with('success', 'Your post created updated successfully');*/
        
        $this->validate($request, [
            'title' => 'required|max:255',
            'slug' => 'required|alpha_dash|min:5|max:255',
            'body' => 'required'
            
        ]);
        //create Post
        $post = Post::find($id);
        $post->title        = $request->input('title');
        $post->slug         = $request->input('slug');
        $post->category_id  = $request->input('category_id');
        $post->body         = $request->input('body');
        $post->author_id = auth()->user()->id;
        $post->save();
        
        return redirect('/dashboard')->with('success', 'Your post created updated successfully');

And this is what I get when I try to update the table:

https://i.stack.imgur.com/MaqE1.jpg

And then when I tried to add $post = post::find($id); and replaced the function edit(Post $post) to ($id) :

public function edit($id)
{
    $post = Post::find($id);
    $cats = Category::all()->pluck('title', 'id');
    
            
    // I was talking about this line as a second step for debugging. I see you are using it now.
    return view('posts.edit', compact('post', 'cats')); 
    
}

but I am now getting this e_error and this is what I get when I try to edit: https://i.stack.imgur.com/pFZp1.jpg

Please let me know if I need to open up a new discuss... Thx

Nakov's avatar

@FORTEIRP - The update method does not work as you don't have the $id initialized and you try to use it. You might have had

public function update($id)

// and changed it to this:

public function update(Request $request)

If in your routes you use:

Route::resource('posts', 'PostsController');

// or this

Route::patch('/posts/{post}', 'PostsController@update');

Then you can use the post as the second parameter, like this:

public function update(Request $request, Post $post)

and remove the Post::find($id); line. You can do the same for the edit function.

I answered it here now, but for the next time, make sure to open another thread as it is a new question.

forteirp's avatar

As I said before, your a life saver and I thank you again..

Please or to participate in this conversation.