0

我正在尝试从 cadmodel 表中获取 category_id = $category_id 的所有 CadModels...我不知道我错在哪里...我收到此错误。

为 foreach() 提供的参数无效(查看:C:\Ampps\www\gcf\resources\views\cadmodels\index.blade.php)

这是我的过滤功能

public function filter(Categories $category_id)
    {
        $categories = Categories::all();
        $cadmodels = CadModel::where('category_id', $category_id)->first();
        return view('cadmodels.index',['cadmodels' => $cadmodels, 'categories' => $categories]);
    }

这是我的 index.blade.php

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row">
            <div class="header-bottom-on">
                <p class="wel">
                    <a href="">Welcome visitor you can login or create an account.</a>
                </p>
                <br>
                <div class="header-can">
                    <ul class="social-in">
                        <li><a href="" target="_blank"><i class="facebook"> </i></a></li>
                        <li><a href="" target="_blank"><i class="twitter"> </i></a></li>
                    </ul>
                    <div class="search">
                        <form action="" method="post" id="searchBox" name="searchBox">
                            <input type="text" name="strSearch" value="Search" align="middle">
                            <input type="submit" value="" name="SubSearch2">
                            <center><a href="" style="font-size:x-small;">Advanced Search</a></center>
                        </form>
                    </div>
                    <div class="clearfix"> </div>
                </div>
                <div class="clearfix"></div>
            </div>
            <div class="col-md-3">
                <h3 class="future">CATEGORIES</h3>
                <div class="list-group">
                    <a href="" class="list-group-item">All Categories</a>
                    @foreach($categories as $category)
                        <a class="list-group-item" href="{{route('cadmodels.filter',$category->id)}}">{{$category->name}}</a>
                    @endforeach
                </div>
            </div>
            <div class="col-md-9">
                <div class="content-top-in">
                    <h3 class="future">FEATURED</h3>
                    @foreach($cadmodels as $cadmodel)
                    <a href="{{route('cadmodels.show',[$cadmodel->id])}}">
                        <div class="col-md-3 md-col" style="padding-bottom:20px;">
                            <div class="col-md">
                                <center><img src="{{ Storage::url($cadmodel->image_url) }}" border="0" alt="a"></center><br>
                                <div class="top-content">
                                    <h5>{{ $cadmodel->name }}</h5>
                                    <div class="white">
                                        <p align="center" class="hvr-shutter-in-vertical hvr-shutter-in-vertical2 ">View</p>
                                        <div class="clearfix"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </a>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
@endsection

CadModel 控制器

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CadModel extends Model
{
    protected $fillable = ['name','category_id','description','image_url'];

}

类别控制器

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Categories extends Model
{
    protected $fillable = ['name'];

}
4

0 回答 0