I need to add a drop-down menu that filtrates with enum values from the table. Instead of that it gets the values from the rows. Any and all help gratefully received.
This is what I have managed to get so far.
View:
<div>
<span>
<select class="mb-2">
<option value="Status" disabled selected>Status</option>
#foreach ($status_filter as $key => $status)
<option value="{{ $key }}">
{{$status}}
</option>
#endforeach
</select>
</span>
</div>
Controller:
$status_filter = Competition::pluck('status');
$request->flash();
return view('competition.index', compact('competitions', 'status_filter'));
This is what I need to get from the migration:
$table->enum('status',['inactive','to_push','active'])->default('inactive');
You can grab those enum values from the table. But there is no direct way. You have to use laravel query builder using raw.
$statusValues = DB::select(DB::raw("SHOW COLUMNS FROM competitions WHERE Field = 'status' "))[0]->Type;
$matches = array();
preg_match('/^enum\((.*)\)$/', $statusValues, $matches);
$enumValues = array();
foreach( explode(',', $matches[1]) as $value )
{
$v = trim( $value, "'" );
$enumValues = $v;
}
print_r($enumValues)
If i got it right you want to make a filter for status like listing all active ou inactive. I think the easiest way to do this is add a query on your variable $status_filter on the controller. Something like: $status_filter = Competition::where('status', 'active')->pluck('status');
As #SazzadHussain suggested adding this block of code inside my model class solved the issue.
public static function getPossibleEnumValues($table, $column) {
$type = DB::select(DB::raw("SHOW COLUMNS FROM $table WHERE Field = '{$column}'"))[0]->Type ;
preg_match('/^enum\((.*)\)$/', $type, $matches);
$enum = array();
foreach( explode(',', $matches[1]) as $value )
{
$v = trim( $value, "'" );
$enum = Arr::add($enum, $v, $v);
}
return $enum;
}
After that getting it from my Controller like that:
$enumoption = Competition::getPossibleEnumValues('competitions','status');
Related
I am new to programming , i have a problem while using explode function ie, after i explode the value and pass those value to a model to retrive records from db only the last value from the exploded data is showing.
ex i have impoded data as (test,test1,test2) after explode the data is like(testtest1test2) here i pass these value to model to take records from a table where a field name is equal to the exploded value.I dont't know how to explain this properly please forgive me.
public function location($id) {
$query = $this->gt_pav_model->select($id);
$data['selectdata'] = $query->result();
foreach( $data['selectdata'] as $s) {
$explode = $s->package_id; // this is to get imploded data from db
}
$t = explode(",", $explode);
foreach( $t as $tt) {
$query = $this->gt_pav_model->select_pa($tt);
$data['dataa'] = $query->result();
$this->load->view('pav/pavdetails',$data);
}
}
public function location($id) {
$final_result = array();
$result= $this->gt_pav_model->select($id)->result();
foreach( $result as $key=> $re) {
$explode = $re->package_id;
$t = explode(",", $explode);
$param = $t[1];
$datas = $this->gt_pav_model->select_pa($param)->result();
$final_result[$key] => $datas;
}
$this->load->view('pav/pavdetails',array('datas'=>$final_result));
}
You can print result in your view page <?php print_r($datas); ?>
So...
I have this foreach loop with information about a product and I want to save the information to a seperate array for each loop.
I was thinking of doing something like this:
$return_array = array();
foreach($items as $item) {
$return_array[] = $item;
}
But I have troubles doing it like this since I am using input values from html and I need to add those before they get send to a database.
My foreach goes like:
foreach($items as $item) {
<table>
<tr>
<td>
<input value="<?= $item->name ?>" name="item<?= $item->id ?>">
</td>
</tr>
... more table tags
<?php foreach($item as $key) { ?>
<input name ="item<?= $item->id ?>_label<?= $key->label ?>
<?php } ?>
... more table tags
<select name="item<?= item->id ?>_status>
//Choose the state the product is in
<option value="damaged">
<option value="good">
</select>
So after this gets submitted with the form (this is in a btw) I get something like this:
(depending on how many labels the product has this number can increase)
$array =
['item1'] = 'test';
['item1_label1'] = 123;
['item1_label2'] = 213;
['item1_status'] = 'good';
['item2'] = 'test2';
['item2_label1'] = 112;
['item2_label2'] = 1232;
['item2_label3'] = 132;
['item2_status'] = 'broken';`
Now what I want would be:
$array =
['item1'] = array[ //name of this doesn't matter
['item1'] = 'test'; // name
['item1_label1'] = 123; //label
['item1_label2'] = 213; //label
['item1_status'] = 'good'; //status
],
['item2'] = array[
['item2'] = 'test2'; //name
['item2_label1'] = 112; //label
['item2_label2'] = 1232; //label
['item2_label3'] = 132; //label
['item2_status'] = 'broken' //status
]
];
I want to create this information from the form. (also the number of items can increase).
try this,
$result = [];
foreach($array as $k => $v)
{
//$result[substr($k, 0, 5)][$k] = $v;
if(strpos($k, '-') === FALSE)
$result[$k][$k] = $v;
else
$result[substr($k, 0, strpos($k, '-'))][$k] = $v;
}
I'm having trouble getting a Form Request to find a multi select input with Laravel. My request input always appears to be NULL. Is there anything I'm missing or wrong with my Syntax?
I've seen some examples mention dot notation syntax for multi selects in Laravel?
e.g.
$name = $request->input('products.0.name');
$names = $request->input('products.*.name');
But I'm assuming this is for multi dimensional selects?
I'm passing my request through to a Repository for data action
<select id="team" name="team[]" class="team" multiple>
#foreach ( $teams as $team )
<option value="{{ $team->id }}">{{ $team->name }}</option>
#endforeach
</select>
use Illuminate\Support\Facades\Input;
public function createUser( CreateUserRequest $request ){
$array = DB::transaction(function($request) use ($request)
{
$name = $request->input('name');
$email =$request->input('email');
// $team = $request->input('team[].*');
$s = $request->input('team');
$t =$request->get('team');
var_dump($s);//NULL
var_dump($t);//NULL
die();
Please check it may be working.
$name = $request->input('name');
$email =$request->input('email');
// array field
$team = $request->input('team');
foreach($team as $key=>$value){
$data->name = $name;
$data->email = $email;
// array value
$data->item = $value;
// save in datatabase
$data->save();
}
I decided to try out Laravel and wanted to create dropdowns for my form. I created a convinience method inside my BaseController that I use to get the data from the database. Below is the function listing:
protected function getList( $model, array $fields, $empty = 'Select option below' )
{
$options = array();
$owner = strtolower( $model ) . 's';
$records = $model::lists($fields[1], $fields[0]);
if( !empty( $records ) ){
foreach( $records as $key => $value ){
$options[$owner][$value] = $key;
}
}
$options[$owner][''] = $empty;
return $options;
}
And then in the controller you can just use it like:
//Get groups
$groups = $this->getList( 'Group', array(
'name', 'id'
));
//Get Project Managers
$project_managers = $this->getList( 'ProjectManager', array(
'name', 'id'
));
The output of the select form control has an optgroup that references the owner of the list or the model rather. How can I remove the optgroup such that its not part of the contents of the dropdown?
Below is the output of the form:
<div class="input select">
<label for="project_manager">Project Manager</label>
<select name="project_manager_id">
<optgroup label="projectmanagers">
<option value="1">Riyaadh</option>
<option value="2">Luyanda</option>
<option selected="selected" value="">Select option below</option>
</optgroup>
</select>
</div>
You are adding another level within your getLists method with this line; $options[$owner][$value] = $key;, this is what is adding your optgroup, to get rid of this change the line to $options[$value] = $key;.
Optionally you can reduce the code of your getLists function to the following;
protected function getList( $model, array $fields, $empty = 'Select option below' )
{
$options = array();
$owner = strtolower( $model ) . 's';
$records = $model::lists($fields[1], $fields[0]);
if( !empty( $records ) ){
$options = array_flip($records);
}
$options[''] = $empty;
return $options;
}
This uses the array_flip method, which exchanges the keys as values and values as keys.
Try this...
$SchoolDetails = GeneralSettingModel::lists('SchoolName', 'id');
or
$SchoolDetails = DB::table('tablename')->lists('SchoolName', 'id');
{{ Form::select('SchoolName',array(''=>'Select School')+$SchoolDetails,null, array('id'=> 'SchoolName'))}}
Output:
<select id="SchoolName" name="SchoolName">
<option value="" selected="selected">Select School</option>
<option value="1">test</option>
<option value="2">test355wrew</option>
<option value="3">GOVT</option>
<option value="4">Kumaraguru</option>
<option value="5">valavan</option>
</select>
you can remove using JQuery by adding ID to select
$(function () {
$("#mySelect").children().remove("optgroup");
});
As Matt Burrow suggested it turned out that I was creating a multi-dimentional array which is translated to a list with optgroup.
I modified my function to rather exclude the owner which the records belong to. Below is my perfectly working function (excerpt):
...
if( !empty( $records ) ){
foreach( $records as $key => $value ){
$options[$value] = $key;
}
}
$options[''] = $empty;
...
I have the following code that searches my models in Laravel 4 for a search phrase. It uses 'IN BOOLEAN MODE' and MATCH() and AGAINST().
public function scopeSearch($query, $q)
{
$fields = Static::getFields();
$fields = implode(', ', $fields);
$query->whereRaw("MATCH(" . $fields . ") AGAINST('" . $q . "' IN BOOLEAN MODE)");
}
public static function getFields()
{
$field_names = array();
$disallowed = array('id', 'created_at', 'updated_at', 'deleted_at');
$columns = DB::select('SHOW COLUMNS FROM accounts');
foreach ($columns as $c) {
$field = $c->Field;
if ( ! in_array($field, $disallowed)) {
$field_names[$field] = $field;
}
}
return $field_names;
}
I'd like help modifying the code above to allow a user to search the fields using partial words and phrases. For example, if a user types purple, I'd like the search to also find any records with email addresses containing the word purple, so info#purplegriffon.com. So, essentially partial matches.
I'd also like to be able to find everything containing griffon in the field for the typed phrase john griffon, even if john does not exist.
Can anyone help me out with this? Cheers.
OK, I've got it working as best I can with FULLTEXT search and using wildcard operators to search for partials. This may not be the best solution but it works.
public function scopeSearch($query, $q)
{
$fields = Static::getFields();
$fields = implode(', ', $fields);
$terms = explode(' ', $q);
if (count($terms) > 1) {
$query->whereRaw("MATCH(" . $fields . ") AGAINST ('" . $q . "' IN BOOLEAN MODE)");
} else {
foreach ($terms as $term) {
$query->whereRaw("MATCH(" . $fields . ") AGAINST ('*" . $term . "*' IN BOOLEAN MODE)");
}
}
}
public static function getFields()
{
$field_names = array();
$disallowed = array('id', 'country_id', 'created_at', 'updated_at', 'deleted_at');
$columns = DB::select('SHOW COLUMNS FROM venues');
foreach ($columns as $c) {
$field = $c->Field;
if ( ! in_array($field, $disallowed)) {
$field_names[$field] = $field;
}
}
return $field_names;
}
If anyone can either simplify or improve this, then I would love to see it. Cheers.
i got solution by :
$terms = mysql_real_escape_string($terms);
$contact_results = Contact::where_account_user_id(Auth::user()->account_user_id)
->raw_where("match (`first`, `last`) against ('{$terms}*' IN BOOLEAN MODE)")
->where_deleted(0)
->paginate(20);
phpconsole($contact_results->results);
return $contact_results;