如何将参数从一个视图传递到另一个视图?

时间:2017-08-18 13:44:22

标签: php laravel

shirt.blade.php:

@extends('layout.main')
@section('title','Shirts') 

@section('content')

<div class="row">

    @forelse($shirts->chunk(20) as $chunk)
    @foreach($chunk as $shirt)
    <div class="small-3 columns">
        <div class="item-wrapper">
            <div class="item2">
                <div class="img-wrapper">
                    <a href="{{route('cart.addItem',$shirt->id)}}"  class="button expanded add-to-cart">
                        <span title = "To Add to cart directly">Add to Cart </span>
                    </a>
                    <a href="#">
                        <img src="{{url('images',$shirt->image)}}"/>
                    </a>
                </div>
                <a href="{{url('/shirt.$shirt->id')}}">
                    <h3>
                        {{$shirt->name}}
                    </h3>
                </a>
                <h5>
                    ${{$shirt->price}}
                </h5>
                <p>
                    {{$shirt->description}}
                </p>
                </div>
            </div>
        </div>
        @endforeach
        @empty
        <h3>No Shirts</h3>
        @endforelse
    </div>
    <br>
  

我正在传递$ shirt-&gt; id参数

</div>
<a href="{{url('/shirt.$shirt->id')}}">
<h3>
    {{$shirt->name}}
</h3>

这是我通过id的控制器。由 FrontController.php命名

public function shirt($id) {
    $shirt = Product::all();
    return view('front.shirt', compact('shirt')
            ->with('id' => '$id');
    }
}

如何从视图 shirts.blade.php 传递ID以查看 shirt.blade.php

请帮助。谢谢你提前

2 个答案:

答案 0 :(得分:1)

你应该试试这个:

更改刀片文件代码和控制器代码,如

<a href="{{url('/shirt', $shirt->id)}}">
    <h3>
       {{$shirt->name}}
    </h3> 


public function shirt($id){
    $id = $id;
    $shirt = Product::all();
   return view('front.shirt',
compact('shirt','id');
}

更新了答案

shirts.blade.php代码

更改
<a href="{{url('shirt', $shirt->id)}}">

    <h3>
        {{$shirt->name}}
    </h3>
</a>

<?php print('<pre style="color:red;">');
  print_r($shirt->id);
  print('</pre>');
  exit; ?>

<a href="{{ route('shirt', [$shirt->id]) }}">

    <h3>
        {{$shirt->name}}
    </h3>
</a>

希望这对你有用!!!

答案 1 :(得分:0)

 public function shirt($id){
   $this_id = $id;
   $shirt = Product::all();
   return view('front.shirt',
   compact('shirt','this_id');
  }
 }