ludo1960 Posted April 2, 2019 Share Posted April 2, 2019 Hi guys, Yet another rookie question, trying to implement this: I have a function: <?php //MyHelper.php function getnext( $name ) { $conn = DB::table('counter')->where('name', $name )->first(); $result = $conn->findAndModify( ['name' => $name], ['_id' => ['seq' => 1]], ['seq' => true], ['new' => true, 'upsert' => true] ); return $result['seq']; } ?> And I am calling it so: $db_array = getnext(array('name' => 'role_id')); print_r( $db_array); Results in call to a member function on NULL, not sure where I'm going wrong! It is making a connection and the counter table has a 'role_id' in it. Can anyone tell me where I'm going wrong? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 2, 2019 Share Posted April 2, 2019 You do realize that you are showing us virtually nothing? The calls to what seem to be methods of some undefined class cannot be interpreted by us since we have no idea what code we are attempting to access with those calls. Even the structure of the calls is confusing without knowing the underlying structure of the class' methods. I assume the class is named 'DB' but I don't know how one creates and uses a line that references somethings called "table", "where" and "first" all in one statement, if that is even what we are supposed to think is happening here. Quote Link to comment Share on other sites More sharing options...
ludo1960 Posted April 2, 2019 Author Share Posted April 2, 2019 Hands up!! So goes the learning curve, maybe I should start at the beginning. I am trying (not very successfully) to create an increment function in the Laravel framework for a mongodb database. _id column . Just to confuse me even more not all of mongoDB functions are available to me as I am using this package. As I understand it mongoDB does not have an native increment field, so my cunning plan was to create a collection and store the incremented values for each $name. So that for example I have a collection named 'counter' and the records are _id = 100 and name = 'some_id' I want to call a function to increment the value and return the current value of the _id i.e. 101. Sounds simple, but too many things to take into consideration for a rookie. If you help me out, I promise never to laugh at your glorious President ever again, but only if you stop laughing at the joke that is Westminster. Is it a deal? PS I'm Scottish, so don't care what's going on in London, Angerland as we call it! Quote Link to comment Share on other sites More sharing options...
ludo1960 Posted April 2, 2019 Author Share Posted April 2, 2019 My latest effort: <?php //MyHelper.php function get_next( $name ) { DB::collection('counter') ->where('name', $name )->increment('_id'); return array_values(['_id']) ; } ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 2, 2019 Share Posted April 2, 2019 I stand behind my previous post. Your code makes no sense to us. For example - what does this SINGLE line do for you? $conn = DB::table('counter')->where('name', $name )->first(); It doesn't appear to be calling any function unless you have a class named DB that has a method named 'table'. And if it does - what exactly are the arguments that you are providing in the call to that method? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 2, 2019 Share Posted April 2, 2019 And your most recent post makes it even more unclear... Quote Link to comment Share on other sites More sharing options...
ludo1960 Posted April 2, 2019 Author Share Posted April 2, 2019 (edited) Ok, if you look here that single line should give access to the underlying table data in laravel. Then if you look here and scroll down to the "Incrementing or decrementing a value of a column" it shows how to increment a value in a database field, and the text editor here wont let me switch off bold type, not my day I think. I thought it was clear what I am trying to achieve, a global function to take care of increments. Edited April 2, 2019 by ludo1960 Quote Link to comment Share on other sites More sharing options...
ludo1960 Posted April 2, 2019 Author Share Posted April 2, 2019 After more digging, i've installed a function helper So to follow your comments I think the classes I need are like this: <?php //namespace app; use Jenssegers\Mongodb\Eloquent\Model; use Illuminate\Support\Facades\DB; if (!function_exists('get_next')) { /** * description * * @param * @return */ function get_next( $name ) { DB::table('counter') ->where('name', $name )->increment('_id'); $retval = array_values(['_id']) ; return $retval; } } The methods I need should be in the classes listed above. Not sure what namespace I need, or if I even need one . Excuse my lack of knowledge, I'm just learning this stuff. Quote Link to comment Share on other sites More sharing options...
kicken Posted April 2, 2019 Share Posted April 2, 2019 Your problem is likely with how you're defining your $conn variable. If you read the code you have and think about it: $conn = DB::table('counter')->where('name', $name )->first(); That says, roughly 'In the table counter, find rows where name=$name, and give me the first row' So $conn is going to contain a row of data where as you seem to be expecting it to be a database connection. Seeing DB::table() makes me assume you are using laravel / eloquent which I'm not familiar with so I can't really provide you the right code. You'll have to do some looking around to find out how to get the connection or whatever you need for your findAndModify method. Might just be the table that you need, in which case $conn = DB::table('counter') Quote Link to comment Share on other sites More sharing options...
ludo1960 Posted April 3, 2019 Author Share Posted April 3, 2019 Got it working, <?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, auto incrementing _id field for mongoDB, Thanks for the pointers guys, got a much better understanding of how php works in the laravel framework Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.