如何在助手 class 中使用 laravel Eloquent 模型
How to use laravel Eloquent model in helper class
如何在控制器之外使用 laravel Eloquent 模型?我似乎无法找出我应该使用什么 use "*insert class here*"
语句。我有一个帮手 class,他会经常使用我的 "Category model"。
这是我正在使用的代码。当我使用类别时失败:
<?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
]);
}
}
如果您的模型不在命名空间内(Laravel 4 的默认设置)。添加:
use Category;
如果它在命名空间内:
use Your\Namespace\Category;
当然,您始终可以直接指定 完全限定的 class名称。这意味着如果您的 class 没有名称空间(存在于全局名称空间中),您可以使用反斜杠来确保您引用它是绝对的而不是相对于当前名称空间:
$categories = \Category::all();
如果 class 恰好在命名空间中,只需指定完整路径:
$categories = Your\Namespace\Category::all();
你应该没事的。您可以这样声明:
use Category;
或
use Namespace\If\Any\Category;
在你的助手 class 之前,其余的保持原样。
或者这样使用:
$categories = \Category::all();
或
$categories = Namespace\If\Any\Category::all();
如何在控制器之外使用 laravel Eloquent 模型?我似乎无法找出我应该使用什么 use "*insert class here*"
语句。我有一个帮手 class,他会经常使用我的 "Category model"。
这是我正在使用的代码。当我使用类别时失败:
<?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
]);
}
}
如果您的模型不在命名空间内(Laravel 4 的默认设置)。添加:
use Category;
如果它在命名空间内:
use Your\Namespace\Category;
当然,您始终可以直接指定 完全限定的 class名称。这意味着如果您的 class 没有名称空间(存在于全局名称空间中),您可以使用反斜杠来确保您引用它是绝对的而不是相对于当前名称空间:
$categories = \Category::all();
如果 class 恰好在命名空间中,只需指定完整路径:
$categories = Your\Namespace\Category::all();
你应该没事的。您可以这样声明:
use Category;
或
use Namespace\If\Any\Category;
在你的助手 class 之前,其余的保持原样。
或者这样使用:
$categories = \Category::all();
或
$categories = Namespace\If\Any\Category::all();