Getting all items in the collection mapWithKeys - laravel

I have:
$collection=Event::all();
$keyed = $collection->mapWithKeys(function ($item) {
return ['title' => $item['name'], 'start' => $item['event_date']];
});
$keyed->all() is returning only last item in the collection with changed keys:
Array
(
[title] => New Year
[start] => 2018-01-01
)
How do i get all events with changed keys?

The callback of mapWithKeys return an associative array conaining a single key / value pair, so if you just need name and event_date I suggest to use pluck like this:
$collection=Event::pluck('event_date', 'name');

Related

Laravel returning "Object" instead of Array?

I'm executing a query like this as well as some others and returning them via response()->json().
$transactions = Transaction::where('created_at', '>=',
now()->firstOfYear())->get()->groupBy(function ($transaction)
{
return Carbon::parse($transaction->created_at)->format('M');
});
return response()->json([
'user' => $user->toArray(),
'transactions' => $transactions->toArray()
]);
However, while transactions is an Array in php, when it goes through response()->json it gets turned into an Object. I was hoping someone could tell me how I can prevent this and keep it as an array so I can iterate over it?
Thanks.
Picture of transactions output as requested. (Had to blur a lot of stuff due to sensitive info.)
Your array is keyed with month names, meaning it is an associative array. If you want the JSON to be an array, you will need your PHP array to be indexed numerically.
One option you can do is this (untested):
$userArray = [];
foreach ($user as $key => $value) {
$userArray[] = (object) [
'month' => $key,
'data' => $value,
];
}
return response()->json([
'user' => $userArray,
'transactions' => $transactions->toArray()
]);
That will make it a numerically indexed array of objects with the month being a property on the object and another property containing the rest of the data.
A solution I used before is to check on the frontend whether the data is array or an object, and if it is an object just convert it to array. Associative arrays will get converted to objects in javascript unless its keys start from 0 and increment like a normal arrays index.
An example of doing this:
window.axios.post('api/endpoint', data)
.then(res => {
const transactions = Array.isArray(res.data.transactions)
? res.data.transactions
: Object.keys.(res.data.transactions).map(key => response.data.transactions[key]);
})

In eloquent fetching data in fieldname=>fieldvalue keys

In laravel 5.8 making request
$searchResultsArray = SearchResult
::getBySourceId($next_sourceId['source_id'])
->select( 'field', 'value' )
->get()
->toArray();
I got resulting rows like :
$searchResultsArray::Array
(
[0] => Array
(
[field] => Id
[value] => 302530
)
[1] => Array
(
[field] => Domain
[value] => site.com
)
If there is a way to get
array of fieldname=>fieldvalue keys, like
Id => 302530
Domain=> site.com
without additive foreach block in my code?
If yes how?
Yes there is. Eloquent always returns collections. You could modify the collection first with mapWithKeys() (https://laravel.com/docs/5.8/collections#method-mapwithkeys) and then convert it to an array.
Try something like this:
$searchResultsArray = SearchResult
::getBySourceId($next_sourceId['source_id'])
->select( 'field', 'value' )
->get()
->mapWithKeys(function ($item) {
return [$item['field'] => $item['value']];
})
->all();

How to create a single array using two iterating loop and than update_batch

How do I take id on every iteration from check_seeds array and add on each itteration into seeded[] array.
In more simple words, I want to take an item from the first iteration and add into the first iteration, take an item from the second iteration and add into the second iteration and so on...
Actually, on update_batch we need third parameter (primary key, index) to update array values in database rows where id from database rows matches with the id in update_batch.
$check_seeds = $this->tournament_model->get_seeds($tournament_id);
$seeds = $this->input->post('seed');
foreach ($seeds as $key => $value){
if(!empty($key) && !empty($value)){
$seeded[] = array(
'id' => (Add id here),
'tournament_id' => $tournament_id,
'stage_id' => $stage_id,
'seed_id' => $value,
'team_name' => $key,
);
$this->db->update_batch('tournament_seed', $seeded, 'id');
redirect('organizer/tournaments);
}
}
print_r($check_seeds)
Array
(
[0] => Array
(
[id] => 3
[tournament_id] => 713161746
[stage_id] => 3
[seed_id] => 3
[team_name] => -V-ATTAX
)
[1] => Array
(
[id] => 4
[tournament_id] => 713161746
[stage_id] => 3
[seed_id] => 3
[team_name] => -V-ATTAX
)
[2] => Array
(
[id] => 5
[tournament_id] => 713161746
[stage_id] => 3
[seed_id] => 3
[team_name] => -V-ATTAX
)
)
in your model function get_seeds() you can query the current max value of id as an alias and return it together with the query result:
function get_seeds($tournament_id) {
$this->db->select_max('id', 'max_id');
$this->db->where('tournament_id', $tournament_id);
$result = $this->db->get('tournament_seed');
return $result->result();
}
then in your controller's for_each() you increment that value:
$i=0;
foreach ($seeds as $key => $value){
$i++;
$seeded[] = array(
'id' => $value->max_id + $i,
//....
);
}
Codeigniter docs: Selecting Data, scroll down to select_max(), as there is no internal bookmark

Laravel insert or update multiple rows

Im new in laravel, and im trying to update my navigation tree.
So i want to update my whole tree in one query without foreach.
array(
array('id'=>1, 'name'=>'some navigation point', 'parent'='0'),
array('id'=>2, 'name'=>'some navigation point', 'parent'='1'),
array('id'=>3, 'name'=>'some navigation point', 'parent'='1')
);
I just want to ask - is there posibility in laravel to insert(if new in array) or update my current rows in database?
I want to update all, because i have fields _lft, _right, parent_id in my tree and im using some dragable js plugin to set my navigation structure - and now i want to save it.
I tried to use
Navigation::updateOrCreate(array(array('id' => '3'), array('id'=>'4')), array(array('name' => 'test11'), array('name' => 'test22')));
But it works just for single row, not multiple like i tried to do.
Maybe there is another way to do it?
It's now available in Laravel >= 8.x
The method's first argument consists of the values to insert or update, while the second argument lists the column(s) that uniquely identify records within the associated table. The method's third and final argument is an array of columns that should be updated if a matching record already exists in the database:
Flight::upsert([
['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
], ['departure', 'destination'], ['price']);
I wonder why this kind of feature is not yet available in Laravel core (till today). Check out this gist The result of the query string would look like this: here
I am putting the code here just in case the link breaks in the future, I am not the author:
/**
* Mass (bulk) insert or update on duplicate for Laravel 4/5
*
* insertOrUpdate([
* ['id'=>1,'value'=>10],
* ['id'=>2,'value'=>60]
* ]);
*
*
* #param array $rows
*/
function insertOrUpdate(array $rows){
$table = \DB::getTablePrefix().with(new self)->getTable();
$first = reset($rows);
$columns = implode( ',',
array_map( function( $value ) { return "$value"; } , array_keys($first) )
);
$values = implode( ',', array_map( function( $row ) {
return '('.implode( ',',
array_map( function( $value ) { return '"'.str_replace('"', '""', $value).'"'; } , $row )
).')';
} , $rows )
);
$updates = implode( ',',
array_map( function( $value ) { return "$value = VALUES($value)"; } , array_keys($first) )
);
$sql = "INSERT INTO {$table}({$columns}) VALUES {$values} ON DUPLICATE KEY UPDATE {$updates}";
return \DB::statement( $sql );
}
So you can safely have your arrays inserted or updated as:
insertOrUpdate(
array(
array('id'=>1, 'name'=>'some navigation point', 'parent'='0'),
array('id'=>2, 'name'=>'some navigation point', 'parent'='1'),
array('id'=>3, 'name'=>'some navigation point', 'parent'='1')
)
);
Just in case any trouble with the first line in the function you can simply add a table name as a second argument, then comment out the line i.e:
function insertOrUpdate(array $rows, $table){
.....
}
insertOrUpdate(myarrays,'MyTableName');
NB: Be careful though to sanitise your input! and remember the timestamp fields are not touched. you can do that by adding manually to each arrays in the main array.
I've created an UPSERT package for all databases: https://github.com/staudenmeir/laravel-upsert
DB::table('navigation')->upsert(
[
['id' => 1, 'name' => 'some navigation point', 'parent' => '0'],
['id' => 2, 'name' => 'some navigation point', 'parent' => '1'],
['id' => 3, 'name' => 'some navigation point', 'parent' => '1'],
],
'id'
);
Eloquent Style
public function meta(){ // in parent models.
return $this->hasMany('App\Models\DB_CHILD', 'fk_id','local_fk_id');
}
.
.
.
$parent= PARENT_DB::findOrFail($id);
$metaData= [];
foreach ($meta['meta'] as $metaKey => $metaValue) {
if ($parent->meta()->where([['meta_key', '=',$metaKey]] )->exists()) {
$parent->meta()->where([['meta_key', '=',$metaKey]])->update(['meta_value' => $metaValue]);
}else{
$metaData[] = [
'FK_ID'=>$fkId,
'meta_key'=>$metaKey,
'meta_value'=> $metaValue
];
}
}
$Member->meta()->insert($metaData);
No, you can't do this. You can insert() multiple rows at once and you can update() multiple rows using same where() condition, but if you want to use updateOrCreate(), you'll need to use foreach() loop.
I didn't find a way to bulk insert or update in one query. But I have managed with only 3 queries. I have one table name shipping_costs. Here I want to update the shipping cost against the shipping area. I have only 5 columns in this table id, area_id, cost, created_at, updated_at.
// first get ids from table
$exist_ids = DB::table('shipping_costs')->pluck('area_id')->toArray();
// get requested ids
$requested_ids = $request->get('area_ids');
// get updatable ids
$updatable_ids = array_values(array_intersect($exist_ids, $requested_ids));
// get insertable ids
$insertable_ids = array_values(array_diff($requested_ids, $exist_ids));
// prepare data for insert
$data = collect();
foreach ($insertable_ids as $id) {
$data->push([
'area_id' => $id,
'cost' => $request->get('cost'),
'created_at' => now(),
'updated_at' => now()
]);
}
DB::table('shipping_costs')->insert($data->toArray());
// prepare for update
DB::table('shipping_costs')
->whereIn('area_id', $updatable_ids)
->update([
'cost' => $request->get('cost'),
'updated_at' => now()
]);
in your controller
use DB;
public function arrDta(){
$up_or_create_data=array(
array('id'=>2, 'name'=>'test11'),
array('id'=>4, 'name'=>'test22')
);
var_dump($up_or_create_data);
echo "fjsdhg";
foreach ($up_or_create_data as $key => $value) {
echo "key ".$key;
echo "<br>";
echo " id: ".$up_or_create_data[$key]["id"];
echo "<br>";
echo " Name: ".$up_or_create_data[$key]["name"];
if (Navigation::where('id', '=',$up_or_create_data[$key]["id"])->exists()) {
DB::table('your_table_ name')->where('id',$up_or_create_data[$key]["id"])->update(['name' => $up_or_create_data[$key]["name"]]);
}else{
DB::insert('insert into your_table_name (id, name) values (?, ?)', [$up_or_create_data[$key]["id"], $up_or_create_data[$key]["name"]]);
}
}

JRegistry exists() returns empty array

I was trying to save a form data to DB. In the controller save() function there is a statement
$data = $model->validate($form, $data);
But it always returns empty. I tracked down the problem to the filter() function in /libraries/joomla/form/form.php (comes with joomla package). Here is some code (shortened):
$input = new JRegistry($data);
$output = new JRegistry;
foreach ($fields as $field)
{
// Initialise variables.
$name = (string) $field['name'];
if ($input->exists($name)){
$output->set($name, $this->filterField($field, $input->get($name, (string) field['default'])));
}
}
$input looks like :
JRegistry Object ( [data:protected] => stdClass Object ( [jform] => stdClass Object ( [title] => Utility Model/Patent application [ap_name] => d ...) [option] => com_eipoapplications [task] => save ) )
And each $name in the loop always contain the form element name (like 'title', 'ap_name' ... ).
But the if conditional statement always returns false. Does any one help me know why JRegistry exists() function is not finding the elements?
I think you are having an inconsistency between form and data.
Let's say the form contains a field with name title.
$data array should have a value under key of same name:
$data = array(
'title' => 'Utility Model/Patent application',
'ap_name' => 'd'
);
Or using print_r
Array
(
[title] => Utility Model/Patent application
[ap_name] => d
)
If there's no data for such field, validation is omitted. If all data keys are wrong, function returns empty array.
The question is, how it happened :/

Resources