Class' App \ Http \ Controllers \ App \ Category'未找到

时间:2016-02-20 22:09:53

标签: php mysql laravel laravel-5

我试图查询某个类别及其子类别中的所有商品。 但我在我的控制器上收到此错误。

id
name
category_id (fk)

我有产品表

id
name
parent_id

和类别表:

id | title | parent
1  | Electronics | null
2  | Smartphones | 1
3  | Android   | 2
4  | Clothes   | null

所以如果类别看起来像这样:

id | title | category_id (fk)
1  | Smartphone1 | 3
1  | Smartphone2 | 2

产品:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class category extends Model
{
    //
    protected $fillable = array('id', 'name', 'parent_id', 'image_url');


    public function products()
    {
        // Build an array containing the parent category ID and all subcategory IDs found
        $categoryIds = array_merge([$this->id], $this->subcategoryIds());

        // Find all products that match the retrieved category IDs 
        return Product::whereIn('category_id', $categoryIds)->get();
    }

    protected function subcategoryIds($id = null, &$ids= [])
    {
        // If no ID is passed, set the current model ID as the parent
        if (is_null($id)) {
            $id = $this->id;
        }

        // Find subcategory IDs
        $categoryIds = $this->query()->where('parent', $id)->lists('id');

        // Add each ID to the list and recursively find other subcategory IDs
        foreach ($categoryIds as $categoryId) {
            $ids[] = $categoryId;
            $ids += $this->subcategoryIds($categoryId, $ids);
        }

        return $ids;
    }
}

这是关于如何执行此操作的代码:

类别模型 - app / category.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
use App\Product;
use App\Category;

use App\Repositories\CategoryRepository;    
public function getProductsFromCategory()
        {

            $id = 1;
            $products = App\Category::find($id)->products();
            return view('welcome', [
                'products' => $products,
            ]);

        }

在我的app / Http / Controllers / ProductController.php

// calc.c

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>


void pushValStack(int stack[], int *top, int value) {
  stack[(*top)++] = value;
}
int popValStack(int stack[], int *top) {
  return stack[--(*top)];
}

void pushOpStack(char *stack, int *top, char *value) { // PROBLEM WITH THIS FUNCTION ?
  stack[(*top)++] = *value;
}
char *popOnStack(char *stack, int *top) {
  return stack[--(*top)];

}

int main(int argc, char *argv[]) {
  int i, token, j;
  for (i=1; i<argc; i++) { // This code here takes in arguments from the command line
    printf("%s ", argv[i]);
  }
  int valueStack[50];
  char* operand; 
  char operandStack[50]; // Should be char *? Very confused...
  int valueTop;
  int operandTop;
  for(token = 1; token < argc; token++) { // token starts at 1 because "calc" is argv[0]

    printf("]", argv[4]);
    if (strcmp(argv[token], "x") == 0 || strcmp(argv[token], "+") == 0 || strcmp(argv[token], "[") == 0) {
      // printf("%s", argv[token]);
      pushOpStack(operandStack, &operandTop, argv[token]); // WHERE I BELIEVE THE CODE BREAKS!
      printf("%d", operandTop);
    }
    if (strcmp(argv[token], "]") == 0) {
      while(strcmp(operandStack[operandTop], "[") == 1) {
        // operand = popOnStack(operandStack, &operandTop);
        //int m = popValStack(valueStack, &valueTop);
        //printf("%d", m);
        // int n = popValStack(valueStack, &valueTop);
        //printf("%d", n);
        if (strcmp(operand, "+") == 0) {
         // pushValStack(valueStack, &valueTop, n + m);
        }
        else if (strcmp(operand, "x") == 0) {
       //   pushValStack(valueStack, &valueTop, n * m);
        }
      } 
    } 
    else {
      // pushValStack(valueStack, &valueTop, atoi(argv[token]));
    }   
  }
  //printf("%d", valueTop);
  //printf("%d", valueStack[valueTop]);
}
/*
  while(operandTop > 0) {
    operand = popOnStack(operandStack, &operandTop);
    int m = popValStack(valueStack, &valueTop);
    int n = popValStack(valueStack, &valueTop);
    if (*operand == '+') {
      pushValStack(valueStack, &valueTop, n + m);
    }
    else if (*operand == '*') {
      pushValStack(valueStack, &valueTop, n * m);
    }
  }
  return valueStack[valueTop];
*/

2 个答案:

答案 0 :(得分:3)

替换

if-else

$products = App\Category::find($id)->products();

您已导入该类,无需再次指定路径。

答案 1 :(得分:0)

您使用'category'声明类并使用大写符号App \ Category,因此将您的类名更改为类Category。 如果您使用的是最重要的,那么每次您可以直接使用Category :: whatever()时,您都不必使用App \ Cateogry进行调用。