微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决根据 laravel-8 中的 product_id 显示所有数据

这是我的项目表。谁能帮我?我坚持了 2 天

我可以在这里显示产品表的数据

enter image description here

您可以看到每个产品或应用程序都有唯一的 id 和按钮,就像我点击 id 6 一样,它会带我到下一页,我可以在项目表中上传 excel 文件。所以事情就像这个 App id:6 可以有很多项目数据,可以上传表单 excel 文件。并且excel文件的每个数据都有id

这是产品表

enter image description here

这是我可以创建产品的地方

enter image description here

如果我点击保存然后我可以上传excel文件

这个 id 项目表

enter image description here

您可以在我的项目表中看到我可以根据 product_id 存储数据。在我的项目表中,我有三个 product_id 1,2,3。我想根据 product_id 显示它们。例如,如果我想显示 product_id 2 的数据,那么在我的 index.blade.PHP 中将只显示 product_id 2 的数据,而不是 product_id 1 和 3

这是我的 projectController.PHPindex() 方法

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()
        ]);
    }
}

这是我的 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(jq

解决方法

您传递的是整个模型而不是 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 会更容易:

class Project extends Model
{
    public function product()
    {
        return $this->belongsTo('App\Models\Product');
    }
}
class Product extends Model
{
    public function projects()
    {
        return $this->hasMany('App\Models\Project');
    }
}

并像这样访问它:

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

        return view('projects.index',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',1) - 1) * 5);
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?