Laravel子类别和父类别,值未显示

时间:2018-11-16 04:45:36

标签: php html laravel laravel-blade

我正在尝试在刀片文件中创建子类别和类别列表视图。我已经创建了所有必要的东西,但是由于某种原因,我的视图中没有显示任何值。没有出现任何错误,但是我也没有看到任何值。

这是我的观点

 @foreach ($pro_cat as $cat)
              <ul class="list-group">
                    <a data-toggle="collapse" href="#collapseExample" class="card-header list-group-item d-flex justify-content-between align-items-center">
                        <option value="{{ $cat->id }}">{{ ucwords($cat->name) }}</option>
                      <span class="badge badge-primary badge-pill">{{ count($pro_cat)}}</span>
                    </a>
                    @foreach ($sub_cat as $cat)
                        @if($sub_cat->cat_id == $pro_cat->id)
                    <div id="collapseExample" class="collapse">
                            <ul class="list-group">
                            <option class="list-group-item" value="{{ $cat->id }}">{{ ucwords($cat->name) }}</option>
                            </ul>
                            @endif
                    </div>
                    @endforeach
                    @endforeach

我不确定它是否与视图相关,或者我的控制器中是否有错误,但是为了防止万一,我将控制器连接到了下面。任何帮助将不胜感激

 $pro_cat = Pro_cat::where('id', NULL)->get();
  $sub_cat = Sub_cat::where('cat_id','!=',NULL)->get();
    return view('liturgy.posts.index')->with('pro_cat', $pro_cat)->with('posts', $posts)->with('sub_cat', $sub_cat);

2 个答案:

答案 0 :(得分:0)

嗯,很简单。像这样建立关系

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model {

    public function sub_category()
    {
        return $this->hasMany(self::class, 'cat_id');
    }

    public function parent_category()
    {
        return $this->belongsTo(self::class, 'cat_id');
    }

}

然后,在调用模型时,像这样添加with()

$categories = Category::with('sub_category')->get();

return view('someview', compact('categories'));

或仅在模型上添加`$ with`属性,例如受保护的$ with = ['sub_category','parent_category'];。这将自动将关系添加到集合中。 风景

@foreach ($categories as $category)
    {!! $category->name !!}
    @foreach($category->sub_category as $sub_category)
        {!! $sub_category->name !!}
    @endforeach
@endforeach

答案 1 :(得分:0)

为您的“类别”模型添加关系:

public function resize($pic_path,$two,$width,$height)
    {
        $config['source_image'] = $pic_path;
        $config['new_image'] = $two;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = $width;
       // $config['height'] = $height;
        $this->CI->image_lib->initialize($config);
        $this->CI->image_lib->resize();
        $this->CI->image_lib->clear();
        return true;
    }

    public function watermark($pic_path)
    {
        $config['image_library'] = 'gd2';
        $config['source_image'] = $pic_path;
        $config['wm_type'] = 'overlay';
        $config['wm_overlay_path'] = 'assets/img/test.png';
        $config['wm_opacity'] = 50;
        $config['width'] = '330';
        $config['height'] = '100';
        $config['wm_vrt_alignment'] = 'middle';
        $config['wm_hor_alignment'] = 'center';
        $this->CI->image_lib->initialize($config);
        $this->CI->image_lib->watermark();
        $this->CI->image_lib->clear();
        return true;
    }

获取在控制器中渴望加载子关系的类别:

public function children()
{
    return $this->hasMany(self::class, 'cat_id');
}

public function parent()
{
    return $this->belongsTo(self::class, 'cat_id');
}

访问关系,例如视图中的简单属性:

$posts = blahblah...
$categories = Category::with('children')->get();
return return view('liturgy.posts.index', compact('categories', 'posts'));