如何在新页面中显示所选产品

时间:2018-02-07 12:04:55

标签: database laravel object view get

我已经通过这条指令做了一切:

Laravel view product page not found

一切正常。

但现在,当我从列表中选择产品时,单击它,然后打开新窗口,我应该看到所选的产品详细信息,但是我收到错误:

  

“未定义的变量:user(查看:C:\ xampp \ htdocs \ Test_Laravel \ resources \ views \ view_object.blade.php)”

我的view_object.blade.php看起来:

<div class="header">
        @include('header')
    </header>
            <body>
                    <div class="content-products">
                        @foreach($user as $users)
                        <div id="myDiv">
                            <div class="products">
                                <img src="{{ $users->Image}}" alt="{{ $users->Title}}" width="200px" height="200px"><br>
                                {{ $users->Title}}<br>
                                {{ $users->Content}}<br>
                                {{ $users->Price}}€<br>
                            </div>
                        </div>
                        @endforeach
                    </div>


            </body>

这是我的控制者:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\file;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Collection;

class BracController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('insertBrac');
    }

    public function showBrac()
    {
        $user=file::all();
        return view('apyrankes', compact('user'));
    }

    public function view_brac()
    {
        $object = \DB::table('braclets')->where('BrackID' , request('brackId'))->get();
        return view('view_object', compact('Object'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        {
        $user = new file;

        $user->Title= Input::get('title');
        $user->Content= Input::get('content');
        $user->Price= Input::get('price');
        if (Input::hasFile('image'))
            {
                $file=Input::file('image');
                $file->move(public_path(). '/uploads', $file->getClientOriginalName());

                $user->Image = $file->getClientOriginalName();
            }

        $user->save();

        return redirect( "insertBrac" );

     }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

3 个答案:

答案 0 :(得分:1)

更改

@foreach($user as $users)
<div id="myDiv">
    <div class="products">
        <img src="{{ $users->Image}}" alt="{{ $users->Title}}" width="200px" height="200px"><br>
        {{ $users->Title}}<br>
        {{ $users->Content}}<br>
        {{ $users->Price}}€<br>
    </div>
</div>
@endforeach

<div id="myDiv">
@if($user)
    <div class="products">
        <img src="{{ $user->Image}}" alt="{{ $user->Title}}" width="200px" height="200px"><br>
        {{ $user->Title}}<br>
        {{ $user->Content}}<br>
        {{ $user->Price}}€<br>
    </div>
@endif
</div>

在您的控制器中:

public function view_brac()
{
    $object = DB::table('braclets')->where('BrackID' , request('brackId'))->first();
    return view('view_object', array('user' => $object));
}

答案 1 :(得分:0)

根据您的previous question

  1. 您必须从视图中传递user,只需要一个用户

    public function view_brac( $BrackID )
    {
      $user = DB::table('tablename')->where('id',$BrackID)->first();
      return view('view_object')->with('user',$user);
    }
    
  2. 并将用户deatils显示为,不需要使用foreach来显示单个数据

    <body>
    <div class="content-products">
        <div id="myDiv">
            <div class="products">
                <img src="{{ $user->Image}}" alt="{{ $user->Title}}" width="200px" height="200px"><br>
                {{ $user->Title}}<br>
                {{ $user->Content}}<br>
                {{ $user->Price}}€<br>
            </div>
        </div>
    </div>
    </body>
    
  3. <强>更新 更新后

    您已将值Object更改为user

    public function view_brac()
    {
        $user = \DB::table('braclets')->where('BrackID' , request('brackId'))->get();
        return view('view_object', compact('user'));
    }
    

    请务必分别使用viewAll刀片和singleView刀片。

答案 2 :(得分:0)

<input type="button" class="button-class" onclick="function(this)">

试试此代码