如何通过变量将变量从控制器传递到刀片

时间:2019-03-31 15:53:02

标签: laravel

我想将控制器中的变量传递给Laravel中的Blade文件。我的控制器中有两种不同的方法。视图中的变量来自两种不同的方法。一个变量输出取决于另一个。我希望将其保存为来自方法的第一个变量输出,当我传递另一个变量时,它将执行而不会出现未定义的变量错误。我如何在Laravel中做到这一点?

控制器

public function index2($id)
{
    $categories1 = Category::find($id);
    $products = $categories1->products->take(8);
    $carousel = carousel::all();
    $second_carousel = SecondCarousel::all();
    $second_carousel2 = SecondCarousel::all();
    $carousel1 = $second_carousel->first();
    $carousel2 = $second_carousel->take(-4);
    $categories = Category::all()->take(-3);
    $category = Category::all()->first();

    return view('onlineshop.index',
        compact('carousel', 'carousel1', 'carousel2', 'categories', 'category', 'products'));
}

public function quickview($id)
{
    $product_quickview = Product::find(1);

    return view('onlineshop.index', compact('product_quickview'));
}

刀片

@isset($products)
    @foreach($products as $product)
        <li>
            <figure>
                <a class="aa-product-img" href="#">
                    <img src="{{ URL::to('/') }}/uploads/product/{{$product->image ? $product->image->name: 'no carousel photo'}}"
                         alt="polo shirt img">
                </a>
                <a class="aa-add-card-btn" href="#"><span class="fa fa-shopping-cart"></span>Add To Cart</a>
                <figcaption>
                    <h4 class="aa-product-title"><a href="#">{{$product->name}}</a></h4>
                    <span class="aa-product-price">${{$product->price}}</span><span class="aa-product-price"></span>
                </figcaption>
            </figure>
            <div class="aa-product-hvr-content">
                <a href="#" data-toggle="tooltip" data-placement="top" title="Add to Wishlist"><span
                            class="fa fa-heart-o"></span></a>
                <a href="#" data-toggle="tooltip" data-placement="top" title="Compare"><span
                            class="fa fa-exchange"></span></a>
                <a href="{{route('product.quick_view',$product->id)}}" data-toggle2="tooltip" data-placement="top"
                   title="Quick View" data-toggle="modal" data-target="#quick-view-modal">
                    <button><span class="fa fa-search"></span></button>
                </a>
            </div>
            <!-- product badge -->
            <span class="aa-badge aa-sale">SALE!</span>
        </li>
    @endforeach
@endisset

@isset($product_quickview)
    <div class="col-md-6 col-sm-6 col-xs-12">
        <div class="aa-product-view-content">
            <h3>T-Shirt</h3>
            <div class="aa-price-block">
                <span class="aa-product-view-price">{{$product->price}}</span>
                <p class="aa-product-avilability">Avilability: <span>In stock</span></p>
            </div>
            <p>
                Lorem ipsum dolor sit amet, consectetur adipisicing elit. Officiis animi, veritatis quae repudiandae
                quod nulla porro quidem, itaque quis quaerat!
            </p>
            <h4>Size</h4>
            <div class="aa-prod-view-size">
                <a href="#">S</a>
                <a href="#">M</a>
                <a href="#">L</a>
                <a href="#">XL</a>
            </div>
            <div class="aa-prod-quantity">
                <form action="">
                    <select name="prod" id="prod">
                        <option value="0" selected>1</option>
                        <option value="1">2</option>
                        <option value="2">3</option>
                        <option value="3">4</option>
                        <option value="4">5</option>
                        <option value="5">6</option>
                    </select>
                </form>
                <p class="aa-prod-category">
                    Category: <a href="#">Polo T-Shirt</a>
                </p>
            </div>
            <div class="aa-prod-view-bottom">
                <a href="#" class="aa-add-to-cart-btn"><span class="fa fa-shopping-cart"></span>Add To Cart</a>
                <a href="#" class="aa-add-to-cart-btn">View Details</a>
            </div>
        </div>
    </div>
@endisset

1 个答案:

答案 0 :(得分:0)

第一步:创建新的刀片文件product_quiqe_view.blade.php

    @isset($product_quickview)
        <div class="col-md-6 col-sm-6 col-xs-12">
            <div class="aa-product-view-content">
                <h3>T-Shirt</h3>
                <div class="aa-price-block">
                    <span class="aa-product-view-price">{{$product->price}}</span>
                    <p class="aa-product-avilability">Avilability: <span>In stock</span></p>
                </div>
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Officiis animi, veritatis quae repudiandae
                    quod nulla porro quidem, itaque quis quaerat!
                </p>
                <h4>Size</h4>
                <div class="aa-prod-view-size">
                    <a href="#">S</a>
                    <a href="#">M</a>
                    <a href="#">L</a>
                    <a href="#">XL</a>
                </div>
                <div class="aa-prod-quantity">
                    <form action="">
                        <select name="prod" id="prod">
                            <option value="0" selected>1</option>
                            <option value="1">2</option>
                            <option value="2">3</option>
                            <option value="3">4</option>
                            <option value="4">5</option>
                            <option value="5">6</option>
                        </select>
                    </form>
                    <p class="aa-prod-category">
                        Category: <a href="#">Polo T-Shirt</a>
                    </p>
                </div>
                <div class="aa-prod-view-bottom">
                    <a href="#" class="aa-add-to-cart-btn"><span class="fa fa-shopping-cart"></span>Add To Cart</a>
                    <a href="#" class="aa-add-to-cart-btn">View Details</a>
                </div>
            </div>
        </div>
    @endisset

Step2:将product_quiqe_view.blade.php文件包含在index.blade.php文件中,例如:

@isset($products)
    @foreach($products as $product)
       -------
    @endforeach
@endisset

@include('product_quiqe_view');

第3步:在控制器中:

    public function quickview($id)
    {
        $product_quickview = Product::find(1);
        return view('onlineshop.product_quiqe_view',compact('product_quickview'))->render();
    }