Yii2 Button with Parameter - php

I have this button in my Yii2 project:
Html::a('label', ['/attributes/index'], ['class'=>'btn btn-primary']) ;
The button is located in the page:
/site/view
Now what I want to do is pass a parameter when this button gets clicked to the attributes/index page from the site/view page.
To be more specific it is the ID that I want to pass of a particular record from a DB I am viewing.
Cheers.

You can pass parameters as key => value pairs after the route:
Html::a('label', ['/attributes/index', 'id' => $id], ['class'=>'btn btn-primary']) ;
See the Yii2 docs: http://www.yiiframework.com/doc-2.0/yii-helpers-baseurl.html#toRoute()-detail

Related

Laravel get data from dropdown list and pass it to the controller

I am trying to have the user choose a month from a dropdown list displaying the months, and passes that month value (in numbers) to the controller
I am having trouble making the dropdown list an action so once the user picks the month, the controller gets called.
here is my index page:
<h>Display a logs by monthly <h>
{{ $id=Form::selectMonth('month')}}
<a href="{{action('LogController#monthly',['id' => $id]) }}" class="btn btn-primary">Monthly Logs
</a>
and when I add the Form method inside the tag i am getting an error says the variable is not defined.
here is the Controller.php
public function monthly($id)
{
$dcmlogs = log::with('users')
->whereMonth('created_at', '=', $id)
->paginate(15);
return view('dcmlog.index', compact('dcmlogs'));
}
You can use something like this :
{!! Form::open(['route' => ['logs', $id], 'method' => 'POST' ])!!}
{{ Form::select('in_out',[1 => 'Jan', 2 => 'Feb'] , null, ['class'=>'
form-control'])
}}
{!! Form::close() !! }}
hope this helps ..
I recommend creatintg a route
Route::get('/logs/{id}', 'LogController#monthly')->name('logs');
And use the route function in the template:
route('logs', ['id'->$id]);

Select default value if no old input is set

I am using form model binding and have the following select input :
{!! Form::select('user_id', $users, old('user_id') ?: Auth::id(), ['class' => 'form-control select2 users']) !!}
I would like to accomplish the following:
In the creation form: select the option where the user_id equals the authenticated user's ID, but if there was old input in the session, select that option instead.
In the edit form: always select the option that is stored in the model.
old('user_id') ?: Auth::id() doesn't seem to work when editing, because it always selects the option of the authenticated user and not the one that is stored in the model.
I believe you already have this line,
{!! Form::Model($users, ['route' => ['admin.user.update', $user->id], 'method' => 'PUT']) !!}
then your code should like this:
{!! Form::select('user_id', $users, isset($selectedUser) ? $selectedUser : null) !!}
at Create function do not pass $selectedUser to view but pass the id in your session,and in your edit function you should pass $selectedUser to view ,which is Auth::user()->id

Passing String between views - Laravel 5.1

I want to create a two buttons buyer and seller in the home page, and on click of these buttons, I want the guest to get directed register page with a ~hidden input~ placed in the form depending on the value of the button pressed.
I am using Laravel 5.1
For what I am trying to achieve, do I need to register a new route and a new function?
First, do I need to use a {!! link_to !!} or {!! Form submit !!}. I thought form submit would work:
{!! Form::open(array('url' => '/auth/register', 'profession' => 'seller')) !!}
{!! Form::submit('Seller', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!}
My routes are:
Route::get('auth/register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
auth/register and AuthController is the one that comes with Laravel default. I also didn't register a new public function. Is this the reason? Do I need to send the data from view to controller and then other view (view->controller->view). I am really confused on this one.
This is how it seems in the view-source:
<form method="POST" action="http://app.com/auth/register" accept-charset="UTF-8" profession="seller"><input name="_token" type="hidden" value="Cw4Het1A1M6020oQL45Cy2Q0ct46TSe6ba2g4r4C">
<input class="btn btn-warning" type="submit" value="Seller">
</form>
Edit:
I think I can't send variables between views. So do I need to register a completely new route and a controller, and two methods in it? I am really confused on this stage.
Check out my answer to your previous similar question for details on the implementation:
Laravel Passing Data From One View to Another View
To answer the questions you are posing
You can use two forms one for buyer and one for seller
{!! Form::open(['route' => ['type_path']]) !!}
{!! Form::hidden('type', 'buyer') !!}
{!! Form::submit('Buyer', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!
{!! Form::open(['route' => ['type_path']]) !!}
{!! Form::hidden('type', 'seller') !!}
{!! Form::submit('Seller', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!
on your landing page and then have both of this forms direct to a same named route (in this case 'type_path')
Then in your routes.php file you would reference only one controller and one method and handle the logic in that method. Again see the link to my answer to see a full implementation of this for the example of writer and reader.
Essentially as you say, you'd be sending as the hidden inputs from the view to the controller to the second view.
You could register a new route like:
Route::get('auth/{TYPE}/register', 'Auth\AuthController#getRegisterType')
Your home page would have something like this:
seller
Your controller method would be something like this:
public function getRegisterType($type)
{
Session::put('registerType', $type);
return redirect('/auth/register');
}
Then you can access you set session variable from the controller methods getRegister & postRegister
Have you two buttons direct to the register page with a query string parameter:
Buyer
Seller
And then just check for the query string parameter in your register.blade.php view:
{!! Form::hidden('account_type', Request::query('type')) !!}
If you need to validate one of the two is present, then you could create a middleware class you apply to the login route only:
public function handle($request, Closure $next)
{
if (! in_array($request->query('type'), ['buyer', 'seller'])) {
// Error
}
return $next($request);
}

Yii2:link to related update page from different model

I have two models namely daily_ward_entry and discharge_note both the models are related by discharge_note.regn_number = daily_ward_entry.ipd_patient_id. I want to create a link from daily_ward_entry to update page on discharge_note.
I can't make out how to create such link.
I have tried like this, but it doesn't redirect at all.
<?= Html::a('Go to Discharge NOte',
['discharge-note/update', $dischargenote->regn_number =>$model->ipd_patient_id],
['class' => 'btn btn-primary']) ?>
your mistake is not correct send parameter. parameter send with key-value pairs. see doc
<?= Html::a('Go to Discharge NOte',
['discharge-note/update', 'id' => $model->ipd_patient_id],
['class' => 'btn btn-primary']) ?>

Laravel/Blade Form PUT Method, dd(Input::all());

Hi I send a form in my contact.blade.php. I read in order to use the PUT method you have to create a hidden input field which contains the method.
#if($do == 'edit')
{{ Form::model($contact, array('method' => 'PUT', 'route' => array('contact.update', $contact->id), 'id' => $do=='edit' ? $do.$contact->id : $do.$contact_type_id, 'form_id' => $do=='edit' ? $do.$contact->id : $do.$contact_type_id)) }}
{{ Form::hidden('_method', 'PUT') }}
#endif
....
{{ Form::submit('speichern', array('class' => 'btn btn-primary')) }}
</div>
{{ Form::close() }}
The route:
Route::put('/contact/{id}', array(
'uses' => 'ContactController#update',
'as' => 'contact.update'
));
The Controller:
public function update($id)
{
dd(Input::all());
// //get user account data
// $user = User::find( Auth::id() );
// // validate input
// $v = Contact::dataValidation( Input::all() );
return Redirect::Route('user.edit', 1)->withSuccess("<em>Hans</em> wurde gespeichert.");
Q1:
As soon as I call dd(Input::all()); I don't get redirected any more, instead I see a json with my form values.
Q2:
I'm just debugging this so I didn't program it. So my second question is:
From my understanding dd(Input::all()); gets all my form data. So don't I need to store it anyways somewhere?
Q1: dd() terminates the script, hence why you are not getting redirected. It's used as a tool to essentially break and examine what is going on.
http://laravel.com/docs/4.2/helpers
Q2: You will still need a model to feed the Input::all data into. Input::all simply fetches the submitted data, it doesn't do anything with it. It ultimately depends on your use case, sometimes you may want to email the data, but obviously most times you would what to store it against your persistence layer (read database / datastore)
Question 1
when you use DD, it will show the data and stop at that line.
DD
Dump the given variable and end execution of the script.
more information you can read it here DD in DD session.
Question 2
I'am not sure about 2nd question but if you want to get value from all input you could us Input::all();
more information All input in Getting All Input For The Request session

Categories