Alexander
Alexander

Reputation: 290

Looking up model in Laravel after returning array of objects in Controller

I am trying to do something I've never done before in Laravel and cannot figure out how to do it. I have the following code in my Controller:

public function show($id)
{
    //Get application for drug
    $application = PharmaApplication::where('ApplNo', $id)->first();


    //Get all products for given application (i.e. the different quantities and forms drug comes in)
    $product = PharmaProduct::where('ApplNo', $id)->get();

    foreach($product as $product){
            $product->ProductNo;
    }


    //Get Marketing Status for drug
    $marketingStatus = DB::table('pharma_marketing_statuses')
                            ->where('ApplNo', $id)
                            ->where('ProductNo', $product->ProductNo)
                            ->get();


    //Lookup marketing status Description
    $marketingStatusDescription = PharmaMarketingSatusLookup::where('MarketingStatusID', $marketingStatus->MarketingStatusID);

    return view('profiles.drug', compact('application', 'product', 'marketingStatus', 'marketingStatusDescription'));

}

I am trying to accomplish the following:

  1. Get the application for a drug - this part of my code works
  2. Return an array of objects for the products (i.e. 7 products that belong to one application). I can do this but get stuck going to the next part.
  3. Next, I have to use the array of objects and search a table with the following columns: MarketingStatusID, ApplNo, ProductNo. I know how to query this table and get one row, but the problem is I have an array that I need to search. I imagine I have to use a loop but don't know where.
  4. Finally, I use the MarketingStatusID to retrieve the MarketingStatusDescription which I will know how to do.

I am also getting an error message that says:

Class 'App\Http\Controllers\profiles\PharmaMarketingSatusLookup' not found

In my Controller, I have use App\PharmaMarketingStatusLookup; so I am not sure why it is searching the Controllers folder

Upvotes: 1

Views: 56

Answers (2)

VIKAS KATARIYA
VIKAS KATARIYA

Reputation: 6005

USE whereIn

use App\PharmaApplication;
use App\PharmaProduct;
use App\PharmaMarketingSatusLookup;

public function show($id)
{
    $application = PharmaApplication::where('ApplNo', $id)->first();

    $products = PharmaProduct::where('ApplNo', $id)->get();

    $productid = array();
    foreach($products as $product){
           $productid[] = $product->ProductNo;
    }


    $marketingStatus = DB::table('pharma_marketing_statuses')
                            ->where('ApplNo', $id)
                            ->whereIn('ProductNo', $productid)
                            ->get();


    $marketingStatusDescription = PharmaMarketingSatusLookup::where('MarketingStatusID', $marketingStatus->MarketingStatusID);

    return view('profiles.drug', compact('application', 'product', 'marketingStatus', 'marketingStatusDescription'));

}

Upvotes: 0

Poldo
Poldo

Reputation: 1932

You have a typo in your class

From PharmaMarketingSatusLookup change to PharmaMarketingStatusLookup

App\Http\Controllers\profiles\PharmaMarketingStatusLookup

Upvotes: 1

Related Questions