jeudi 25 juillet 2019

Return total rows of multiple models in one controller

sorry for asking sort of easy question but I'd like to clear the confusion of mine. Have been searching the net but mostly works with static models. I am currently trying to find a better way to execute these as the models will be dynamic (new database to work with each week or so) and I just thought there would be a better way instead or iterating the classes. I am new with this framework and really hope if you could spill your ideas to show the better way other than my code below.
I would say raw query is somehow easier but I'd like to use eloquent instead.

use App\lifting as data1;
use App\test1 as data2;
use App\test2 as data3;

class dataController extends Controller
{
    public function index()
    {
        $datas = array(data2::count(), data1::count(), data3::count());

        return $datas;
    }
}

Sure it's working and I know it's quite beginner level but you get the idea...

Thanks.

[EDIT]

When I say better way I mean like keeping the models in one collection and just use foreach something like that to get the total rows of each table.

Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire