Route::get() function on laravel not returning proper value

buddies! How you all doing?

So, I've a little problem here. I started working with Laravel to create a simple system (maybe not that simple) and I've found a error that I can not fix it.

In the web.php:

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function() {
    return View::make('pages.workers');
});
Route::get('vacations', function() {
    return View::make('pages.vacations');
});

Route::get('/', 'App\Http\Controllers\WorkersController@index');
Route::get('/{id}', 'App\Http\Controllers\WorkersController@getWorker');
Route::get('/vacations', 'App\Http\Controllers\VacationsController@index');
Route::get('/vacations/{id}', 'App\Http\Controllers\VacationsController@detailVacations');

Route::post('/', 'App\Http\Controllers\WorkersController@addWorker');
Route::post('/vacations', 'App\Http\Controllers\VacationsController@addVacations');

Now I've two controllers: WorkersController.php and VacationsController.php, the code of the same are, respectively:

WorkersController:

<?php
    namespace App\Http\Controllers;
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use App\Workers;

    class WorkersController extends Controller {
        public function index() {
            $workers['workers'] = Workers::all();
            return view('workers', $workers);
        }

        public function addWorker(Request $req) {
            if($req->has('nome')) {
                $nome = $req->input('nome');
                $sobrenome = $req->input('sobrenome');
                $telefone = $req->input('telefone');
                $entrada = $req->input('entrada');
                $saida = $req->input('saida');
                $workers = new Workers();
                $workers->nome = $nome;
                $workers->sobrenome = $sobrenome;
                $workers->telefone = $telefone;
                $workers->entrada = $entrada;
                $workers->saida = $saida;
                $workers->save();
                echo '<script>alert("Funcionário inserido com sucesso!")</script>';
                echo '<script>location.href="'.BASE_URL.'"</script>';
                die();
            }
        }

        public function getWorker($id) {
            $info = Workers::find($id);
            $data['nome'] = $info['nome'];
            $data['sobrenome'] = $info['sobrenome'];
            $data['telefone'] = $info['telefone'];
            $data['entrada'] = $info['entrada'];
            $data['saida'] = $info['saida'];
            return view('worker_single',$data);
        }
    }
?>

VacationsController:

<?php
    namespace App\Http\Controllers;
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use App\Vacations;

    class VacationsController extends Controller {
        public function index() {
            $vacations['vacations'] = Vacations::all();
            return view('vacations', $vacations);
        }

        public function addVacations(Request $req_v) {
            if($req_v->has('nome')) {
                $nome = $req_v->input('nome');
                $sobrenome = $req_v->input('sobrenome');
                $saida_ferias = $req_v->input('saida');
                $volta = $req_v->input('volta');
                $vacations = new Vacations;
                $vacations->nome = $nome;
                $vacations->sobrenome = $sobrenome;
                $vacations->saida = $saida_ferias;
                $vacations->volta = $volta;
                $vacations->save();
                echo '<script>alert("Férias inseridas com sucesso para o funcionário(a)!")</script>';
                echo '<script>location.href="'.VACATIONS.'"</script>';
                die();
            }
        }

        public function detailVacations($id) {
            $info_v = Vacations::find($id);
            $vacation_detail['nome'] = $info_v['nome'];
            $vacation_detail['sobrenome'] = $info_v['sobrenome'];
            $vacation_detail['entrada'] = $info_v['entrada'];
            $vacation_detail['volta'] = $info_v['volta'];
            return view('vacation_single',$vacation_detail);
        }
    }
?>

So, I got a workers.blade.php that lists all workers and a workers_single.blade.php that gets the id from a table, creates a link and displays the information about the worker. Besides that I have a vacations.blade.php and a vacations_single.blade.php, the first one is exactly like workers.blade.php and the second shows the information about the workers vacations following the same principle of workers_single.

The problem is that it doesn't create a url for the vacations_single, it returns for the workers_single with the following error: Trying to access array offset on value of type null

Any ideas what could it be?

Workers.php

<?php
    namespace App;
    use Illuminate\Database\Eloquent\Model;

    class Workers extends Model {
        protected $table = 'workers';
        public $timestamps = false;
    }
?>

Vacations.php

<?php
    namespace App;
    use Illuminate\Database\Eloquent\Model;

    class Vacations extends Model {
        protected $table = 'vacations';
        public $timestamps = false;
    }
?>

2 answers

  • answered 2021-05-14 17:42 Dleep

    The problem lies in your routes.

    First, you have multiple definitions for the routes '/' and '/vacations'. Only the first will match on each case.

    Then, if you remove the routes that match with views instead of controllers, you'll still have this problem:

    Route::get('/{id}', 'App\Http\Controllers\WorkersController@getWorker');
    Route::get('/vacations', 'App\Http\Controllers\VacationsController@index');
    

    If you try to access http://your-site/vacations, it'll tie that URL to the first route that matches. Which in this case is, in fact, the first one, WorkersController@getWorker, with the parameter $id set to 'vacations'

    You have to map your routes so that there're no clashes. You could make all worker routes have a 'workers' prefix, or, if your worker IDs are integers, make the getWorker route match with only integers, like this:

    Route::get('/{id}', 'App\Http\Controllers\WorkersController@getWorker')->where('id', '[0-9]+');
    

  • answered 2021-05-14 17:49 Niklesh Raut

    Never use routes like get('/{id}') It would conflict with other url like get('vacations') Instead use get('/workers/{id}') to fix this problem