1

Having trouble achieving the desired results. I am trying to get the user collection model i have defined, sort through the ids assigned and get the products with that ID.

I cannot seem to then pass this back to the controller. If i just echo I get what I want back but I would prefer to add it to the collection then I can just call it from one collection (everything I need) rather than calling two arrays.

//get collection relationship
        $collection = User::find(1)->collection;
        // product number id
        $products = $collection->products;
        // remove ',' character 
        $productId = explode(',', $products);
        //count many products 
        $productCount = count($productId);


        // collection
        foreach ($productId as $key => $value) {
            // dd($products);
            $array = $this->getArray(str_split($value));
            // array
            foreach($array as $product){
                $name = $product->name;
                $img = $product->image_path;
                $price = $product->price;


                $productFinal = $name . ' ' . $img  . ' ' . $price;
                //price
                echo $productFinal;

            }
        }

This returns the list of all products associated, I am just struggling to then append that to a collection or other array and pass that to blade so that I can do a foreach loop on $productFinal and get $product->name etc. I would like to append it to the $collection, however push() or merge() have been unsuccessful. Many thanks peace :)

edit: Collection model reference

class Collection extends Model
{
protected $id = ['id'];

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

class Product extends Model
{
// uses carbon instance from model $dates
protected $dates = ['created_at'];
protected $id = ['id'];

public function setPublishedAtAttribute($date)
{
    $this->attributes['created_at'] = Carbon::createFromFormat('Y-m-d', $date);
}
/**
 * Example mutators for the scopes
 * @param  [type] $query [description]
 * @return [type]        [description]
 */
public function scopePublished($query)
{
    $query->where('created_at', '<=', Carbon::now());
}

public function scopeUnpublished($query)
{
    $query->where('created_at', '>', Carbon::now());
}

/**
 * Return category for product bread controller
 * @return [type] [description]
 */
 public function category(){
    return $this->belongsTo(ProductCategory::class);
 }


  } 

1 Answer 1

2

How is User::find(1)->collection defined? Can you post your models to show the relationship between Users, collection?, and Products?

Try setting up a hasManyThrough relationship like so:

/**
 * Get all of the products for the user.
 */
public function products()
{
    return $this->hasManyThrough('App\Product', 'App\Collection');
}

That will result in a collection object containing all the products for the user.

Sign up to request clarification or add additional context in comments.

5 Comments

So you're looking for all products belonging to a user through the collection model as i understand?
yes, and want to append each product to the collection in place of where the product ids go and have an array of all products
I will try this
how can I replace the "products" => "1, 2, 3, 4" with an array of products? because I still have to run the function to get relevant products. Thanks for your time dude
Product::whereIn('id', explode(',', "1,2,3,4"))->get(); Just needs to get the comma separated ids and then transform them into an array and finally pass that along in the where clause.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.