Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Troj's avatar
Level 4

Data too long for column 'payload' at row 1

I'm showing the recently viewed deals on the homepage to my visitors. This only works for one viewed item, after that the following QueryExeption occurs:

Illuminate\Database\QueryException
SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'payload' at row 1 (SQL: update `sessions` set `payload` =.....

I took this from the docs - Driver Prerequisites

My first thought was to change the column 'payload' to a varchar or longtext field. But there must be a reason why it's not used like that in the docs, right.

What should i do to fix this?

  • Store more specific data to the session
  • Or change the fieldtype "payload" in the database
  • Or something else

This is my sessions table:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSessionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('sessions', function (Blueprint $table) {
            $table->string('id')->primary();
            $table->foreignId('user_id')->nullable()->index();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->text('payload');
            $table->integer('last_activity')->index();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('sessions');
    }
}

in my DealsController this is how i store info to the session. It returns a massive array.

<?php

namespace App\Http\Controllers;

use App\Models\Deal;
use App\Filters\DealFilters;
use App\Models\Category;
use App\Models\City;
use App\Models\Address;
use Carbon\Carbon;
use Illuminate\Support\Str;
use Illuminate\Http\Request;

class DealController
{

    public function show(Request $request, $countrySlug, $citySlug, Category $category, Address $address, Deal $deal)
    {
        $city = City::where('country_slug', $countrySlug)->where('slug', $citySlug)->firstOrFail();

        $dealIds = collect(session()->get('deals', []))->pluck('id');

        if ($dealIds->contains($deal->getKey()) && $index = $dealIds->search($deal->getKey())) {
            session()->pull("deals.{$index}");
        }

        session()->push('deals', $deal);

        $mediaItems = $deal->getMedia('multi_collection');
        $imagesPerChunk = count($mediaItems);

        return view('deals.show', [
            'city' => $city,
            'category' => $category,
            'address' => $address,
            'deal' => $deal,
            'mediaItems' => $mediaItems,
            'imagesPerChunk' => $imagesPerChunk
        ]);

    }

}
0 likes
4 replies
Tray2's avatar

The text fields can handle 64Kb which is 65,536 bytes. If that is too little try changing it to longtext.

Troj's avatar
Level 4

@tray2 that worked indeed but i think there's something else going on, and i probable should make the collection more specific. When i look at debugbar it's a massive query with all the content from all columns, in the deals table, including content from the text field.

This is just a really small part of it:

select * from `deals` where `deals`.`id` in ('{\"id\":2,\"title\":\"Dolor alias accusantium et consequatur praesentium consequuntur autem.\",\"slug\":\"dolor-alias-accusantium-et-consequatur-praesentium-consequuntur-autem-2\",\"category_id\":2,\"address_id\":null,\"text\":\"When a request comes in your app will return a response. To create that response, your application has to do some work. ...

Could you give me an example on how to specify that collection. Here is the dump and i need some info from the attributes only:

array:4 [▼
  "_token" => "AWYN34rFERVEGyqRCC8GYg1peC4jfgevRG4vjnn49gTHEh"
  "_previous" => array:1 [▶]
  "_flash" => array:2 [▶]
  "deals" => array:11 [▼
    0 => App\Models\Deal {#733 ▼
      #guarded: []
      #fillable: array:14 [▶]
      +dates: array:5 [▶]
      +casts: array:9 [▶]
      #connection: "mysql"
      #table: "deals"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:36 [▼
        "id" => 2
        "title" => "Dolor alias accusantium et consequatur praesentium consequuntur autem."
        "slug" => "dolor-alias-accusantium-et-consequatur-praesentium-consequuntur-autem-2"
        "submitted_by_user_id" => 1
        "category_id" => 2
        "address_id" => null
        "text" => """
          When a request comes in your app will return a response. To create that response, your application has 
         to do some work. Most likely queries will execute. This a ..I removed the rest of the content because it 
         was sooooo long
          """
        "price" => "50.00"
        "subtitle" => null
        "whats_included" => null
        "highlights" => null
        "wheelchair_access" => 0
        "smartphone_ticket" => 0
        "starting_time" => null
        "duration" => null
        "external_url" => "https://www.example.com"
        "preview_secret" => "sefver$v6b@FF"
        "visits" => 0
        "publish_date" => "2021-01-17 11:47:50"
        "starts_at" => null
        "ends_at" => null
        "send_automated_tweet" => 0
        "tweet_sent" => 0
        "tweet_url" => "https://twitter.com/TwitterAPI/status/324530482458723456"
        "author_twitter_handle" => null
        "author" => "IamTheauthor"
        "created_at" => "2021-01-17 11:47:49"
        "updated_at" => "2021-01-17 11:52:53"
        "city_id" => 2
      ]
      #original: array:36 [▶]
      #changes: []
      #classCastCache: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:5 [▶]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      +mediaConversions: []
      +mediaCollections: []
      #deletePreservingMedia: false
      #unAttachedMediaLibraryItems: []
      #queuedTags: []
    }
    1 => App\Models\Deal {#764 ▶}
Tray2's avatar

Not sure what it is you want to do here at all.

Hussam3bd's avatar

I had the same issue, I tried all the above solutions and nothing worked for me.

My case: The token keeps changing on every request only when the session driver is set to database, and it works just fine on file and Redis driver.

After a lot of debugging, I found that the issue was not with session config, it was from the payload column in the session table in the DB.

I changed the payload column from text to long text, and it worked!

1 like

Please or to participate in this conversation.