获取所有CadModel,其中Category id ='id'..... Laravel 5.4

时间:2017-09-17 09:56:26

标签: php mysql laravel-5 laravel-blade laravel-controller

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

  

为foreach()提供的参数无效(查看:   C:\ Ampps \ WWW \ GCF \资源\视图\ 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'];

}

0 个答案:

没有答案