Using Laravel Form class to add the 'disabled' attribute - laravel

Using Laravel 4's Form class, we can create a list using
{{ #Form::select('colors', Colors::all()), $color }}
Question: How can we add the attribute disabled using Blade without having to rewrite the clean Blade syntax into the usual ugly form?

Just add array('disabled') in the end like:
{{ Form::select('colors', Colors::all(), $color, array('disabled')) }}

This should do the work.
{{ #Form::select('colors', Colors::all()), array(
'disabled' => 'disabled',
'class' => 'myclass'
) }}

Though already answered, IMO both answers weren't neutral enough, so to avoid duplicates the arguments are
#Form::select('name', $optionsArray, $selectedOption, ['disabled']).
So if you're prepopulating form with #Form::model() you should do #Form::select('name', $optionsArray, null, ['disabled']) - array with 'disabled' has to be 4th parameter.

Related

Laravel Collective date format

Is there a way to set Laravel Collective date picker format to yyyy-mm-dd?
I am using now:
{{ Form::date('deadline', null,['class' => 'form-control']) }}
but in the front end I get an input field with mm/dd/yyyy inside. I tried parsing the Carbon instance as second parameter, but that does nothing.
{{ Form::date('deadline', \Carbon\Carbon::now()->format('Y-m-d'),['class' => 'form-control']) }}
You could pass an DateTime instance for the second parameter. As per the source code if the second parameter is an DateTime instance it would return the formatted date (Y-m-d).
So you could try this,
{{ Form::date('deadline', new \DateTime(), ['class' => 'form-control']) }}
Note:
As #Ajahi himalil stated in the comment, the value in the request would be different from the one selected. Please verify the data on form submission.
No, Laravel Collective does not provide this option.
Use https://eonasdan.github.io/bootstrap-datetimepicker/
{{ Form::text('deadline', null, ['class' => 'form-control', 'id'=>'datetimepicker']) }}
Javascript
$('#datetimepicker').datetimepicker({
format: 'yyyy-mm-dd'
});
After applying all these methods and nothing working. simple input tag with date type is the only solution that i came to know instead of collective laravel.

Laravel collective select placeholder not working when multiple is to true {{ Form::select() }}

I have a problem with Laravel collective select placeholder not working
{{ Form::select('album',$albums,$selected, ['class'=>'form-control','placeholder'=>'select album' ]) }}
But when i allow tags to true like this using select 2, it works fine,
{{ Form::select('tags[]',$tags,$tagged, ['data-input'=>'select2-tags','multiple'=>true]) }}
The second one works fine, I don't want the album to have multiple input attribute, What am i doing wrong?
You can do this with select, but you can add placeholder as the value with 0 index:
$albums[0] = 'select album';
Alternatively, you can create your own select function using Laravel Collective Form macros.
You can follow this :
{{ Form::select('album', $album , Input::old('album'), array('placeholder' => 'Please select','class'=>'form-control')) }}

Using HTML Placeholder in Laravel 4

{{ Form::text('username', Input::old('username')) }}
This is my code, I want to add Username as placeholder in the text box. I already use bootstrap in the project.
I tried to use the example
{{ Form::text('username', 'Username', Input::old('username')) }}
But it gives a default value which needs to be deleted and then the user has to type his value. I want something like how it shows up on Twitter.com etc. Which gets erased once user types into it.
{{ Form::text('username', Input::old('username'), array('placeholder'=>'Username')) }}
This works for Laravel 4!!
{{ Form::text('username', null, array('placeholder'=>'Username' )); }}
Use:
{{ Form::text('txtUsername', '',array('class'=>'input', 'placeholder'=>'Username')) }}
Been a while since this was updated but with Former for Laravel the way to do this would be:
Former::text('field')->placeholder('langfile.path.to.placeholder.text')))
The lang file would be called langfile.php with the array containing:
<?php
return array (
'path.to.placeholder.text' => 'Some Placeholder Text.'
);
In case somebody wonders how this should work for a password field:
{{ Form::password('password', array('placeholder'=>'Password')) }}

Laravel Pagination links not including other GET parameters

I am using Eloquent together with Laravel 4's Pagination class.
Problem: When there are some GET parameters in the URL, eg: http://site.example/users?gender=female&body=hot, the pagination links produced only contain the page parameter and nothing else.
Blade Template
{{ $users->link() }}
There's a ->append() function for this, but when we don't know how many of the GET parameters are there, how can we use append() to include the other GET parameters in the paginated links without a whole chunk of if code messing up our blade template?
I think you should use this code in Laravel version 5+.
Also this will work not only with parameter page but also with any other parameter(s):
$users->appends(request()->input())->links();
Personally, I try to avoid using Facades as much as I can. Using global helper functions is less code and much elegant.
UPDATE:
Do not use Input Facade as it is deprecated in Laravel v6+
EDIT: Connor's comment with Mehdi's answer are required to make this work. Thanks to both for their clarifications.
->appends() can accept an array as a parameter, you could pass Input::except('page'), that should do the trick.
Example:
return view('manage/users', [
'users' => $users->appends(Input::except('page'))
]);
You could use
->appends(request()->query())
Example in the Controller:
$users = User::search()->order()->with('type:id,name')
->paginate(30)
->appends(request()->query());
return view('users.index', compact('users'));
Example in the View:
{{ $users->appends(request()->query())->links() }}
Be aware of the Input::all() , it will Include the previous ?page= values again and again in each page you open !
for example if you are in ?page=1 and you open the next page, it will open ?page=1&page=2 So the last value page takes will be the page you see ! not the page you want to see
Solution : use Input::except(array('page'))
Laravel 7.x and above has added new method to paginator:
->withQueryString()
So you can use it like:
{{ $users->withQueryString()->links() }}
For laravel below 7.x use:
{{ $users->appends(request()->query())->links() }}
Not append() but appends()
So, right answer is:
{!! $records->appends(Input::except('page'))->links() !!}
LARAVEL 5
The view must contain something like:
{!! $myItems->appends(Input::except('page'))->render() !!}
Use this construction, to keep all input params but page
{!! $myItems->appends(Request::capture()->except('page'))->render() !!}
Why?
1) you strip down everything that added to request like that
$request->request->add(['variable' => 123]);
2) you don't need $request as input parameter for the function
3) you are excluding "page"
PS) and it works for Laravel 5.1
In Your controller after pagination add withQueryString() like below
$post = Post::paginate(10)->withQueryString();
Include This In Your View
Page
$users->appends(Input::except('page'))
for who one in laravel 5 or greater
in blade:
{{ $table->appends(['id' => $something ])->links() }}
you can get the passed item with
$passed_item=$request->id;
test it with
dd($passed_item);
you must get $something value
In Laravel 7.x you can use it like this:
{{ $results->withQueryString()->links() }}
Pass the page number for pagination as well. Some thing like this
$currentPg = Input::get('page') ? Input::get('page') : '1';
$boards = Cache::remember('boards' . $currentPg, 60, function() {
return WhatEverModel::paginate(15);
});
Many solution here mention using Input...
Input has been removed in Laravel 6, 7, 8
Use Request instead.
Here's the blade statement that worked in my Laravel 8 project:
{{$data->appends(Request::except('page'))->links()}}
Where $data is the PHP object containing the paginated data.
Thanks to Alexandre Danault who pointed this out in this comment.

Laravel 4 Javascript Link using {{Html}}

Is there any Laravel4 Html() function or way to add a disabled link. Of course I could create the <a> tag directly, though I'd prefer to be consistent.
ie:
{{ Html::link('javascript:;','Delete',array('id'=>"deletebt")) }}
You must use link_to, for example :
link_to('link', 'title', array('id' => 'MyId'));

Resources