alex wang
alex wang

Reputation: 25

passing data through controller via return view in Laravel 11

I am currently trying to learn Eloquent ORM but first I am trying to pass some static data to my Controller.

Here is the snippet of my Controller

SCControler.php

<?php

namespace App\Http\Controllers;

use App\Models\SCModel;
use Illuminate\Http\Request;

class SCControler extends Controller
{
    //
    public function displaySC(Request $request)
    {
        $secques = SCModel::all();
        return view('auth.user_settings', [
            'myname' => 'alexis',
        ]);
    }
}

in the code above, as you can see I am now trying to fetch the data through my model SCModel:all(); however, in the page of user_settings it says not defined altho the table now have data. So I've tried debugging it if it can pass a static data, so as you can see in the return view.

Unfortunately, it still shows an error but now it says, Call to undefined method ::displaySC()

Here is my SCModel as well, I have made sure that the $table and the $fillable strings there inside the array matches in the table.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SCModel extends Model
{
    use HasFactory;

    protected $table = "sc_tbl";
    public $timestamps = false;
    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'sc_id',
        'sc_desc',
    ];
}

I will also be posting my routes in my web.php file. So whenever I try to redirect directly to the user_settings I can access it so I think this route is working.

Route::get('/user_settings', [SCModel::class, 'displaySC'])->name('auth.user_settings');

I would greatly appreciate any help that you could provide to my mistakes.

just to give context, this is just purely for studying so if think the database or data-related it's okay to delete/drop it to my db.

Upvotes: 0

Views: 628

Answers (1)

geertjanknapen
geertjanknapen

Reputation: 1447

You are using the wrong class in your route. You are trying to call displaySC on your model, whilst you should be calling it on your controller instead.

So change this

Route::get('/user_settings', [SCModel::class, 'displaySC'])->name('auth.user_settings');

To this

Route::get('/user_settings', [SCControler::class, 'displaySC'])->name('auth.user_settings');

You will probably also have to change a using at the top of your routes file.

Upvotes: 0

Related Questions