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

GodziLaravel's avatar

[***important update***] Why Tinker result is different than controller result ?

Hello .

Important

I found something very important : I dont think that this problem is related to the DB or model because

Route::get('tst4', function () {
return 'http://test https://';
}) ;

returns : //test //

Laravel 5.8 PHP 7.2 PostgeSQL 10.10

Here the result from Tinker :

>>> App\training::find(7);
=> App\training {#3142
     id: 7,
     title: "training https: test after adding the permission: https",
     begin_date: "2019-11-05 00:00:00",
     end_date: "2019-11-05 00:00:00",
...
   }

From Controller:

{
  "id": 7,
  "title": "training  test after adding the permission: https",
  "begin_date": "2019-11-05 00:00:00",
  "end_date": "2019-11-05 00:00:00",
  "created_at": "2019-11-05 15:19:35",
...

As you can see in Tinker result the field title contains the string https: unlike in the result from the controller.

This problem is also with the word http:

I dont know if it's a kind of security in Laravel to prevent links maybe, but how to solve this ?

Training controller :

    public function show($id)
    {
        $training = Training::with([
            'trainers', 'categories', 'participants'
        ])->findOrFail($id);


        return $training;
    }

Training model :


    protected $casts = [
        'external_urls' => 'object'
    ];
    protected $appends = ["levelName"];

    public function getLevelNameAttribute()
    {
        if ($this->level == null) return null;
        return level::find($this->level_id)->name;
    }

    public function categories()
    {
        return $this->belongsToMany(category::class);
    }

    public function level()
    {
        return $this->belongsTo('App\level');
    }

    public function participants()
    {
        return $this->belongsToMany(User::class, 'participants_training')
            ->select([DB::raw("CONCAT(last_name,' ', first_name) AS name"),
            'last_name', 'first_name'
            ]);
    }

    public function trainers()
    {
        return $this->belongsToMany(User::class, 'trainers_training')
            ->select([
                DB::raw("CONCAT(last_name,' ', first_name) AS name"),
                'last_name', 'first_name'
            ]);

    }
}

Thanks

0 likes
79 replies
Jaytee's avatar

That's interesting. I'm not sure what could be causing this to be honest. If Laravel is responsible for this, then it's certainly a bug, as it would seem it's only removing the first result, and not all results, because you still have a https in the string

training  test after adding the permission: https

But, I also don't see why Laravel would do this, as it would cause issues for users who store full urls in the database.

I had a quick look at the tinker, and response source code, and couldn't find any indication of it stripping protocols.

1 like
GodziLaravel's avatar

@jaytee thanks

In local env ( PHP7.3.6 postgreSQL 10.9) I dont have this problem !

1 like
Talinon's avatar

@mostafalaravel

Are you sure that your app environment is running under the same as tinker? Maybe it's using a test environment and therefore using a test database.

What does the record show when you look at it using a 3rd party database query?

1 like
GodziLaravel's avatar

@talinon Yes sure about that , and here the field content when I use phpPgAdmin

training https: test after adding the permission: https
1 like
Jaytee's avatar

What happens if you return it as an array in the controller instead?

return $training->toArray();
Talinon's avatar

So, the result from Tinker is correct, but the one from your app/controller is not, right?

What happens if you dd($training) in your controller? That at least would rule out accessors and appending.

Do you have a brower plugin that parses JSON? Maybe it's on the fritz and needs to be disabled.

1 like
Jaytee's avatar

Yeah, I'm with @talinon on this one, if you have a browser extension, then that's likely the cause, due to them providing the ability to click through to links.

The best option to find out, again is what @talinon said, use dd($training->title) in the controller and see what the result is.

1 like
GodziLaravel's avatar

@talinon

Here the result : it exists!

  #casts: array:1 [▶]
  #appends: array:1 [▶]
  #connection: "pgsql"
  #table: "trainings"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:13 [▼
    "id" => 7
    "title" => "training https: test after adding the permission: https"
    "begin_date" => "2019-11-05 00:00:00"
    "end_date" => "2019-11-05 00:00:00"
    "created_at" => "2019-11-05 15:19:35"
    "updated_at" => "2019-11-06 12:20:35"
    "level_id" => 1
    "certified" => true
    "external_urls" => "[{"url": "https: www - http: - https:", "name": "https:"}]"
    "certification_number" => "AZD557AZD7"
    "certification_expired_date" => "2019-07-13"
    "intern" => true
    "body_name" => null
  ]
  #original: array:13 [▶]
  #changes: []
  #dates: []
  #dateFormat: null
  #dispatchesEvents: []
  #observables: []
  #relations: array:3 [▶]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▶]
1 like
GodziLaravel's avatar

@jaytee

here the result again not exists :

id": 7,
  "title": "training  test after adding the permission: https",
  "begin_date": "2019-11-05 00:00:00",
  "end_date": "2019-11-05 00:00:00",
  "created_at": "2019-11-05 15:19:35",
  "updated_at": "2019-11-06 12:20:35",
1 like
Talinon's avatar

@JeffreyWay - I've noticed the badges on forum threads are displaying inconsistently.

1 like
Jaytee's avatar

Here's an interesting, but promising solution to try, to figure out if it's the response causing this, or some browser error.

use dd(strlen($training->toArray()['title'])) in the controller and strlen($training->title) in tinker. See if the count is the same.

1 like
Jaytee's avatar

@talinon Yeah me too. I submitted this bug through the contact form RE: badges

No response yet.

1 like
Jaytee's avatar

Give the strlen a go, in my previous comment above.

1 like
Talinon's avatar

@mostafalaravel

Where is external_urls coming from? It almost seems like you have something mutating the attributes. Does your Training model extend from another class?

In your dd() results, compare the original array with the attributes array and see if the title is different?

1 like
GodziLaravel's avatar

@jaytee

with dd(strlen($training->toArray()['title'])); =>53

with tinker >>> strlen(App\training::find(7)->title); also 53

1 like
Jaytee's avatar

Okay. So definitely seems to be a display issue.

return a view and pass the training object through. Then output the title into a <h1> tag or something. See what the result is.

1 like
Talinon's avatar

Did you compare the original array to the attributes array?

Or if it's easier:

dump($training->getOriginal('title'));
dd($training->title);
1 like
GodziLaravel's avatar

@talinon

"training ftp: test aftehttps:r adding the permission:"
"training ftp: test aftehttps:r adding the permission:"

I could see https:

@jaytee I dont use view in my app , I consume only API in VueJs

1 like
Talinon's avatar

@mostafalaravel

Open up your browser developer tools and check the network tab for the API response. What does it show?

1 like
Talinon's avatar

And did that come from the raw response or the "preview"? Make sure you're looking at the raw data.

1 like
GodziLaravel's avatar

@talinon

And did that come from the raw response or the "preview"?

Do you want me to run Raw request ?

1 like
GodziLaravel's avatar

@talinon If I upgrade to php 7.3 do you think it could solve the situation ?

because in my local computer I have php 7.3 and PostgreSQL 10.9 and I dont have this problem !

1 like
Talinon's avatar

@mostafalaravel

I was referring to the response in the browser. For example, in Chrome:

Ctrl + Shift + J (Cmd + Option + J on Mac)

Select Network Tab

Run your app and click on the API response

You'll then have an option to inspect the response with the sub-tabs "Response" or "Preview". I suggested to use the Response tab because it won't (shouldn't) have any interference with any possible browser display issue.

I don't think upgrading PHP will solve the problem.

What happens if you simply dd('training https: test after adding the permission: https') from the controller? If that is altered in your browser then it is without doubt a display issue within your browser.

If the above displays correctly, I would then proceed with commenting out everything from your Model, make sure its only extending Illuminate\Database\Eloquent\Model; and see if that resolves it. If so, work backwards restoring the model until you find what is causing it.

Failing all that, I really have no idea, because it makes no sense.

1 like
GodziLaravel's avatar

@talinon

the : dd('training https: test after adding the permission: https') returns :

training https: test after adding the permission: https

it's not a browser problem because I tried this in different browsers and OS

also if it was about the model , why it works on local (it's the same app !)

could be the GIT problem ?

thanks

1 like
Jaytee's avatar

No because GIT doesn't store the data in your database. Is this application in production?

Since I've opened an issue for this on Github, could you be more specific on a few things for me please:

  1. Could you show me the migration file
  2. What exact Laravel version are you on? 5.8.X
  3. What are the server specifications?
  4. If this is in production, could you provide a URL to the website?
  5. You stated this is in use with an SPA. Could you show the JavaScript code that handles this response?
1 like
Next

Please or to participate in this conversation.