I need two variable in one foreach.
Now i'm having like this:
#php $championships = \App\Models\Championship::all(); #endphp
<div class="tour-list">
#foreach($tours as $tour)
<div class="tour-list-box row">
<input type="text" value="{{$tour->name}}" class="col-md-3 input-styles">
<input type="date" value="{{$tour->start_date}}" class="col-md-3 input-styles">
<select name="" id="" class="col-md-3 input-styles">
#foreach($championships as $championship)
#if($championship->deleted)
<option value="">select championship</option>
#else
<option value="">{{$championship->name}}</option>
#endif
#endforeach
#if($tour->championship->deleted)
#else
<option selected value="">{{$tour->championship->name}}</option>
#endif
</select>
<!-- all championships -->
<span tour_id="{{$tour->id}}" class="btn btn-sm btn-primary pull-left col-md-1 tour-remove-button"><span class="glyphicon glyphicon-remove"></span></span>
<span championship_id="{{$championship->id}}" class="btn btn-sm btn-primary pull-right col-md-1 tour-edit-input"><span class="glyphicon glyphicon-edit"></span> </span>
</div>
#endforeach
</div>
But it's repeated 2 times so i need this 2 variables $tours and $championships in one foreach.
I tried this:
#foreach(array_combine($championships, $tours) as $championship => $tour)
#endforeach
But i'm getting error ' array_combine(): Argument #1 ($keys) must be of type array, '
is it passable to have 2 variables?
Thanks for answers <3
First of all,it's not recommended to use the model directly inside the blade file,instead putt all this logic inside the controller and pass one variable to the view.
second,all eloquent are objects(collections) so you cant deal with them as array ,to treat them ass array you need $championships = \App\Models\Championship::all()->toArray()
third,array_combine — Creates an array by using one array for keys and another for its values you need to use $array = array_unique(array_merge($array1, $array2)); instead to merge 2 arrays int one array containing only unique values
Related
There are space comes after every character of field name while displaying errors in blade template in laravel 9 as mentioned in uploaded image
my code is mentioned as below
CODE IN HTML IS :
<form action="{{route('saveAsset')}}" method="post">
{{#csrf_field()}}
<div class="form-group">
<label for="assetTitle" class="mt-2 mb-2">{{$assetCategory->category}} TITLE :</label>
<input type="text" class="form-control" name="assetTitle" id="assetTitle" placeholder="Enter Asset Title">
</div>
<div class="inputError">#error('assetTitle') Error: {{ $message }} #enderror </div>
<input type="hidden" name="assetCateId" id="assetCateId" value="{{$assetCategory->id}}">
#if(count($attributes) > 0)
#foreach($attributes as $attribute)
<label for="assetType-{{$attribute->attr_id}}" class="mt-4 mb-2">{{$attribute->attr_title}} : </label>
<div class="form-group">
<select class="form-select" name="{{$attribute->attr_title}}" id="attribute-{{$attribute->attr_id}}" aria-label="Default select example">
<option value="">Select {{$attribute->attr_title}}</option>
#foreach($attrValues as $attrValue)
#if ($attrValue->attr_id == $attribute->attr_id && strlen($attrValue->value) > 0 )
<option value="{{$attribute->attr_id}}-{{$attrValue->value_id}}" > {{$attrValue->value}}</option>
#endif
#endforeach
</select>
</div>
<div class="inputError"> #error("{$attribute->attr_title}") Error: {{ $message }} #enderror </div>
#endforeach
#endif
<div>
<button type="submit" class="btn btn-primary mt-3 px-4">Save</button>
</div>
</form>
CODE IN CONTROLLER IS :
$fields = $req->input();
$ValidationRules=[];
foreach($fields as $fieldName=>$fieldvalue){
if($fieldName == "assetTitle"){
$ValidationRules[$fieldName] = 'required|unique:assets,title';
}else{
$ValidationRules[$fieldName] = 'required';
}
}
$req->validate($ValidationRules);
I guess you have made some changes in resources/lang/en/validation.php file.
in this or any language you have set , there is a attribute named attributes
that you can change the the attribute name displaying in error message.
because the error message of asset field is ok I do not think it's a general error . check this file and attributes or messages key in it.
Don't know if you still need help but I just had the same issue and I think it's because you might have the input names with text uppercase, laravel automaticly adds a space for each uppercase character in the name attribute when showing the errors.
For example:
name="thisModel"
Will display as:
... this Model ...
If you have:
name="MODEL"
Will display as:
... M O D E L ...
Hope this helps someone in the future x)
I am a beginner in Laravel. I want to implement a feature that:
I want to use Laravel 8 and PHP:
Select a part of the data in a table (via a select)
how to get the id selected in the select :
-> get the identifier selected in the option tag in order to use it in an sql query that combines selection and insertion
Copy the data and insert it in another table (via an add button)
how to recover the id present in the url of the page
Display the data inserted in a table
no problem
<select name="id_segment" class="form-control">
<?php
$req1 = DB::select("SELECT id_segment, nom_segment FROM `segements` ");
foreach ($req1 as $re1 => $r1) {
?>
<option name="id_segment" value="{{ $r1->id_segment }}"> {{ $r1->id_segment }}
{{ $r1->nom_segment }}</option>
<?php
} ?>
</select>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary" id="AjoutSegment">Ajouter</button>
<a class="btn btn-primary" href="{{ route('groupecampagnes.index') }}"> Back</a>
</div>
I have already tried with $_POST and $_GET while making a var_dump, but I have seen that some people say that it does not work, me it gave me the display of an error with the id that I seek to recover precisely, whereas it should just display it to me on the page to see if I recover it well and truly. The var_dump is not present in the code because I removed it in the meantime. After I tried various solutions found on the net, which have me display as error for some of them that I had to do with a std class object.
Assuming your code is in a file called page and stored in the views directory (resources/views/page.blade.php), you could go to your controller and pass the needed data to your view like this:
$segments = DB::select("SELECT id_segment, nom_segment FROM `segements` ")->get();
return view('page', compact('segments'))
Then you can use it in your view:
<select name="id_segment" class="form-control">
#foreach ($segments as $r1)
<option name="id_segment" value="{{ $r1->id_segment }}"> {{ $r1->id_segment }} {{ $r1->nom_segment }}</option>
#endforeach
</select>
I'm working on a reporting page using php Laravel framework. I select the options from the comboboxes, these values are sent to the controller to replace them in some queries used to display the reports.
When I click the submit button, the page refreshes and the name shown is the last element of the dropdown.
My Controller:
$negocioDive = DB::table('ma_leads_reporte')
->selectRaw('NEGOCIO, year(FEC_CREACION) as AÑO')
->groupBy('NEGOCIO', 'AÑO')
->get()->toArray();
$selectFecha = DB::table('param_fecha')
->selectRaw('mesNum, mesNom')
->groupBy('mesNum', 'mesNom')
->get()->toArray();
$año = $request ->input('año');
$mes = $request -> input('mes');
$negocio = $request -> input('negocio');
//Solicitud de Cotización versus mes anterior
$mesAnt = DB::table('ma_leads_reporte')
->selectRaw('count(*) as C, day(FEC_CREACION) AS DIA, monthname(FEC_CREACION) AS MES')
->whereMonth('date_identified', '=', $mes-1)
->whereYear('date_identified', '=', $año)
->groupBy('MES', 'DIA')
->get()->toArray();
$vscont = array_column($mesAnt, 'C');
$antMes = array_column($mesAnt, 'MES');
$dia = array_column($mesAnt, 'DIA');
My View:
<div class="container">
<div class="row justify-content-center">
<div class="content mt-3">
<div class="animated fadeIn">
<div class="col-lg-8">
<div class="card">
<div class="card-header">
<form action="{{route('reportes')}}" method="GET" id="fecha">
{{Form::label('', 'Año')}}
<select id="año" name="año">
#foreach($negocioDive as
$negocioD)
<option value="{{$negocioD->AÑO}}"
selected="selected">{{$negocioD->AÑO}}</option>
#endforeach
</select>
{{Form::label('', 'Mes')}}
<select id="mes" name="mes">
#foreach($selectFecha as $fecha)
<option value="{{$fecha->mesNum}}" name="{{$fecha->mesNom}}">{{$fecha->mesNom}}</option>
#endforeach
</select>
{{Form::label('', 'Negocio')}}
<select id="negocio" name="negocio" >
#foreach($negocioDive as $negocioD)
<option value="{{$negocioD->NEGOCIO}}" selected="selected">{{$negocioD->NEGOCIO}}</option>
#endforeach
</select>
<button type="submit"class="btn btn-default" id="filter">Filtrar
<i class="fa fa-filter"></i>
</button>
</form>
<?php if(isset($_GET['año']) && isset($_GET['mes']) && isset($_GET['negocio'])){
echo "<h4> Año: $año | Mes: {$fecha->mesNom} | Negocio: $negocio </h4>";
}?><!--
<p id="mesJS"></p> -->
</div>
</div>
</div>
After receiving the request variables, it should show the correct values instead of the last index of the dropdown.
What can I do?
dropdown options:
After submit:
The reason it is showing the last index of the dropdown is that you are looping on your various items and creating your options within each select box with 'selected' overwritten every time all the way to the last one in the loop.
To fix this, you'll need to add some type of conditional on the option box to tell it if the loop item is the correct selected one. If the new model has the key within this loop, then, and only then, make it selected. Something like this (You'll need to make this code your own):
#foreach($negocioDive as $id=>name)
<option value="{{$id}}" {{$theThingYouAreSelecting->id === $id?"selected":''}}>$name</option>
#endforeach
NOTE - I'm assuming you are sending a key->value array to the foreach as this is what the option expects - the key would be the id and the value would be the name for the item.
Once you get this working, I strongly recommend looking at LaravelCollective HTML - it binds the model to the form and automates a huge chunk of the conditionals.
in my Laravel App I have a Blade "Insert Project" where I want to dynamic load html data from Database when a DropDown changes. To be more concrete: I have several Project Categories in the Table "Cats" (id, name, code) - Each Category needs different fields to be added, therefore I have put the html code in the Table "Cats". I want to achieve the following:
If a user selects a Project Category the html code which is attached to the category should be shown.
My Code so far:
<form method="POST" action="{{ route('project-insert') }}">
#csrf
<form action="{{ route('filter') }}" method="post">
<label for="Cat">Kategorie</label>
<select class="form-control" name="cat_id" id="cat_id" data-parsley-required="true" onchange='this.form.submit()'>
#foreach ($cats as $sn)
{
<option value="{{ $sn->id }}">{{ $sn->name }}</option>
}
#endforeach
</select>
</form>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Add Project') }}
</button>
</div>
</div>
</form>
In my Controller I have add:
public function insertProject(Request $data) {
return $data['cat_id'];
}
public function filter(Request $request)
{
$cat_id = $request->input("cat_id");
return $cat_id;
$code = DB::table('cats')->where('id', '=', $cat_id);
var_dump($code->code);
}
When I change the DropDown, then the function "insertProject" will be called and not the desired function "filter".
How can I achieve it to call the correct function, or how can I achieve it to call "insertProject" only when the button has been pressed?
Thank you for your advice and help.
Stefan
the function sumqtyin() inside codeigniter model
function sumqtyin(){
$kdbahan = $_POST['kode_bahan_baku']; //i wanna echo a value from the input text kode_bahan_baku but it always says error "undefined index kode_bahan_baku"
$datenow = date("Y-m-d");
return $this->db->query("
SELECT IFNULL(SUM(qty_in),0) AS qty_in
FROM trans_stock_movement
WHERE tanggal_movement='$datenow'
AND status_aktif='YES' AND kode_bahan_baku = '$kdbahan' ");
}
my view input text for kode_bahan_baku inside a post form
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Bahan Baku</label>
<div class="col-sm-2">
<div class="input-group">
<input type="text" class="form-control" id="nama_bahan_baku" name="nama_bahan_baku" placeholder="Bahan Baku" value="" style="width:150px" required="required">
<input type="text" id="kode_bahan_baku" name="kode_bahan_baku" value="" class="form-control">
<div class="input-group-btn">
<button type="button" class="btn btn-info btn-flat" data-toggle="modal" id="btnMenu" data-target="#menuModal">
<i class="fa fa-fw fa-search"></i>
</button>
</div>
</div>
</div>
</div>
please help :(
Your controller is the go-between of your view and your model, as such, you should be grabbing the data out using $this->input->post('kode_bahan_baku') in the controller and passing those details through to the model:
Controller:
$kdbahan = $this->input->post('kode_bahan_baku');
$dbResultObj = $this->yourLoadedModel->sumqtyin($kdbahan);
then use the object $dbResultObj like: $dbResultObj->qty_in to get out the result
Model:
function sumqtyin($kdbahan){
$datenow = date("Y-m-d");
return $this->db->select('SUM(IFNULL(qty_in,0)) AS qty_in')
->where('tanggal_movement', $datenow)
->where('status_aktif', 'YES')
->where('kode_bahan_baku', $kdbahan)
->get('trans_stock_movement')
->result();
}
Please Follow the MVC structure , Before you make any manipulation on database you just have to follow the MVC method .
VIEW => CONTROLLER => MODEL => CONTROLLER => VIEW
like #Brian Ramsey did . check out the guidelines of codeigniter
https://www.codeigniter.com/user_guide/