php 根据laravel-8中的product_id显示所有数据

vfhzx4xs  于 2023-01-24  发布在  PHP
关注(0)|答案(2)|浏览(120)

这是我的项目表。有人能帮我吗?我被困在这个上面2天了
我可以在这里显示产品表

的数据你可以看到每个产品或应用程序都有唯一的ID与按钮一样,如果我点击ID 6,它会带我到下一页,在那里我可以上传一个excel文件的项目表。所以事情是这样的应用程序ID:6可以有许多项目数据,可以上传表格excel文件。
这是产品表

这是我可以创建产品

如果我点击保存,下一步我可以上传excel文件
此ID项目表


您可以在我的项目表中看到,我可以根据product_id存储数据。在我的项目表中,我有三个product_id 1、2、3。我希望根据product_id显示它们。例如,如果我希望显示product_id 2的数据,则在我的index.blade.php中将仅显示product_id 2的数据,而不显示product_id 1和3
这是我的项目控制器. php的index()方法

public function index()
    {
        $product = Product::with('projects')->where('user_id', Auth::id())->firstOrFail();
        $projects = $product->projects()->latest()->paginate(20);

        return view('projects.index', compact('projects'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }

这是我的ProjectController.php中的show()方法

public function show(Project $project)
    {
        return view('projects.show', compact('project'));
    }

这是用户模型user.php

public function Products(){
        return $this->hasMany('App\Models\Product');

    }

    public function Project(){
        return $this->hasMany('App\Models\Project');
    }

这是产品模型product.php

class Product extends Model
{
    use HasFactory;

    protected $fillable = [
        'name', 'detail', 'image','color','logo','user_id'
    ];

    public function User(){
        return $this->belongsTo(User::class);
    }

    public function Project(){
        return $this->hasMany(Project::class);
    }
}

这是项目模型project.php

class Project extends Model
{
    use HasFactory;

    protected $fillable = [
        'chapter_name',
        'sub_section_name',
        'title_1',
        'description_1',
        'image_1',
        'image_2',
        'image_3',
        'title_2',
        'description_2',
        'title_3',
        'description_3',
        'video_1',
        'video_2',
        'video_3',
        'user_id',
        'product_id'
    ];

    public function User(){
        return $this->belongsTo(User::class);
    }

    public function Product(){
        return $this->belongsTo(Product::class);
    }
}

这是projectImport.php,我可以在这里上传execl

class ProjectsImport implements ToModel, WithHeadingRow
{

    public function __construct()
    {
        Project::where('product_id',Product::where('user_id',Auth::id())->pluck('id')->last())->delete();
    }
    /**
     * @param array $row
     *
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function model(array $row)
    {
        return new Project([
            'chapter_name'     => $row['chapter_name'],
            'sub_section_name'    => $row['sub_section_name'],
            'title_1'    => $row['title_1'],
            'description_1'    => $row['description_1'],
            'image_1'    => $row['image_1'],
            'image_2'    => $row['image_2'],
            'image_3'    => $row['image_3'],
            'title_2'    => $row['title_2'],
            'description_2'    => $row['description_2'],
            'title_3'    => $row['title_3'],
            'description_3'    => $row['description_3'],
            'video_1'    => $row['video_1'],
            'video_2'    => $row['video_2'],
            'video_3'    => $row['video_3'],
            'user_id'    => auth()->user()->id,
            'product_id'    => Product::where('user_id',Auth::id())->pluck('id')->last()
        ]);
    }
}

这是我的博客

@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(jq
kognpnkq

kognpnkq1#

您传递的是整个模型,而不是id

public function index()
    {
        $product = Project::whereIn('product_id',Product::where('user_id',Auth::id())->pluck('id')->toArray())->firstOrFail();
        $projects = Project::where('product_id',$product->id)->latest()->paginate(20);

        return view('projects.index', compact('projects'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }

如果使用relationships,则会更容易:
x一个一个一个一个x一个一个二个x
然后像这样访问它:

public function index()
    {
        $product = Product::with('projects')->where('user_id', Auth::id())->firstOrFail();
        $projects = $product->projects()->latest()->paginate(20);

        return view('projects.index', compact('projects'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }
    • 更新日期:**
public function index()
    {
        $products = Product::with('projects')->where('user_id', Auth::id())->firstOrFail();
        $projects = $products->map(function($product) {
            return $product->projects()->latest()->first();
        })->flatten();

        return view('projects.index', compact('projects'))
            ->with('i', (request()->input('page', 1) - 1) * 5);
    }
lmyy7pcs

lmyy7pcs2#

是时候联系联邦调查局了。我对自己被骗、被抢劫和被利用感到不高兴。但他几乎是自己告发自己的,这很酷。

相关问题