我的显示功能在 Laravel 中不起作用。URL显示特定的id,但页面数据不会出现

当我单击产品页面上的特定产品时,它会将我定向到单个产品页面。URL 中还显示特定的产品 ID。但数据不会被传递。我的单个产品页面不显示特定的产品数据。


这是我的控制器。


SingleProductController.php



<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use App\Product;

use App\Category;


class SingleProductController extends Controller

{

    public function index(Product $product)

    {

        $arr['product'] = $product;

        return view('singleproducts')->with($arr);


    }


    

    public function create()

    {

        //

    }


   

    public function store(Request $request)

    {

        //

    }


    

    public function show($id)

    { 

    


        $product = Product::find($id);

       

        return view ('singleproducts')->with(['product'=>$product]);

    }


   

    public function edit($id)

    {

        //

    }


    

    public function update(Request $request, $id)

    {

        //

    }


   

    public function destroy($id)

    {

        //

    }

}

这是我的刀片文件。


singleproducts.blade



@extends('layouts.singleproducts')

@section('content')


   <h2 class="font-size-25 text-lh-1dot2">{{ $product ['prod_name'] }}  </h2>


 <div class="mb-4">

                                    <div class="d-flex align-items-baseline">

                                        <ins class="font-size-36 text-decoration-none">{{ $product ['prod_price'] }}</ins>

                                        <del class="font-size-20 ml-2 text-gray-6">$2,299.00</del>

                                    </div>

                                </div>

    

@endsection


aluckdog
浏览 96回答 3
3回答

红颜莎娜

路线.phpRoute::get('/singleproducts', 'SingleProductController@index')->name('singleproducts');Route::get('/eachproduct/{id}', 'SingleProductController@show')->name('eachproduct');产品列表页@if (isset($products) && count($products) > 0)&nbsp; &nbsp; @foreach ($products as $product)&nbsp; &nbsp; <div class="blog_box_inner" style="cursor: pointer;" >&nbsp; &nbsp; &nbsp; &nbsp; <h1 class="blog_inner_heading">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="/eachproduct/{{$product['id']}}" class="blog_readmore_link">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{$product['prod_name']}}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; </h1>&nbsp; &nbsp; </div>&nbsp; &nbsp; @endforeach&nbsp; &nbsp; @endif单页<h2 class="font-size-25 text-lh-1dot2">{{ $product['prod_name']}}&nbsp; </h2>控制器public function index() {&nbsp; &nbsp; $products = Product::orderBy('id', 'DESC')->get();&nbsp; &nbsp; return view('')->with(compact('products'));}public function show($id) {&nbsp; &nbsp; $product = Product::find($id);&nbsp; &nbsp; &nbsp;return view('')->with(compact('product'));}完全希望它能轻松地帮助您。

茅侃侃

尝试这个&nbsp;public function show(Product $product){&nbsp;&nbsp; &nbsp; return view ('singleproducts', compact('product'));}并通过路线Route::get('/eachproduct/{product}', 'SingleProductController@show')->name('eachproduct');并在您的视图页面中获取产品数据singleproducts.blade@extends('layouts.singleproducts')@section('content')<h2 class="font-size-25 text-lh-1dot2">{{ $product->prod_name }}&nbsp; </h2><div class="mb-4"><div class="d-flex align-items-baseline"><ins class="font-size-36 text-decoration-none">{{ $product->prod_price }}</ins><del class="font-size-20 ml-2 text-gray-6">$2,299.00</del></div></div>@endsection它会根据 id 自动查找您的产品。

拉莫斯之舞

像这样使用return&nbsp;view('singleproducts',compact('product'));在你看来,像这样使用<h2&nbsp;class="font-size-25&nbsp;text-lh-1dot2">{{&nbsp;$product->prod_name&nbsp;}}&nbsp;&nbsp;</h2> <ins&nbsp;class="font-size-36&nbsp;text-decoration-none">{{&nbsp;$product->prod_price&nbsp;}}</ins>
打开App,查看更多内容
随时随地看视频慕课网APP