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

m4martie's avatar

Undefined URL value

Hello, I'm developing a project, which I stumbled upon to an error that every time view button of my page I come up with this error

http://localhost/system/public/my_classes/view/add-student/undefined

And I don't know how to fix this problem can someone help me, I don't think there's a problem to my Controller, Blade or Routing. I'm confused. Help me.

0 likes
6 replies
Cronix's avatar

Look at however/wherever you are generating that url in your view or javascript. I'm thinking this is from javascript since php would generally give you an error, but javascript will return "undefined" for something that doesn't exist. Are you using js to build these urls?

m4martie's avatar

Controller

public function add_student(MyClass $my_class)
    {

        $role = Role::where('name', 'shs-student')->first();
        $students = $role->users;

        $users = User::withTrashed()->get();

        return view('my_class.add_student', compact('my_class', 'students', 'users'));
    }
public function list(MyClass $my_class)
        {

            // print_r($my_class);
            // die();
            
            return response()->json($my_class);
        }

Route

Route::get('view/add-student/{my_class}','StudentController@add_student')->name('my_class.add_student');
Route::get('list', 'MyClassController@list')->name('my_class.list');

Blade

<a :href="'add-student/' + my_class.id" class="ui mini green icon button"><i class="plus icon"></i>

Vue.js

getMy_Class(){
            axios.get('{{ route('my_class.list', $my_class->id) }}')
            .then(response => {
                this.my_class = response.data;
            })
            .catch(error => {
                console.log(error.response.data);
            });
        },
m4martie's avatar

@CRONIX - yeah, I've been using vue.js for this, and somewhat thinking to change it to pure php

m4martie's avatar

@TYKUS - yeah, probably it's because of the js, can you suggest a better way for this thing, I'm just willing to learn someways since then

Snapey's avatar

You also have to ask yourself, how come I can see public in my URLs

Snapey's avatar

So in this

<a :href="'add-student/' + my_class.id" 

my_class.id is undefined so you end up with undefined in your URL

Please or to participate in this conversation.