【问题标题】:i want to view only last data i have entered in my database but it showing all data from database in laravel-8我只想查看我在数据库中输入的最后一个数据,但它显示了 laravel-8 中数据库中的所有数据
【发布时间】:2021-04-28 10:28:07
【问题描述】:

我正在做一个项目,在我的 index.blade.php 中,我只想显示我输入的最后一个数据,但我编写它的路线显示了数据库中的所有数据。我怎样才能只显示我输入的最后一个数据。提前致谢。 你能在我的代码上看到并帮助我吗? 这是我的 index.blde.php 中的代码

@extends('products.layout')

@section('content')
    <div class="row">
        <div class="pull-right">
            <!-- Authentication -->
               <form method="POST" action="{{ route('logout') }}">
                   @csrf

                   <x-jet-dropdown-link href="{{ route('logout') }}"
                           onclick="event.preventDefault();
                                   this.closest('form').submit();">
                       {{ __('Log Out') }}
                   </x-jet-dropdown-link>
               </form>
           </div>
        {{--  --}}
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Click Button</h2>
            </div>
            <div class="pull-right">
                <a class="btn btn-success" href="{{ route('products.create') }}"> For New Data</a>
            </div>
        </div>
    </div>

    @if ($message = Session::get('success'))
        <div class="alert alert-success">
            <p>{{ $message }}</p>
        </div>
    @endif

    <table class="table table-bordered">
        <tr>
            <th>No</th>
            <th>App Name</th>
            <th>App Logo</th>
            <th>Splash Image</th>
            <th>Description</th>
            <th>Color</th>
            <th width="280px">Action</th>
        </tr>
        @foreach ($products as $product)
        <tr>
            <td>{{ ++$i }}</td>
            <td>{{ $product->name }}</td>
            <td><img src="/logo/{{ $product->logo }}" width="100px"></td>
            <td><img src="/image/{{ $product->image }}" width="100px"></td>

            <td>{{ $product->detail }}</td>
            <td>{{ $product->color }}</td>
            <td>
                <form action="{{ route('products.destroy',$product->id)}}" method="POST">

                    <a class="btn btn-info" href="{{ route('products.show',$product->id) }}">Show</a>

                    <a class="btn btn-primary" href="{{ route('products.edit',$product->id) }}">Edit</a>

                    <a class="btn btn-info" href="products_link">Get Json</a>

                    @csrf
                    @method('DELETE')

                    <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </table>

    {!! $products->links() !!}

@endsection

这是 ProductController.php

 <?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\Admin\StoreTagsRequest;

class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //$tags = Product::all();
        //return view('products.index', compact('tags'));

        $products = Product::latest()->paginate(5);

        return view('products.index',compact('products'))

            ->with('i', (request()->input('page', 1) - 1) * 5);
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //$tag = Product::create($request->all());

        //return redirect()->route('admin.tags.index');
        $request->validate([
            'name' => 'required',
            'detail' => 'required',
            'color' => 'required',
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'logo' => 'required|mimes:jpeg,png,jpg,gif,svg|max:512',
        ]);

        $input = $request->all();

        if ($image = $request->file('image')) {
            $destinationPath = 'image/';
            $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
            $image->move($destinationPath, $profileImage);
            $input['image'] = "$profileImage";
        }

        if ($logo = $request->file('logo')) {
            $destinationPath = 'logo/';
            $profileLogo = date('YmdHis') . "." . $logo->getClientOriginalExtension();
            $logo->move($destinationPath, $profileLogo);
            $input['logo'] = "$profileLogo";
        }

        Product::create($input);

        return redirect()->route('products.index')
                        ->with('success','Product created successfully.');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function show(Product $product)
    {
        return view('products.show',compact('product'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function edit(Product $product)
    {
        return view('products.edit',compact('product'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Product $product)
    {
        $request->validate([
            'name' => 'required',
            'detail' => 'required',
            'color' => 'required'
        ]);

        $input = $request->all();

        if ($image = $request->file('image')) {
            $destinationPath = 'image/';
            $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
            $image->move($destinationPath, $profileImage);
            $input['image'] = "$profileImage";
        }else{
            unset($input['image']);
        }

        if ($logo = $request->file('logo')) {
            $destinationPath = 'logo/';
            $profileLogo = date('YmdHis') . "." . $logo->getClientOriginalExtension();
            $logo->move($destinationPath, $profileLogo);
            $input['logo'] = "$profileLogo";
        }else{
            unset($input['logo']);
        }

        $product->update($input);

        return redirect()->route('products.index')
                        ->with('success','Product updated successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Product  $product
     * @return \Illuminate\Http\Response
     */
    public function destroy(Product $product)
    {
        $product->delete();

        return redirect()->route('products.index')
                        ->with('success','Product deleted successfully');
    }

    function indextwo(){
        //return DB::select("select * from  products");
       //DB::table('products')->orderBy('id','desc')->first();
       return Product::orderBy('id', 'DESC')->first();
    }
    
}

【问题讨论】:

    标签: php laravel laravel-8


    【解决方案1】:

    好吧,看来您发布了删除表单的代码。 你可以试试:

    Product::latest()->get();
    

    按 LIFO(后进先出)的顺序显示所有产品。

    Product::latest()->first();
    

    获取您输入的最后一个产品。

    【讨论】:

    • 我已经编辑了我的代码,你能再帮我一次,我必须添加这个代码吗?我是 larave-8 的新成员
    • 是的 $products = Product::latest()->paginate(5);应该显示最后 5 个产品,不是吗?
    猜你喜欢
    • 1970-01-01
    • 2022-01-27
    • 2022-01-25
    • 2019-11-08
    • 1970-01-01
    • 2015-03-22
    • 2016-03-27
    • 1970-01-01
    • 2019-02-11
    相关资源
    最近更新 更多