Laravel Blade - add variable inside javascript confirmation - laravel

i want the javascript confirmation to include the variable name $button_label. Which means that it should have a confirmation of "Save record ?" or "Update record ?".
I'm having a hard time doing it.
Any thoughts?
#php $button_label = Route::current()->getName() == 'maintenance.state.edit' ? 'Update record' : 'Save record' ; #endphp
{!! Form::submit($button_label, ['class' => 'btn btn-primary btn-lg', 'onclick' => 'return confirm("Proceed ?")']) !!}

Try this one:
"return confirm('{$button_label} ?')"
Also read this page, if you want to learn why i changed the positions of single and double quotes.

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

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

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

form model binding and file upload

I got an answer previously to my question about how to create edit/update details form in laravel, and the answer was utilizing Form::model(modelName). Nevertheless, the page which is updating is the same page handling the insertion. I have the following form opening:
{{ Form::open(array('action' => 'SomeController#basic','files' => true)) }}
how can I change this to use the form::model instead? I also have an upload of file in same page (profile pic).
Laravel 4 or 5 Form <-> Model binding
Maybe is a "bit" late for the original poster, but for those who search for a solution, this can be used as a later reference.
The L4 solution is:
{{ Form::open($object, array('method' => 'PATCH', 'action' => array('SomeController#update', $object->id),'files' => true)) }}
For Laravel 5, you would use:
{!! Form::model($object, ['method' => 'PATCH', 'action' => ['ObjectsController#update', $object->id], 'files' => true]) !!}
Home this helps!
Form model binding can be done by declaring form open as
{{ Form::model($user, array('action' => 'SomeController#post', 'files'=> true)) }}
You pass the model to the view for example here $user is the model.
$user = User::find(1);
return View::make('editUser')->with(compact('user'));
For file upload, if the $user is isset you could display the image. Along with the upload button. When update is clicked you can check
if(Input::has('file'))
then move the file and upload the path in the database. May be there is a different solution for this.
Is this what you were looking for?

Resources