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

Jacobs's avatar

Validator throws false errors

Hey, I'm really confused as to what's going on in my project. I have a simple validation, logged in user (in my case comes from the accounts table, has related players on the account), can click on one of his characters (players) on myAccount page to hide it, hiding means a simple boolean hide_char toggle. However before that, I have to validate that the player from request('name') exists and does belong to the logged in user.

Here's the route:

Route::get('myaccount', 'MyAccountController@Index')->name('myaccount');

Route::post('myaccount/visible', 'MyAccountController@Visible')->name('visible');

Here's the controller:

<?php

namespace App\Http\Controllers;

use App\player;

use Illuminate\Support\Facades\Auth;

class MyAccountController extends Controller
{
    public function __construct() {
        $this->middleware('auth');
    }
    public function index()
    {
        return view('myaccount', [
            'account' => Auth::user()->toArray(),
            'characters' => player::with('online')
                ->where('account_id', Auth::user()->id)
                ->get()
                ->toArray()
        ]);
    }
    public function visible(){
        $this->validate(
            request(),
            [
                'name' => 'required|string|in:players|in_array:' . implode(',', Auth::user()->characters->pluck('name')->toArray())
            ],
            [
                '*.in' => 'A character with that name does not exist.',
                '*.in_array' => 'This character doesn\'t belong to your account!'
            ]
        );
//        Auth::user()->toggleHide(request('name'));
        return back();
    }
}

Here's Account Model:

<?php

namespace App;
use App\player;

use Illuminate\Foundation\Auth\User as Authenticatable;

class Account extends Authenticatable
{
    protected $fillable = array(
        'id', 'password', 'name', 'email', 'type', 'premdays', 'lastday', 'creation', 'points', 'cooldown', 'flag'
    );

    protected $with = [
        'characters'
        ];
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function hasAccess($val) {
        return $this->pluck('access')->first() >= $val;
    }
    public function characters() {
        return $this->hasMany(Player::class);
    }
    public function hasCharacter($charName) {
        return $this->characters->contains('name', $charName);
    }
    public function toggleHide($charName)
    {
        $character = $this->characters->where('name', $charName)->first();
        $character->hide_char = !$character->hide_char;
        $character->save();
        return $this;
    }
    public function store(){
    }
    //
}

Here's player model:

<?php

namespace App;

use App\PlayersOnline;

use Illuminate\Database\Eloquent\Model;
class player extends Model
{
    protected $fillable = array(
        'id', 'name', 'group_id', 'account_id', 'level', 'vocation', 'health', 'healthmax', 'experience', 'lookbody', 'lookfeet', 'lookhead', 'looklegs', 'looktype', 'lookaddons', 'maglevel', 'mana', 'manamax', 'manaspent', 'soul', 'town_id', 'posx', 'posy', 'posz', 'conditions', 'cap', 'sex', 'lastlogin', 'lastip', 'save', 'skull', 'skulltime', 'lastlogout', 'blessings', 'onlinetime', 'deletion', 'balance', 'stamina', 'skill_fist', 'skill_fist_tries', 'skill_club', 'skill_club_tries', 'skill_sword', 'skill_sword_tries', 'skill_axe', 'skill_axe_tries', 'skill_dist', 'skill_dist_tries', 'skill_shielding', 'skill_shielding_tries', 'skill_fishing', 'skill_fishing_tries', 'world', 'created_at', 'updated_at'
    );

    public function getVocationAttribute($value) {
        return config('aac.vocations.' . $value);
    }

    public function getGroupIdAttribute($value) {
        return config('aac.ingame_positions.' . $value);
    }
    public function online() {
        return $this->belongsTo(PlayersOnline::class, 'id', 'player_id');
    }
    public function getLastloginAttribute($value) {
        if($value != 0)
            return \Carbon\Carbon::createFromTimestamp($value)->format('d F Y - h:i');
        else return 'Never logged in.';
    }
    public function store(){
    }
    //
}

However, the request throws a false error, while, when dding on visible() method:

dd(, request('name'),implode(',', Auth::user()>characters->pluck('name')->toArray()));

Returns

"Jacobs"
"Jacobs,Jacobs2"

Which, if dd is commented, returns errors:

A character with that name does not exist.
This character doesn't belong to your account!

Any idea what's going on? This is completely wrong as the validator has gone crazy. Also tried cache:clear, route:clear, config:cache, view:clear, dump-autoload

0 likes
3 replies
jsonkenyon's avatar

Change your controller from


namespace App\Http\Controllers;

use App\player;

use Illuminate\Support\Facades\Auth;

class MyAccountController extends Controller
{
    public function __construct() {
        $this->middleware('auth');
    }
    public function index()
    {
        return view('myaccount', [
            'account' => Auth::user()->toArray(),
            'characters' => player::with('online')
                ->where('account_id', Auth::user()->id)
                ->get()
                ->toArray()
        ]);
    }
    public function visible(){
        $this->validate(
            request(),
            [
                'name' => 'required|string|in:players|in_array:' . implode(',', Auth::user()->characters->pluck('name')->toArray())
            ],
            [
                '*.in' => 'A character with that name does not exist.',
                '*.in_array' => 'This character doesn\'t belong to your account!'
            ]
        );
//        Auth::user()->toggleHide(request('name'));
        return back();
    }
}

to

<?php

namespace App\Http\Controllers;

use App\player;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class MyAccountController extends Controller
{
    public function __construct() {
        $this->middleware('auth');
    }
    public function index()
    {
        return view('myaccount', [
            'account' => Auth::user()->toArray(),
            'characters' => player::with('online')
                ->where('account_id', Auth::user()->id)
                ->get()
                ->toArray()
        ]);
    }
    public function visible(Request $request){
        $this->validate(
           $request,
            [
                'name' => 'required|string|in:players|in_array:' . implode(',', Auth::user()->characters->pluck('name')->toArray())
            ],
            $messages = [
                '*.in' => 'A character with that name does not exist.',
                '*.in_array' => 'This character doesn\'t belong to your account!'
            ]
        );
//        Auth::user()->toggleHide(request('name'));
        return back();
    }
}

Jacobs's avatar

Issue solved, in_array should've been in, in should've been exists.

1 like

Please or to participate in this conversation.