Incrementing an integer in a controller with Laravel

Active3 hr before
Viewed126 times

4 Answers


I'm not sure how to increment an integer in the controller,Making statements based on opinion; back them up with references or personal experience.,This is the what I tried. I'm grabbing the code, adding one and then saving it. This generates the error: "Call to a member function save() on string" , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

The problem is you are storing the count property, not the object itself.

$count = Count::find(1);

$count - > count += 1;

$count - > save();

return ($count);
load more v

Customer::find($customer_id) - > increment('loyalty_points');

// increment by 20 
Customer::find($customer_id) - > increment('loyalty_points', 20);
load more v

Update Statements Updating JSON Columns Increment & Decrement ,Increment & Decrement,Updating JSON Columns,Insert Statements Upserts

You may use the table method provided by the DB facade to begin a query. The table method returns a fluent query builder instance for the given table, allowing you to chain more constraints onto the query and then finally retrieve the results of the query using the get method:

< ? php

namespace App\ Http\ Controllers;

use App\ Http\ Controllers\ Controller;
use Illuminate\ Support\ Facades\ DB;

class UserController extends Controller {
    * Show a list of all of the application's users.
    * @return \Illuminate\Http\Response
   function index() {
      $users = DB::table('users') - > get();

      return view('user.index', ['users' => $users]);
load more v

Again, isn’t Eloquent awesome?,“` Customer::query() ->where(‘id’, $customer_id) ->update([ ‘loyalty_points’ => DB::raw(‘loyalty_points + 1’) ]); “`,[…] behind the site is that you get one Laravel tip a day. I like the short and sweet format and have already learned a […], DB::table(‘users_history’) ->where(‘term’, ‘=’, $keywords) ->where(‘user_id’, ‘=’, $user_id) ->increment(‘count’);

A straightforward way of doing this is get the row, make the calculation and update the row, like this:

$customer = Customer::find($customer_id);
$loyalty_points = $customer - > loyalty_points + 1;
$customer - > update(['loyalty_points' => $loyalty_points]);
load more v

Other "laravel-integer" queries related to "Incrementing an integer in a controller with Laravel"