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

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']) !!}

Related

Pass user id through select box list laravel

I want to pass the selected user id to controller from select box.How can I achieve it?
{!! route('approve', ['id' => $user->id]) !!}
In select box,
{{ Form::select('Actions', [
'approve' => 'Approve',
'decline' => 'Decline']
) }}
My resulto to be like this,
First of all you'll need an input for selecting users. For example I got a list of users with their username and id here :
$users= \App\Users::pluck('username','id');
after sending it to the view, you can use an input for selecting user :
{!! Form::select('user_id', $users,'0', ['class' => 'form-control']) !!}
That's it. Now you can select the user and get data as $_POST['user_id'] or $_GET['user_id'] in the action. gl.

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']) !!}

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

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']) !!}

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();
}

Laravel changing function/text of a button depending on value

I'm using Laravel 5 and I have a button "Enable" that once pressed sets a flag in the DB to 1. This all works fine with my code below:
Blade view
{!! Form::open(['url' => '/cms/user/' . $user->id . '/flag', 'method' => 'PUT']) !!}
{!! Form::hidden('flagged', !$user->flagged) !!}
{!! Form::submit('Flag', ['class' => 'btn btn-success'])!!}
{!! Form::close() !!}
Controller
/**
* Update the specified resource in storage.
*
* #Middleware({"auth"})
*
* #param int $id
* #Put ("cms/user/{user}/flag",as="cms.users.update.flag")
* #return Response
*/
public function updateflag($id)
{
$user = User::find($id);
//dd(Input::get('flagged'));
$user->flagged = Input::get('flagged');
$user->save();
Session::flash('message', 'Successfully flagged idea!');
return redirect()->route('cms.users.index');
}
Now what I'm trying to work out is changing the button once the user flags it. I.e.: The first time the button should say "Enable" (DB value is 0). Then I press the button and it updates the flag in the DB to 1 (which works fine).
But now, after the button is pressed, how do I change the button text to "Disable" so I can then disable the user on pressing the button. Kind of like an on/off switch that I can enable/disable the user but updating the button text and function based on what the flagged value is in the DB.
Pass a boolean variable to the view (apparently you already have a variable that contains this information):
return redirect()->route('cms.users.index')->with(['flag' => $flag]);
Then in view make an if/else:
#if($flag)
{!! Form::submit('Enable', ['class' => 'btn btn-success', 'disabled' => 'disabled']) !!}
#else
{!! Form::submit('Enable', ['class' => 'btn btn-success']) !!}
#endif

Resources