Populate form without model in laravel - laravel

My application is made in laravel for a competition admin.
I have 'create' and 'edit' forms on Teams and Players. One team has multiple players.
I would like to link from the Team page to the 'create player' page. The Create Player page does not use a model (doesn't bind). How can I still prefill the select box with the team from the team page? Can I bind without saving a record in the database?
What should my routes be like?

Pass the team ID in the URL?
/players/create?team={teamId}
PlayersController#create method:
$teams = Team::all();
return view('players.create', compact('teams'));
players.create view:
<select name="team">
#foreach ($teams as $team) {
<option value="{{ $team->id }}"{{ $request->has('team') && $request->query('team') === $team->id ? ' selected' : '' }}>{{ $team->name }}</option>
#endforeach
</select>

You could make a route for example
// teamId is optional
Route::get('player/create/{teamId?}', ['as' => 'player_create', function ($teamId = null) {
// You can of course better do this logic in a controller!
// just an example :)
// check if $teamId is null here for example
// Or whatever logic you want to grab a team by
$team = Team::find($teamId);
$teams = Team::all();
// Again.. or whatever way you want to pass your data!
return view('player.create', ['teamName' => $team->name, 'teams' => $teams, 'whatever' => 'elseyouneed']);
}]);
And in the form of your view:
{!! Form::select('team', $teams, $teamName) !!}
Edit
since html isn't part of the core anymore, you can't use that out of the box so I suppose Chris' approach is better. You could however install a package for it.

Related

Laravel : how to get $payment_gateway value

I'm trying to create a feature where, when i create a new booking i can choose the payment method like via xendit or transfer. But when i tried to submit the output of the payment method is still offline payment because of this code {{$row->gatewayObj ? $row->gatewayObj->getDisplayName() : ''}} , and not xendit. How do i fix this??
The Controller :
public function create(Request $request){
// $this->checkPermission('news_create');
$allServices = get_bookable_services();
$whatsAppBookableServices = ["art", "food", "gear", "car", "hotel"];
$payment_gateway = ["xendit", "offline payment"];//tambahan Nicho
$row = new BookingOffline();
$row->fill([
'status' => 'publish',
]);
$data = [
// 'categories' => NewsCategory::get()->toTree(),
'row' => $row,
'breadcrumbs' => [
[
'name' => __('Report'),
'url' => 'admin/module/report/booking'
],
[
'name' => __('Add Booking By WA'),
'class' => 'active'
],
],
'bookableServices' => array_keys($allServices),
'whatsAppBookableServices' => $whatsAppBookableServices,
'payment_gateway' => $payment_gateway,//tambahan Nicho
];
return view('Report::admin.booking.create', $data);
}
The Blade file :
<td>
{{$row->gatewayObj ? $row->gatewayObj->getDisplayName() : ''}}
</td>
The gatewayObj :
function get_payment_gateway_obj($payment_gateway)
{
$gateways = get_payment_gateways();
if (empty($gateways[$payment_gateway]) or !class_exists($gateways[$payment_gateway])) {
return false;
}
$gatewayObj = new $gateways[$payment_gateway]($payment_gateway);
return $gatewayObj;
}
There are still missing pieces to the puzzle, so I cannot provide you with a code snippet to implement.
However, I think you should be able to diagnose it this way:
Check your controller.
Do a die-dump of the $data just above the line containing return view.... Like so: dd($data['payment_gateway'])
Then refresh the page in your browser and see if the $data object is exactly how you want it. The value should be ["xendit", "offline payment"].
Check your form
I suppose you have a form element like a <select></select>, which is iterating over the values of the $data['payment_gateway'] array. If you do not have this, how are your users choosing between the payment options?
Next, make sure that each iteration of payment gateway is being submitted properly. YOu did not include the snippet that handles form submission, but if you're using a <select> element, the options each need to be submitted with a value.
If we hardcode the select, you will have something like this:
<select name="payment_gateway">
<option value="xendit">Xendit</option>
<option value="offline">Offline Payment</option>
</select>
So when the server receives this form information, it knows the exact value of payment gateway to use. Dynamically, it could look like this:
<select name="payment_gateway">
#foreach($data['payment_gateways'] as $gateway)
<option value="{{ $gateway }}">{{ $gateway }}</option>
#endforeach
</select>
Intercept the request and check that your payment_gateway is being submitted properly.
Go to the controller method that handles your form, and do something like dd($request->all())
Then inspect the value of payment_gateway.

Laravel Validating Arrays Select fields

Laravel easily validates array based form input fields
<input name='input_name[0][0]">
<input name='input_name[0][1]">
with
'input_name.* = 'required',
https://laravel.com/docs/5.5/validation#validating-arrays
But how can I validate array based select fields?
I have a form where customer info is added, user has to choose the customer's gender and it's possible to add infinite number of customer.
So i have a select for 1 customer:
<select name="gender[0]">
<option selected="selected" disabled="disabled" hidden="hidden" value="">Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option></select>
and then
<select name="gender[1]">...
<select name="gender[N]">
When I set the rule as:
'gender.*' => 'required'
It doesn't recognize an 'unchoosen' select-box as an error....
But if I update validation rules to:
'gender[0]'=>'required'
'gender[1]'=>'required'
'gender[N]'=>'required'
It works absolutely fine... (by "it works", I mean that it returns a mistake "Gender field is required").
So, apparently Laravel has some problems with array based select names.
Appreciate any help!
public function rules() {
$rules = [];
$gender = $this->input('gender');
foreach ($gender as $index => $item) {
$rules["gender.{$index}"] = 'required';
}
return $rules;
}
I've decided o answer the question myself.
1) First solution is to make the first "placeholder" option not disabled (as was in my case, see above - it's because I use LaravelCollective and they have it by default):
<select name="gender[0]">
<option selected="selected" hidden="hidden" value="">Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option></select>
When you remove 'disabled' from option-1 of your select then it sends ' ' when posting (instead of sending nothing with 'disabled'). So it sends
gender[0] = '';
gender[1] = '';
etc...
Actually, if you have a lot of gender[N] (or maybe other array based selects) I think it's the neatest solution.
2) Second solution is provided below by omadonex:
public function rules() {
$rules = [];
$gender = $this->input('gender');
foreach ($gender as $index => $item) {
$rules["gender.{$index}"] = 'required';
}
return $rules;
}
in this case you'll have a separate rule for every array-based select and it will work (see why in the end of my topicstarter-post). I prefer this solution less than 1st one because you'll have a long list of "gender"-rules if you have a lot of ...
3) Also I've undertood why 'gender[0]'=>'required'
works
and 'gender.*' => 'required'
does not
for array based selects like <select name=gender[0]>
It's kind of obvious if you think about it: when POSTING select tag with first option (a placeholder) being disabled, as in my example above:
<option selected="selected" disabled="disabled" hidden="hidden" value="">Gender</option>
the $POST sends nothing....
So if Laravel's validation rule is 'gender[0]'=>'required' Laravel "thinks": "OKAY, I've received no "gender[0]", but I know what is required exactly ("gender[0]", of course) . I have to send a mistake because there is no "gender[0]".
But if rule is 'gender.*' => 'required' and Laravel get's no input of "gender" kind, then it also doesn't know what EXACTLY is required ('gender.*' may mean gender[0]... gender [12345] ... gender[anything]). Laravel can't send a mistake, because infinite number of gender[...] is missing, so he simply omits it....
PS. If you work with LaravelCollective forms of newer versions, they create placeholder "disabled" by default. Here is a macro to avoid it.
{!!
Form::macro('selectNonDisabled', function($value, $placeholder, $array, $disabled=null, $class=null) {
$select = "<select class='form-control $class' $disabled name='$value'>";
$select .= "<option selected='selected' hidden='hidden' value=''>$placeholder</option>";
foreach ($array as $key => $value) {
$select .= "<option value='$key'>$value</option>";
}
$select .= "</select>";
return $select;
});
!!}

New to Laravel - How to pass model data to a blade view?

Ok, I am totally re-writing this question, now that I am a bit more familiar with larval.
Here is my situation: I have a guitar lessons site based on larval 5.2.36, where each lesson belongs to a category, and within a lesson are several exercises. An exercise table does not have a category id as it is linked to a lesson which has a category.
Goal What I am trying to figure out is how to pass the category of the currently displayed lesson or exercise to a menu sidebar view that displays the categories, so that the category of the lesson or exercise is highlighted. For this, I need to understand how to do such a task in laravel.
From what I gathered, this is often done via controllers. However, there is no menu controller, but rather a menu composer. It contains a function
class MenuComposer
{
public function compose(View $view)
{
$minutes = 6 * 60;
$value = Cache::remember('menu-categories', $minutes, function() {
return \App\Category::with('parent')->with('children')->get();
});
$view->with('categories', $value);
}
}
Then in the menu blade file we have
#foreach ($categories as $category)
<?php $category = $category->present(); ?>
#if ($category->parent == null)
<li>{{ $category->title }}</li>
#foreach ($category->children as $child)
<?php $child = $child->present() ?>
<li class="level1">{{ $child->title }}</li>
<?php
/*
#foreach ($child->children as $grandChild)
<?php $grandChild = $grandChild->present() ?>
<li class="level2">{{ $grandChild->title }}</li>
#endforeach
*/
?>
#endforeach
#endif
#endforeach
So this is clear. I see that I can use the menu composer to pass additional data with a $view->with() call.
The question is how do I get the current category? For exercises and lessons, the routes don't have category data. They are of form
lesson/lessonid/lessontitle
or
exercise/exid/extitle
So I know I could do some sort of query of the model. But seems that wouldn't make sense, since I know there are other places in the process flow where the current cat is being passed. For instance, on an exercise page, the view is retrieving category as
$exercise->lesson->category->title
It is being passed this in exercise controller as
public function index($id, $name = null)
{
//$this->hit($id);
$exercise = $this->apiController->get($id);
$authorized = $this->isUserAuthorized();
return view('exercise/index', [
'exercise' => $exercise->present(),
'authorized' => $authorized,
]);
}
Similarly, a lesson controller passes $lesson object to lesson view as
public function index($id, $name = null)
{
//$this->hit($id);
$lesson = $this->apiController->get($id);
$subscribed = $this->request->user() && $this->request->user()->subscribed('premium');
return view('lesson/index', [
'lesson' => $lesson->present(),
'subscribed' => $subscribed,
]);
}
Based on above, seems I could modify the return statements in the lesson and exercise controller to pass the category to the menu view, but I don't see in the documentation how to do that, and I suspect the menu view is rendered before the lesson and exercise controller are called...
Also read about using service providers. middleware, etc, here: How to pass data to all views in Laravel 5?
But all these approaches seem overkill. I don't need every view to have the data. Seems to me, I need to do this somehow in the menu composer. But I don't know what method to use from the menu composer to retrieve the current lesson or exercise category. In the menu composer after debugging in phpstorm I see that the $view object for a lesson has $view->$data->$lesson->$entity.
So what I did was edited the menu composer to pass category to view:
$d=$view->getdata();
$s=array_key_exists ('lesson' , $d );
if ($s ==1) $attr = collect($d)->get('lesson');
$cat=$attr->cat();
This works since in the LessonPresenter I added function
public function cat()
{
$cat = $this->entity->category['attributes']['title'];
return $cat;
}
This works, but I feel like it is a hack. And I will have to do this for the Exercise Presenter as well. Being new to larval I suspect there has to be a more elegant way to do this. So can someone please explain how this should be done?
thanks,
Brian
You can use Facades of Laravel directly in blade templates.
Just use {! !} syntax to try and echo it. e.g: {!! Route::current() !!}
There are also similar functions of Route facade you can use.
Then, you can check your category with #if() ... #endif blocks and add something like class name within it.
Note: Don't put lots of logic in your blade files. Do it in your controller file (even in your other service classes) and pass simplest variables (e.g $isCurrentCategory) as an array to your template files using View::make() function's 2nd parameter.
Maybe this can help you
<a href="#" class="{{ (\Request::route()->getName() == 'routename') ? 'active' : '' }}">
You can also get the route prefix for example, you can check this out here:
Laravel API Docs Routing

Repopulating user inputs after successful validation

I am trying to create a search form where the user has to select from some dropdown menus and enter text in one of a few fields. The problem is I am redisplaying the search page with results below it. To do this I am not redirecting, I am just returning a view with the datasets I need compacted along with it.
Is there any way to get to retrieve input similar to how you would do this Input::old('x') when you were redirecting after failed validation?
The routes are:
Route::get('search', ['as' => 'main.search.get', 'uses' => 'MainController#showSearchPage']);
Route::post('search', ['as' => 'main.search.post', 'uses' => 'MainController#showSearchResults']);
Example of code I have in the view:
{!! Form::open(array('route' => 'main.search.post', 'class' => 'form-inline align-form-center', 'role' => 'form')) !!}
<div class="form-group">
{!! Form::label('product_code', 'Product Code: ',['class' => 'control-label label-top']) !!}
{!! Form::text('product_code', Input::old('product_code'), ['class' => 'form-control input-sm']) !!}
</div>
So when you submit a search, it calls showSearchResults which then returns a view if it succeeds, if it fails validation via my SearchRequest class it gets redirected to the main.search.get route, errors are printed and input is returned to the fields.
I have done a lot of searching and have come up more or less empty handed, it would be nice if there was a way to say ->withInput() when returning a view (not redirecting) or something.
Currently my only solution is to Input::flash() but since I am not redirecting that data persists for an extra refresh. That isn't a terribly big deal at this point, but I was wondering if anyone else had a better solution.
Edit - Code below from controller where view is returned:
...
Input::flash();
return view('main.search', compact('results', 'platformList', 'versionList', 'customerList', 'currencyList', 'customer', 'currency'));
}
Thank you
I had the same problem. The solution that worked for me was to add the following line into the controller.
session(['_old_input' => $request->input()]);
Now I'll explain how it works.
In the view, the global function old() is called:
<input type="username" id="username" class="form-control" name="username" value="{{ old('username') }}" placeholder="Username" autofocus>
This function is in vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
function old($key = null, $default = null)
{
return app('request')->old($key, $default);
}
This calls Illuminate\Http\Request->old():
public function old($key = null, $default = null)
{
return $this->session()->getOldInput($key, $default);
}
Which calls Illuminate\Session\Store->getOldInput():
public function getOldInput($key = null, $default = null)
{
$input = $this->get('_old_input', []);
return Arr::get($input, $key, $default);
}
This call is looking for _old_input in the session. So the solution is to add the input to the session using this value.
Hope this helps.
You can use request instead of old since its the post request
change {{old('product_code')}} to {{request('product_code')}}

Laravel explain me how working query

I have a problem with query on laravel.
Please show me how it work, because I can't understand doc.
For example, I have VideoController.php and I have some data from forms:
$gall = array(
'name' => Input::get('name'),
'user_id' => Auth::id()
);
now I want to add this data to DB, but I don't know how to call to create function in model (and how this function should look).
And please explain me, how I should select data from database and display it on view, where for example user_id = 15;
In your VideoController.php file you should have a method to store the data, which you should post your data to through a form or something.
In a view, this form should look something like this:
{{ Form::open(['route' => 'video.store']) }} <!-- check your routes to see if video.store exists, you'll get an error otherwise -->
... form elements ...
{{ Form::submit('Submit') }}
{{ Form::close() }}
In your store() function you should have code similar to this
$video = new Video(); // if your video model is Video.php
$video->name = Input::get('name');
$video->user_id = Auth::id();
$video->save();
And if you want to display data, i.e. your video index or something, in your VideoController.php file, in the index() function:
$videos = Video::all();
return View::make('video.index', array('videos' => $videos));
then in your view
#foreach($videos as $video)
{{ $video->name }}
#endforeach

Resources