1

Part of my PostController.php

    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use Session;
    use Carbon\Carbon;

    use App\Category;
    use App\Post;
    use App\Tag;
    use App\User;


    class PostController extends Controller
    {
    public function create()
      {
        $categories = Category::pluck('name','id');
        $tags = Tag::pluck('name','id');

        return view('posts.create')->withCategories($categories)->withTags($tags); 
      }

Part of my posts/create.blade.php

 <div class="input-field col m6 s12">
   {!! Form::select('category', $categories, null, ['class' => 'validate', 'placeholder' => 'test']) !!}
   {{ Form::label('category', 'Category: ') }}
 </div> 

I'm using Laravel Collective to create my forms with Blade Syntax, and I'm using Materialize CSS.

It all works and submits, but at the minute it has the first item in the $categories array automatically selected, and I'd like this to not be the case.

I'm trying to have a disabled placeholder that's selected and says "Select your category".

I'm not sure if this is possible?

I know I don't have to use the blade syntax, and can just create a normal HTML form, but this project is entirely for learning purposes, as I'm trying to learn Laravel.

n8udd
  • 657
  • 1
  • 9
  • 30

3 Answers3

1

It's possible by creating the placeholder yourself:

{!! Form::select('category', ['' => 'Select your category'] + $categories->all(), null,
    ['class' => 'validate'], ['' => ['disabled']]) !!}
Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109
0

Add

"class" => "first-disabled "

to your select and then \

Use this code in the scripts and it will disable it: \

$( "select.first-disabled option:first-child" ).attr("disabled", "disabled");
cursorrux
  • 1,382
  • 4
  • 9
  • 20
0

You can try: prepend

return view('posts.create')->withCategories($categories->prepend('Select category', ''))->withTags($tags);

Hope it help you !!!