n8udd
n8udd

Reputation: 721

Set Laravel Collective Select default input as disabled

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.

Upvotes: 1

Views: 1506

Answers (3)

Vo Chi Long
Vo Chi Long

Reputation: 11

You can try: prepend

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

Hope it help you !!!

Upvotes: 0

basem Elgamal
basem Elgamal

Reputation: 1

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");

Upvotes: 0

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25926

It's possible by creating the placeholder yourself:

{!! Form::select('category', ['' => 'Select your category'] + $categories->all(), null,
    ['class' => 'validate'], ['' => ['disabled']]) !!}

Upvotes: 1

Related Questions