0
votes

I need to show information of checkout_pages table that has two foreign keys that needs to be validated.

  • users

    • id
  • products

    • id
    • user_id
  • checkout_pages

    • id
    • user_id
    • product_id

The product has an owner (products.user_id), the checkout_pages has relationships between users and products. I need to show the information about checkout_pages for the specific user and specific product (not necessarily the product owner).

I make some code, but it only validate the checkout_pages.product_id and I can't make it validate the checkout_pages.user_id.

Model Product

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

ProductController

 public function index()
    {
        $products = Product::all()->where('user_id', \Auth::user()->id);
        return view('products.index', compact('products'));
    }

Edit

  • User can have multiple checkout_pages, but of different products
  • This is the code I have now, I know it isn't validating the checkout_pages table, but looking through the Laravel Doc I couldn't find an answer
  • I need to get this infos from checkout_pages in the ProductController.
1
This code doesn't validate anything, it gets data from the database and sends it to a view. What is the question exactly? Maybe you meant something else than "validate"?Loek
what do you mean by foreign keys that needs to be validated.?kheengz
@kheengz I need to get the checkout_pages register to show to a specific user(checkout_pages.user_id). So the user will see only his own checkout_pages of all products (checkout_pages.product_id) he applied for. The validation of products table is ok (only the owner/user can see his own product), but I can't handle the 'validation' of the checkout_pages (any user can have a checkout_pages of any product, but only this user can see his own checkout_pages).lucastfernandes
@lucastfernandes i made a recommendation below...kheengz

1 Answers

0
votes

ProductController

public function index()
    {
        $products = Product::where('user_id', \Auth::user()->id)->get();
        return view('products.index', compact('products'));
    }

from your view blade.php file

@foreach($products as $product)

    @foreach($product->checkout_pages()->where('user_id', Auth::user()->id)->get() as $checkoutPage)
        {{ $checkoutPage }}
    @endforeach

@endforeach

so $checkoutPage hold each checkout_pages for the $product coming from each products