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

anon160124's avatar

LARAVEL-VOYAGER

Hi everybody... I use voyager as an admin dashboard. The point is that if I try to make an update and I am logged in as an admin with all rights, it will redirect me to the homepage and role_id will be deleted from the database. Changes will be saved .... This will only happen if the admin wants to edit his data. This does not happen when editing other users. Can you help me? Thank you

Voyager/UsersController.php

namespace App\Http\Controllers\Voyager;

use Illuminate\Http\Request;
use TCG\Voyager\Facades\Voyager;
use Illuminate\Database\Eloquent\Model;
use TCG\Voyager\Events\BreadDataUpdated;
use TCG\Voyager\Http\Controllers\VoyagerBaseController;
use TCG\Voyager\Http\Controllers\Traits\BreadRelationshipParser;

class UsersController extends VoyagerBaseController
{
	use BreadRelationshipParser;

	public function update(Request $request, $id)
	{
		$slug = $this->getSlug($request);

		$dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();

		// Compatibility with Model binding.
		$id = $id instanceof Model ? $id->{$id->getKeyName()} : $id;

		$data = call_user_func([$dataType->model_name, 'findOrFail'], $id);

		// Check permission
		$this->authorize('edit', $data);

		if (!auth()->user()->hasPermission('edit_roles')) {

			unset($request['role_id']);

			return back()->with([
				'message' => '....ERROR....',
				'alert-type' => 'error',
			]);
		}

		// Validate fields with ajax
		$val = $this->validateBread($request->all(), $dataType->editRows, $slug, $id);

		if ($val->fails()) {
			return response()->json(['errors' => $val->messages()]);
		}

		if (!$request->ajax()) {
			$this->insertUpdateData($request, $slug, $dataType->editRows, $data);

			event(new BreadDataUpdated($dataType, $data));

			return redirect()
				->route("voyager.{$dataType->slug}.index")
				->with([
					'message' => __('successfully_updated') . " {$dataType->display_name_singular}",
					'alert-type' => 'success',
				]);
		}
	}
}
0 likes
0 replies

Please or to participate in this conversation.