$retval = array_values(['_id]) ; you're missing a quote in there.
How to use DB facade with helper
Hi guys,
a noob question, I've installed this helper package https://github.com/browner12/helpers and i've created my first helper function, needless to say it does not work. Could somebody be kind enough to point out my errors:
<?php
//namespace app\Helpers;
use Jenssegers\Mongodb\Eloquent\Model;
use Illuminate\Support\Facades\DB;
if (!function_exists('get_next')) {
/**
* description
*
* @param
* @return
*/
function get_next( $name )
{
$data = DB::table('counter')
->where('name', $name )->increment('_id');
$retval = array_values(['_id]) ;
return $retval;
}
}
Well spotted, but still the value in the database does not change and the the returned value is Array ( [0] => _id )
Maybe I'm not calling it properly:
$db_array = get_next(array('name' => 'role_id'));
print_r( $db_array );
Did you add the helpers you want to use.
Not sure what you mean, I just ran php artisan make:helper MyHelper like it says https://github.com/browner12/helpers then added my function. Have I missed something?
So your counter table actually has a field named _id, that is numerical that you're trying to increment by 1?
I don't understand what you're trying to do here
$retval = array_values(['_id']) ;
return $retval;
array_values() returns the values of an array. You're giving it an array of ['_id'], so it returns an array of the values, which is the exact same array as you started with. You then return the array, which will always be literally ['_id']. An array containing a string of _id.
I did say it's a noob question, I want to increment the _id field by one and use the $name variable to find the record, to edit it. then return the new value of _id. I know mongodb is a bit weird because you can add fields as you go along which is totally different to mysql or postgres. In the 'counter' table I have one record '_id' = 100 and 'name' = 'role_id'
According to this https://github.com/jenssegers/laravel-mongodb#query-builder it should work,
<?php
//namespace app\Helpers;
use Jenssegers\Mongodb\Eloquent\Model;
use Illuminate\Support\Facades\DB;
if (!function_exists('get_next')) {
/**
* description
*
* @param
* @return
*/
function get_next( $name )
{
DB::collection('counter')
->where('name', $name )->increment('_id');
$retval = DB::collection('counter')
->where('name', $name)->first();
return $retval;
}
}
must be an error in the namespace, I've no idea what use?
Aha, after much reading the namespace should be:
namespace App\Helpers;
class MyHelper
{
//my function in here
}
When I try this i get the error Call to undefined function App\Admin\Controllers\get_next()
So the question now becomes: My function is in App/Helpers/MyHelper.php how do I override the App\Admin\Controllers ?
@LUDO1960 - Your helpers are part of the global namespace. Your helper file(s) do not need a namespace declaration. In your last post you are trying to include them in a class. None of this will solve the underlying issue of the function not doing what you expect. And, there you were right--you're not calling it correctly because you are passing an array as your argument and then not accessing its value by its index.
// Instead of this:
$db_array = get_next(['name' => 'role_id]);
// Do this:
$db_array = get_next('role_id');
However, if you want/need to pass an array as your argument to get_next(), you have to access it by index in your helper:
// If you call it like this:
$db_array = get_next(['name' => 'role_id']);
// You'll need to access role_id like this:
function get_next( $name )
{
DB::table('counter')
->where('name', $name['role_id'] )->increment('_id');
return DB::collection('counter')->where('name', $name['role_id'])->first();
}
If you don't need to pass an array as an argument, simply passing the value (role_id) to get_next will work with your helper:
// Call like this:
$db_array = get_next('role_id');
// But maybe change your parameter name and do this:
function get_next( $role_id )
{
DB::table('counter')
->where('name', $role_id )->increment('_id');
return DB::collection('counter')->where('name', $role_id)->first();
}
HTH!
Thank you for your detailed answer, it really does help me a lot. Here's what happens:
<?php
use Jenssegers\Mongodb\Eloquent\Model;
use Illuminate\Support\Facades\DB;
function get_next( $role_id )
{
DB::collection('counter')
->where('name', $role_id )->increment('_id');
return DB::collection('counter')
->where('name', $role_id)->first();
}
I get the error:
Performing an update on the path '_id' would modify the immutable field '_id'
Don't like the look of that, can I force it somehow? Has my cunning plan been foiled by MongoDB?
Thanks again for your valued input.
Just found this https://docs.mongodb.com/v3.0/tutorial/create-an-auto-incrementing-field/
@LUDO1960 - TMYK. I personally don't use Mongo sooooo, yes your strategy is foiled, but not your plan. Since you can't create a new _id by incrmenting, why not just do an insert and return the newly inserted id? Like so:
function get_next( $role_id )
{
return DB::collection('counter')
->insertGetId(['name' => $role_id]);
}
@lindstrom Tried your suggestion but could not get it to do what I wanted. However it did get me thinking:
<?php
use Jenssegers\Mongodb\Eloquent\Model;
use Illuminate\Support\Facades\DB;
function get_next( $name )
{
$old_num = DB::collection('counter')
->where( 'name', $name )->pluck('_id');
$new_num = $old_num[0] + 1;
DB::collection('counter')
->where( 'name', $name )->delete();
DB::collection('counter')->insert(
['_id' => $new_num, 'name' => $name ]
);
}
Voila, an auto incrementing mongodb _id field, thanks everybody for getting me over the hurdle. Kept the variable as $name so I can throw other id's at the function not just role_id.
Please or to participate in this conversation.