Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
    }

>Solution :

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading