0

I'm just starting using laravel and there's a problem when i'm using models.

I already make a dummy data inside model in profilesModel.php

private static $pData = [
    "pNamaLengkap" => "Anro Anderson",
    "pJobDescription" => "Full Stack Developer",
    "pUnitKerja" => "Talent Management",
    "pDirektorat" => "Human Resource"
];

public static function all()
{
    return self::$pData;
}

I already tried calling the model using web.php

use App\Models\profilesModel;

Route::get('/profiles', function () {
    return view('profiles', [
        "profiles" => profilesModel::all()
    ]);
});

and the data is recognize and become an array, here's the prove enter image description here

But from the blade.php didn't get the data and become a Undefined variable enter image description here

When I'm using this code in web.php instead of the model it worked perfectly fine

Route::get('/profiles', function () {
    return view('profiles', [
        "pNamaLengkap" => "Anro Anderson",
        "pJobDescription" => "Full Stack Developer",
        "pUnitKerja" => "Talent Management",
        "pDirektorat" => "Human Resource"
    ]);
});

But I'm stil wanted to use the model, please help Thank you

Constantine
  • 650
  • 9
  • 15
  • 3
    Don't make dummy data like that. If you want dummy data just add some data in your database and make a proper eloquent model to pass to the view – apokryfos Nov 09 '21 at 07:35
  • You don't have a variable named `$pNamaLengkap` but you do have a variable with that value key `$profiles['pNamaLengkap']`. But please, don't do that. As @apokryfos mentioned, use a proper eloquent model and seed some data to your database. – Clément Baconnier Nov 09 '21 at 07:56
  • can you make an easy example of using eloquent model with this problem, i don't quite understand how to use it. I'll try it now – daniel leander Nov 09 '21 at 08:24
  • Why aren't you using a controller? Queries don't belong in the route file – Gert B. Nov 09 '21 at 08:57
  • i already try using controller, the result is the same – daniel leander Nov 09 '21 at 10:02
  • When you do `return view('profile', [ 'profiles' => $profiles ]);` in your code that means your view will receive a variable called `$profiles` which will have as a value the value that variable `$profiles` had in your route handler. If you pass more entries in this array you get more variables available in your view accessible with names that correspond to the keys of the array. In short in your first approach you'd need to use `$profile['pNamaLengkap']` and if you had a real model you'd use `$profile->pNamaLengkap` – apokryfos Nov 09 '21 at 11:07

0 Answers0