m33bo
m33bo

Reputation: 1354

Loop collection, build array, join collection with laravel

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


  } 

Upvotes: 1

Views: 986

Answers (1)

user320487
user320487

Reputation:

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.

Upvotes: 2

Related Questions