Abdullah Al Shahed
Abdullah Al Shahed

Reputation: 89

Show all data according to product_id in laravel-8

This is my projects table. Can anyone help me? I am stuck on this for 2 day

I can show here datas of products table enter image description here You can see every product or app have unique id with button like if i click id 6 it will take me to next page where I can upload an excel file in project table. so things is like this App id:6 can have many projects data which can be upload form excel file. and every data of excel file have id

This is products table enter image description here This is where i can create products enter image description here If i click save and next i can upload excel file

This id projects table

enter image description here You can see in my project table I can store data according to product_id. In my projects table I have three product_id 1, 2, 3. I want to show them according to product_id. For example, if I want to show data of product_id 2 then in my index.blade.php will show only data of product_id 2 not product_id 1 and 3

This is my index() method of projectController.php

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

This is my show() method in my ProjectController.php

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

This is user model user.php

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

    }

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

This is product model 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);
    }
}

This is project model 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);
    }
}

This is projectImport.php where i can upload 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()
        ]);
    }
}

This is my 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

Upvotes: 1

Views: 2707

Answers (1)

shaedrich
shaedrich

Reputation: 5715

You're passing the whole model instead of the id column

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

It'd be easier if you used 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');
    }
}

And access it like this:

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

Update:

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

Upvotes: 1

Related Questions