简体   繁体   中英

How to use laravel Eloquent model in helper class

How do I use a laravel Eloquent model outside of a controller? I cannot seem to find out what use "*insert class here*" statement I should use. I have a helper class that will be using my "Category model" often.

Here is the code I am using. It is failing when i use Category:

<?php

namespace Msh\Redirects;

use Illuminate\Support\Facades\Config as Config;
use Illuminate\Support\Facades\Input as Input;


/**
 * This is the actual "product class" that generates the 301 redirect url's
 *
 * This is the product
 *
 * @author bgarrison
 */
class StorefrontRedirectsGenerator implements RedirectsGenerator {

    public function generateUrls() {
        // Set the database to the name of the domain
        Config::set('database.connections.mysql_tenant.database', Input::get('domain'));

        // Grab all the categories
        $categories = Category::all();

        // Use category information to determine request url's and target url's
        $urlMapping = [];
        $count = 0;
        $categoryCount = 0;
        foreach ($categories as $category) {
            if ($category->Published == 1) {
                $oldUrl = 'c-' . $category->CategoryID . '-' . $category->SEName . '.aspx';
                $urlMapping['category_urls'][$oldUrl][] = $category->SEName . '.html';
                $count++;
                $categoryCount++;
            }
            if ($category->ParentCategoryID !== '0') {
                $parentCategory = Category::where('CategoryID', '=', (int) $category->ParentCategoryID)->get();
                foreach ($parentCategory as $pcategory) {
                    $url = $pcategory->SEName . '/' . $urlMapping['category_urls'][$oldUrl][0];
                    if (!in_array($url, $urlMapping['category_urls'][$oldUrl])) {
                        $urlMapping['category_urls'][$oldUrl][] = $url;
                        $count++;
                        $categoryCount++;
                    }
                }
            }
        }
        $urlMapping['category_urls']['count'] = $categoryCount;
        return Response::json([
                'success' => true,
                'count' => $count,
                'data' => $urlMapping
        ]);
    }

}

If your model isn't inside a namespace (the default setup with Laravel 4). Add this:

use Category;

If it is inside a namespace:

use Your\Namespace\Category;

Of course you can always directly specify the fully qualified classname . That means if your class has no namespace (exists in the global namespace) you use a backslash to make sure you reference it absolute and not relative to the current namespace:

$categories = \Category::all();

And if the class happens to be in a namespace, just specify the full path:

$categories = Your\Namespace\Category::all();

You should be fine. You either declare it this way:

use Category;

or

use Namespace\If\Any\Category;

before your helper class and leave the rest as is.

Or use it this way:

$categories = \Category::all();

or

$categories = Namespace\If\Any\Category::all();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM