Laravel/Eloquent: Issues with date mutation and form model binding - laravel

I have a column with datatype of date which is date mutated. I date mutated it so that Laravel will convert it to a Carbon instance and I can use it easily at other places, where I need to convert it into Carbon instance. I am using Model binding in the edit form. As the field is date mutated, on the edit form it appears as "2015-07-29 00:00:00". I need it to be in this format instead: "2015-07-29".
I can't use accessor, as I need it as a Carbon instance at many other places.
I can't explicitly pass value after converting, as I am using the input inside a form partial and I also use it for create.
My workaround is the following:
I am sending a flag while including the view partial in the edit page and using it a condition to have two different code for create and edit.
#if (isset($edit))
{!! Form::text('eta', $order->eta->format('Y-m-d'), ['class' => 'form-control', 'required']) !!}
#else
{!! Form::text('eta', null, ['class' => 'form-control', 'required']) !!}
#endif
Is there a better way?

I think you can avoid from #if and $edit as,
{!! Form::text('eta', ( $order->eta ? $order->eta->format('Y-m-d') : null ) , ['class' => 'form-control', 'required']) !!}

Related

Default value for laravel collective select

I have a Laravel collective select as follows:
{!! Form::select('phone', $numbers, $title, ['class' => 'phone-select2 col4', 'id' => 'phone']) !!}
I want to pass default value for dropdown as separate variable from $numbers. So, have passed it as $title but it is didn't work due to it is not in the $numbers. So, how can I pass default value for it?
What you're looking for is a placeholder.
https://laravelcollective.com/docs/6.x/html#drop-down-lists
According to the documentation, you can set the default value to null and provide placeholder attribute as part of the fourth argument:
['class' => 'phone-select2 col4', 'id' => 'phone', 'placeholder' => 'Pick an option']

Laravel update form date and time

I am trying to make a form to update deadline where I need to edit the date and time. Also I want the form to have a calendar. I want to add the default value with calendar option
I am having the deadline as this format n database 2019-04-25 00:00:00
Here is the code I made to add the calendar:
View with the calendar
{{ Form::label('Abstracts Submission Deadline', 'Abstracts Deadline') }}
{{ Form::input('dateTime-local','Abstracts', $rss->Abstract_Submission_Deadline, ['class' => 'form-control', 'placeholder' =>$rss->Abstract_Submission_Deadline ]) }}
View without the calendar
{{ Form::label('Ranking Deadline', 'Ranking Deadline') }}
{{ Form::text('Ranking', $rss->Ranking_Deadline, ['class' => 'form-control', 'placeholder' => $rss->Ranking_Deadline ] ) }} `
The first box I have the calendar without the default value, the second one I have the default value without the calendar
Use value attribute instead of placeholder.

Drop-Down Lists from laravelcollective?

I use dropdownlist from laravelcollective. I am wondering how I can use (Selected Default) with this if I fetch data from a database. Here follows my source code:
{!! Form::select('port',$ports,null,['class'=>'form-control']) !!}
The selected default that I want is for example (Select Port).
First of all create a list in your controller and use as a first element the "Please select a port" text with empty value:
$ports =['' => 'Please Select a port'] + Port::lists('shortName','id')->toArray();
After you pass it to the view use it like this:
{!! Form::select('port_id',$ports,null,['class' => 'form-control']) !!}
The correct is to use attribute placeholder in dropdown.
{!! Form::select('port', $ports, null, ['placeholder' => 'Select a Port', 'class'=>'form-control']) !!}

How to populate combo box dynamically with Laravel (From Database)

sorry guys if this is nothing to you all but I seem not to get it I have a drop down and would love to populate it from the database I am using laravel 5.2 and looked at almost all questions asked on this topic and most of it is on laravel 4. I tried populating my combo from database but kept on getting the same error "ErrorException in 0fe8e1e2379436fb1f6f8c15a481341a7cff00e0.php line 22:
Undefined variable: callsign " here is how I have done it form the controller l inserted this code:
$callsign = \DB::table('drivers')->lists('Code');
return view('spotCheck.create')->with('drivers', $callsign);
from my view here is the code:
{!! Form::label('Code','Select a Driver') !!}
{!! Form::select('Code', $callsign, null, ['class' => 'form-control']) !!}
the question is what I am i not doing write and whats up with the error how does my controller know where to populate drop down
for the controller I had to do this
$drive = DB::table('drivers')->lists('Code');
return view('mypage',compact('drive'));
and on the view did this
{!! Form::label ('Call Sign:',null, ['class'=>"control-label"])!!}
{!! Form::select('call_sign', $drive, null, ['class' => 'form-control' , 'id' => 'sel']) !!}

selected value tag of Form::model doesn't bind property from Model

I'm learning Laravel and have an issue when trying to bind a property of the Model to selected values of the select tag. I tried to leave the 3rd parameter null because I believe Form Model Binding will automatically take care of it but it doesn't work. Here is what I already tried:
{{$article->tag_list}} // show [1,3]
//it doesn't work
{!! Form::select('tag_list[]', $tags, null , ['class' => 'form-control', 'multiple'] ) !!}
-------------
//it doesn't work as well
{!! Form::select('tag_list[]', $tags, $article->tag_list , ['class' => 'form-control', 'multiple'] ) !!}
-----------
//it works
{!! Form::select('tag_list[]', $tags, [1,3] , ['class' => 'form-control', 'multiple'] ) !!}
In the model I have the getTagListAttribute() which works fine.
public function getTagListAttribute(){
return $this->tags->lists('id');
}
With Text Input, the form works fine. Btw, I'm using 5.2.1 version. What am I missing here ?
I found the missing piece. the select function expects an array but getTagListAttribute() return an Collection object.
public function getTagListAttribute(){
return $this->tags->lists('id')->all();
}
or I can do this
public function getTagListAttribute(){
return $this->tags->lists('id')->toArray();
}

Resources