Incrementing an integer in a controller with Laravel

Related searches

I'm not sure how to increment an integer in the controller

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"

I'm returning the count to see the results in the browser. Running $count = Count::find(1)->count in Tinker gives the correct amount.

public function update(Request $request, Count $count)
{
    $count = Count::find(1)->count;

    $addOne = $count + 1;

    $count->save();

    return ($count);
}

Could someone show me how this is not working and what I can do to fix this?

This is the migration:

 public function up()
  {
     Schema::create('counts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('count');
        $table->timestamps();
     });
 }

This is the Modal:

  <?php

  namespace App;

  use Illuminate\Database\Eloquent\Model;

  class Count extends Model
  {
      protected $fillable = [
        'count'
      ];
  }

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

$count = Count::find(1);

$count->count += 1;

$count->save();

return ($count);

Should do the trick.

Some more defined naming might help as well. Had to do some mental gymnastics to wrap my head around what count I was counting.

Eloquent - a better way to increment an int, Hi, This is my code to get the current value of my int and increment by 1. Is there a better Is this maybe depreciated or a Laravel 5 method? But this works Laravel has a great database migration mechanism, but there are some things missing there. For example, what if you want your IDs start with some number, like 140001, 140002 etc? By default, we have a function increments() to set auto_increment:

The accepted answer is good, but this could be achieved even easier with the helper method that Laravel provides.

so this code:

$count = Count::find(1);
$count->increment('count');

return $count;

will do the same.

How to increment a value in the database (SOLVED), It will return an int, not the query builder object. You have to do it like this: $query = \App\User::where� This is probably a simple thing, but I don't know how to declare and increment an integer variable in a view in Laravel. I have a couple foreach loops that I am using:

You can do

  $count = Count::find(1);
  $count->count += 1;
  $count->save();
  return $count;

How to increment or decrement a column value in Laravel?, In above example we get the row first then add calculation and finally update row. But Laravel made this task very easy with increment and decrement function. There is nothing in the migration you've shown that would cause LoginID to be an auto incrementing primary key. – patricus Apr 2 '16 at 4:36 I don't know the cause, but it worked a few hours later. – ILoveBaymax Apr 2 '16 at 15:27

laravel add auto increment Code Example, Get code examples like "laravel add auto increment" instantly right from your google search results with the Grepper Chrome Extension. Use two columns auto increment ( laravel 4.1) 5. Laravel - update is incrementing the ID? 0. Laravel 5.2 Eloquent primary key as a multiple key. 2.

Database: Query Builder - Laravel, Updating JSON Columns; Increment & Decrement. Deletes <?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use If you are adding a huge array of integer bindings to your query, the whereIntegerInRaw or� Controllers can group related request handling logic into a single class. Controllers are stored in the app/Http/Controllers directory. Basic Controllers. Defining Controllers. Below is an example of a basic controller class. Note that the controller extends the base controller class included with Laravel.

Set INTEGER columns as auto-increment (primary key)->charset('utf8mb4') Specify a character set for the column (MySQL)->collation('utf8mb4_unicode_ci') Specify a collation for the column (MySQL/PostgreSQL/SQL Server)->comment('my comment') Add a comment to a column (MySQL/PostgreSQL)->default($value) Specify a "default" value for the column->first()

Comments
  • why not simply use $count++?