Home > Net >  Calling a get all function om my controller from the model returns empty. Laravel 5.8
Calling a get all function om my controller from the model returns empty. Laravel 5.8

Time:11-26

I'm trying to fetch all rows of a table from my db in a model function. Then i'm trying to call that function from my controller.

It returns an empty array. If i put the model's code directly in my controller it works, i get all rows and data in json format.

Why when i'm calling the function from my controller it returns empty?

Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;


class circuits extends Model
{
    protected $fillable = [
        'circuitId', 'circuitRef', 'name',
        'location', 'country', 'lat',
        'lng', 'alt', 'url',
    ];

    public function races()
    {
        return $this->hasMany('App\races', 'circuitId');
    }

    public function allCircuits(){
        $data = Circuits::all();
        return response()->json($data);
    }
}

Controller

    public function index()
    {
        $data = new circuits;
        $data->allCircuits();
        echo ($data);
    }

CodePudding user response:

you have to save the return value to some variable and echo that try:

public function index()
{
    $data = new circuits;
    $allCircuits = $data->allCircuits();
    echo ($allCircuits);
}
  • Related