【问题标题】:Method Illuminate\Support\Collection::links does not exist. (View: C:\xampp\htdocs\ContentBaseApp\resources\views\projects\index.blade.php)方法 Illuminate\Support\Collection::links 不存在。 (查看:C:\xampp\htdocs\ContentBaseApp\resources\views\projects\index.blade.php)
【发布时间】:2021-07-18 16:43:35
【问题描述】:

我正在尝试根据 forignId(product_id)从我的项目表中检索数据。这里 forignId 来自产品表,我在 laravel 中使用 CURD 添加数据,生成请求的 id。 这是产品表 这是我的项目表。 这是 index.blade.php

@extends('layouts.app')

@section('content')

    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Laravel 8 CRUD </h2>
            </div>
            <div class="d-flex flex-row-reverse flex-column">
                <div class="d-flex">
                    <a class="btn btn-success text-light mr-5" data-toggle="medel" id="mediumButton" data-target="#mediumModel"
                    data-attr="{{ route ('projects.create')}}" title="upload project">
                        <i class="fas fa-cloud-upload-alt fa-2x"></i>
                    </a>
                    <form action="{{ route('importProject') }}" method="POST" enctype="multipart/form-data" class="d-flex">
                        @csrf
                        <input type='file' name="file">

                        <button class="btn btn-info" style="margin-left: -60px" title="Import Project">
                            <i class="fas fa-cloud-upload-alt fa-2x"></i></button>

                            <a class="btn btn-warning" href="{{ route('export') }}">Export User Data</a>
                    </form>

                </div>
            </div>
            <div class="pull-right">
                <a class="btn btn-success text-light" data-toggle="modal" id="mediumButton" data-target="#mediumModal"
                    data-attr="{{ route('projects.create') }}" title="Create a project"> <i class="fas fa-plus-circle"></i>
                </a>
            </div>
        </div>
    </div>

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

    <table class="table table-bordered table-responsive-lg table-hover">
        <thead class="thead-dark">
            <tr>
                <th scope="col">No</th>
                <th scope="col">Chapter Name</th>
                <th scope="col" >Sub-Section Name</th>
                <th scope="col">Title 1</th>
                <th scope="col">Description 1</th>
                <th scope="col">Image 1</th>
                <th scope="col">Image 2</th>
                <th scope="col">Image 3</th>
                <th scope="col">Title 2</th>
                <th scope="col">Description 2</th>
                <th scope="col">Title 3</th>
                <th scope="col">Description 3</th>
                <th scope="col">Video 1</th>
                <th scope="col">Video 2</th>
                <th scope="col">Video 3</th>

                <th scope="col">Date Created</th>
                <th scope="col">Action</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($projects as $project)
                <tr>
                    <td scope="row">{{ ++$i }}</td>
                    <td>{{ $project->chapter_name }}</td>
                    <td>{{ $project->sub_section_name }}</td>
                    <td>{{ $project->title_1 }}</td>
                    <td>{{ $project->description_1 }}</td>
                    <td>{{ $project->image_1 }}</td>
                    <td>{{ $project->image_2 }}</td>
                    <td>{{ $project->image_3 }}</td>
                    <td>{{ $project->title_2 }}</td>
                    <td>{{ $project->description_2 }}</td>
                    <td>{{ $project->title_3 }}</td>
                    <td>{{ $project->description_3 }}</td>
                    <td>{{ $project->video_1 }}</td>
                    <td>{{ $project->video_2 }}</td>
                    <td>{{ $project->video_3 }}</td>

                    <td>{{ date_format($project->created_at, 'jS M Y') }}</td>
                    <td>
                        <form action="{{ route('projects.destroy', $project->id) }}" method="POST">

                            <a data-toggle="modal" id="smallButton" data-target="#smallModal"
                                data-attr="{{ route('projects.show', $project->id) }}" title="show">
                                <i class="fas fa-eye text-success  fa-lg"></i>
                            </a>

                            <a class="text-secondary" data-toggle="modal" id="mediumButton" data-target="#mediumModal"
                                data-attr="{{ route('projects.edit', $project->id) }}">
                                <i class="fas fa-edit text-gray-300"></i>
                            </a>
                            @csrf
                            @method('DELETE')

                            <button type="submit" title="delete" style="border: none; background-color:transparent;">
                                <i class="fas fa-trash fa-lg text-danger"></i>
                            </button>
                        </form>
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>

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


    <!-- small modal -->
    <div class="modal fade" id="smallModal" tabindex="-1" role="dialog" aria-labelledby="smallModalLabel"
        aria-hidden="true">
        <div class="modal-dialog modal-sm" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body" id="smallBody">
                    <div>
                        <!-- the result to be displayed apply here -->
                    </div>
                </div>
            </div>
        </div>
    </div>


    <!-- medium modal -->
    <div class="modal fade" id="mediumModal" tabindex="-1" role="dialog" aria-labelledby="mediumModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body" id="mediumBody">
                    <div>
                        <!-- the result to be displayed apply here -->
                    </div>
                </div>
            </div>
        </div>
    </div>


    <script>
        // display a modal (small modal)
        $(document).on('click', '#smallButton', function(event) {
            event.preventDefault();
            let href = $(this).attr('data-attr');
            $.ajax({
                url: href,
                beforeSend: function() {
                    $('#loader').show();
                },
                // return the result
                success: function(result) {
                    $('#smallModal').modal("show");
                    $('#smallBody').html(result).show();
                },
                complete: function() {
                    $('#loader').hide();
                },
                error: function(jqXHR, testStatus, error) {
                    console.log(error);
                    alert("Page " + href + " cannot open. Error:" + error);
                    $('#loader').hide();
                },
                timeout: 8000
            })
        });

        // display a modal (medium modal)
        $(document).on('click', '#mediumButton', function(event) {
            event.preventDefault();
            let href = $(this).attr('data-attr');
            $.ajax({
                url: href,
                beforeSend: function() {
                    $('#loader').show();
                },
                // return the result
                success: function(result) {
                    $('#mediumModal').modal("show");
                    $('#mediumBody').html(result).show();
                },
                complete: function() {
                    $('#loader').hide();
                },
                error: function(jqXHR, testStatus, error) {
                    console.log(error);
                    alert("Page " + href + " cannot open. Error:" + error);
                    $('#loader').hide();
                },
                timeout: 8000
            })
        });

    </script>

@endsection

这是我的 projectController.php

<?php

namespace App\Http\Controllers;

use App\Exports\UsersExport;
use App\Models\Project;
use App\Imports\ProjectsImport;
use App\Models\Product;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\get;


class ProjectController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {


        // $projects = Project::whereIn('product_id',Product::where('user_id',Auth::id())->pluck('id'))->latest()->paginate(20);


        // $products = Product::where('user_id',Auth::id())->pluck('id');
        $product = $request->input('id');
        // $projects = Project::where('product_id',$product)->pluck('id')->paginate(20);
        $projects = DB::table('projects')->pluck('product_id',$product);


        return view('projects.index', compact('projects'))
            ->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('projects.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'chapter_name' => 'required',
            'sub_section_name' => 'required',
            'title_1' => 'required',
            'description_1' => 'required',
            'image_1' => 'required',
            'image_2' => 'required',
            'image_3' => 'required',
            'title_2' => 'required',
            'description_2' => 'required',
            'title_3' => 'required',
            'description_3' => 'required',
            'video_1' => 'required',
            'video_2' => 'required',
            'video_3' => 'required',
        ]);
        // $input = Project::whereIn('product_id',Product::where('user_id',Auth::id())->pluck('id'));
        $product = $request->input('id');
        $input = Project::where('product_id',$product);
        Project::create($input);
        return redirect()->route('project.index')
               >with('success','Product created successfully.');
    }

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

//     public function show(Product $product)
// {
//     return view('projects.show', [
//         'projects' => $product->projects()->latest()->paginate(20),
//     ]);
// }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Project  $project
     * @return \Illuminate\Http\Response
     */
    public function edit(Project $project)
    {
        return view('projects.edit', compact('projects'));
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Project  $project
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Project $project)
    {
        // $user_id =  Auth::user()->id ;

        $request->validate([
            'chapter_name' => 'required',
            'sub_section_name' => 'required',
            'title_1' => 'required',
            'description_1' => 'required',
            'image_1' => 'required',
            'image_2' => 'required',
            'image_3' => 'required',
            'title_2' => 'required',
            'description_2' => 'required',
            'title_3' => 'required',
            'description_3' => 'required',
            'video_1' => 'required',
            'video_2' => 'required',
            'video_3' => 'required',
        ]);

        // $input = $request->all();

        $product = $request->input('id');
        $input = Project::where('product_id',$product);

        $project->update($input);

        return redirect()->route('project.index')
                        ->with('success','Product updated successfully');
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Project  $project
     * @return \Illuminate\Http\Response
     */
    public function destroy(Project $project)
    {
        $project->delete();

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

    public function importProject()
    {
        Excel::import(new ProjectsImport, request()->file('file'));

        return back()->with('success','Project created successfully.');
    }

    public function export()
    {
        return Excel::download(new UsersExport, 'projects.xlsx');
    }
}

【问题讨论】:

    标签: php laravel laravel-8


    【解决方案1】:

    请阅读文档https://laravel.com/docs/8.x/pagination#displaying-pagination-results

    您需要在构建器上调用 paginate() 方法(或 simplePaginate() 或 cursorPaginate())(感谢@lagbox),以便在刀片模板中使用 links() 方法。

    编辑: 如果我正确理解您的示例,我会像这样编辑 ProjectController@index:

    public function index(Request $request)
    {
        $productId = $request->input('id');
        $projects = Project::where('product_id', $productId)->paginate(15);
    
    
        return view('projects.index')->with(
                [
                    'projects' => $projects,
                    'i' => ($request->input('page', 1) - 1) * 5
                ]
            );
    }
    

    然后,将{{ $projects-&gt;links() }} 放入 index.blade.php 文件中。

    但是,ProjectController 文件中还有很多需要重构的地方,例如 store 方法中的这个废话:

    $input = Project::where('product_id',$product);
    Project::create($input);
    

    【讨论】:

    • 这些方法将在 Builder 而不是 Collection 上调用
    • 看看我是 Laravel 的初学者我已经添加了这个代码 {!! $projects->links() !!} 但仍然无法正常工作。从你那里我无法解决问题
    • @AbdullahAlShahed 我编辑了我的答案,请检查。我还建议您查看这些文档 - laravel.com/docs/8.x/querieslaravel.com/docs/8.x/collections(尤其是 pluck() 方法)
    • 好的,但它没有给我我想要的东西。在这里,我在项目表中上传了一个 excel 文件。项目表有一行名为 product_id。这是从产品表中请求的 id。如果请求的 id 为 3,则 index.blade.php 将显示项目表中请求的 id 3 的所有数据。这没有发生。为此,我使用了 pluck 函数
    猜你喜欢
    • 2019-08-06
    • 1970-01-01
    • 2019-08-29
    • 2019-12-06
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    相关资源
    最近更新 更多