datarecall

datarecall

Member Since 8 Months Ago

Experience Points
33,780
Total
Experience

1,220 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
324
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-in-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 7
33,780 XP
Aug
13
1 day ago
Activity icon

Replied to Eloquent Sub Query

@michaloravec thanks that worked perfectly.

Activity icon

Replied to Eloquent Sub Query

$product = Product::find(88);
        $diaries = Diary::whereHas('productables', function ($query) use ($product) {
            $query->where('product_id', $product->id);
        })->get();
        dd($diaries);

Product 88 is attached to App\Diary and it is also attached to a Week (which belongs to a different diary) https://share.getcloudapp.com/lluYYEPp

The result is pulling the right Diary but since this product is attached to the Week as well need to figure out a way to grab the diary that corresponds to those weeks.

Activity icon

Replied to Eloquent Sub Query

Just sent you an email @michaloravec

Activity icon

Replied to Eloquent Sub Query

@michaloravec we already setup the morphto on the productable model though so using it like above is forcing us to do it that way.

Activity icon

Replied to Eloquent Sub Query

@michaloravec ok so

$diaries = Diary::whereHas('productables.productable', function ($query) use ($product) {
            $query->where('id', $product->id);
        })->get();

Since Productables model has productable method and its a morphto it wants me to use a wherehasmorph but then we run in into the problem of the nested relationships again

Activity icon

Replied to Eloquent Sub Query

@michaloravec I get that part but say on my product page I want to list all diaries

$product = Product::find(1);
$diaries = Diary::
			//See if this product is used on the diary itself
				//if it is return the diary
			// see if the product is used on the week
				//if it is used on a week find the corresponding diary that has this week and return it
Activity icon

Replied to Eloquent Sub Query

@michaloravec thanks alot for all your help, one other question for you if you don't mind since you kind of understand the table structure

If I have a product now with a specific ID how would you determine the diaries that use this product since its a polymorphic table and the week belongs to a specific diary you would need to figure out a way to do a subquery to attach the App\Week with the Diary_id ?

Activity icon

Replied to Eloquent Sub Query

Got it the orWhereIn was causing the issue @michaloravec I switched it to wherein on the id

Activity icon

Replied to Eloquent Sub Query

I think its the way its joining up this is the query that is being produced by the one we have done so far

SELECT
	*
FROM
	`products`
WHERE
	EXISTS (
		SELECT
			*
		FROM
			`productables`
		WHERE
			`products`.`id` = `productables`.`product_id`
		AND (
			(
				`productables`.`productable_type` = 'App\Diary'
				AND EXISTS (
					SELECT
						*
					FROM
						`diaries`
					WHERE
						`productables`.`productable_id` = `diaries`.`id`
					AND `id` = 13
				)
			)
			OR (
				`productables`.`productable_type` = 'App\Week'
				AND EXISTS (
					SELECT
						*
					FROM
						`weeks`
					WHERE
						`productables`.`productable_id` = `weeks`.`id`
					OR (`id` IN(51, 52, 53, 54))
				)
			)
		)
	)

But the query I think it needs to be is:

SELECT
	*
FROM
	`products`
WHERE
	EXISTS (
		SELECT
			*
		FROM
			`productables`
		WHERE
			`products`.`id` = `productables`.`product_id`
		AND (
			(
				`productables`.`productable_type` = 'App\Diary'
				AND
				`productables`.`productable_id` = 13
			)
			OR (
				`productables`.`productable_type` = 'App\Week'
				AND		
				`productables`.`productable_id` IN(51,52,53,54)
				
			)
		)
	)
Activity icon

Replied to Eloquent Sub Query

Making headway @michaloravec ok so the results produced don't seem to be correct though that query is producing 44 results for $products however when I run these queries

dump($diary->products); //PRODUCES 1 Result
dump($diary->weeks->pluck('products')->collapse()); //PRODUCES 5 RESULTS

also I tried changing

$query->where('id', $diary->id);
$query->where('productable_id', $diary->id);

but the results still returned 44 so maybe the wherehasmorph filter isn't running at all ?

Activity icon

Replied to Eloquent Sub Query

Ok @michaloravec I got that all setup now the error is :

Call to undefined method App\Product::productables.productable()

but I can run it through tinker like so:

Psy Shell v0.10.4 (PHP 7.4.5 — cli) by Justin Hileman
>>> $p = Product::find(1)
[!] Aliasing 'Product' to 'App\Product' for this Tinker session.
=> App\Product {#4237
     id: 1,
     supplier_id: 1,
     name: "omnis",
     long_desc: "Dicta eveniet suscipit deserunt facere dolore necessitatibus
 provident atque. Maxime qui rerum sint quaerat non est dolore. Ut eos pariat
ur suscipit fuga illo aut dolorem consequatur. Quaerat velit totam aut possim
us.",
     release_date: "2018-04-18",
     active: 1,
     created_at: "2020-08-07 20:20:48",
     updated_at: "2020-08-07 20:20:48",
     media: Illuminate\Database\Eloquent\Collection {#4236
       all: [],
     },
   }
>>> $p->productables->first()->productable
=> App\Diary {#4231
     id: 12,
     user_id: 442,
     name: "Similique et adipisci quia vero et dolorem dolorem.",
     type: "gree",
     grow_medium: "hy",
     published_at: "2020-08-07 20:20:54",
     created_at: "2020-08-07 20:20:54",
     updated_at: "2020-08-07 20:20:54",
   }
>>>
Activity icon

Replied to Eloquent Sub Query

Sorry @michaloravec trying to explain everything here

I have a productable table which is more or less a pivot table that will link to the Diary / Week which contains the product_id here is a screenshot of the productable table: https://share.getcloudapp.com/P8ubb5e1

Then I have trying to query all products which are in this is the Product Model, I added this to my products model as per your instructions

public function productable()
    {
        return $this->hasMany(Productables::class,'product_id');
    }

for this part here:

public function productable()
{
    return $this->morphTo();
}

should I create the productable model and add that to it?

Activity icon

Replied to Eloquent Sub Query

this is in my Product Model:

public function productable()
    {
        return $this->morphTo('productable');
    }

when I add 'productable.productable' its giving me the error:

BadMethodCallException
Call to undefined method App\Product::productable.productable()
Activity icon

Replied to Eloquent Sub Query

@michaloravec this is the error that I get for that

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'products.productable_type' in 'where clause' (SQL: select * from `products` where ((`products`.`productable_type` = App\Diary and exists (select * from `diaries` where `products`.`productable_id` = `diaries`.`id` and `id` = 13)) or (`products`.`productable_type` = App\Week and exists (select * from `weeks` where `products`.`productable_id` = `weeks`.`id` or (`id` in (51, 52, 53, 54))))))

it seems to be looking in the products table not the productable table but the end result of the query its getting all the products in the diary, so would you need a subquery ?

Activity icon

Replied to Eloquent Sub Query

@michaloravec I saw that but how would you modify the query to use it properly?

$weekIds = $diary->weeks->pluck('id');
        $products = Product::query()
            ->whereHasMorph(
                'productable',
                ['App\Diary', 'App\Week'],
                function (Builder $query, $type) use($diary,$weekIds){
                    if ($type === 'App\Diary') {
                        $query->orWhere('productable_id', $diary->id);
                    }
                    if ($type === 'App\Week') {
                        $query->orWhereIn('productable_id', $weekIds);
                    }
                })->get();

gives me the error Column not found: 1054 Unknown column 'products.productable_type'

Activity icon

Started a new Conversation Eloquent Sub Query

I have a polymorphic table called productables it laid out like: https://share.getcloudapp.com/z8uZZYeR

product_id is linked to my products table

A product can be added to a diary and/or a product can be added to a week

If I want to grab all products that belong to that diary/weeks I am trying a query like this:

$weekIds = $diary->weeks->pluck('id'); //(this return an array of all the week ids for this diary)
$products = Product::query()
            ->whereHas('productable', function ($query) use ($diary) {
                $query->where([
                    'productable_type' => 'App\Diary',
                    'productable_id' => $diary->id
                ]);
            })->orWhereHas('productable', function ($query) use ($weekIds) {
                $query->whereIn([
                    'productable_type' => 'App\Diary',
                    'productable_id' => $weekIds
                ]);
            })->get();
        dd($products);

but productable is not a relationship on the Product model I don't think it should be either unless I am wrong?

Aug
07
1 week ago
Activity icon

Replied to Pagination

@jlrdw the problem is I can't figure out how to turn that method into an eloquent query right now this is just a collection which you can't paginate cleanly

Activity icon

Started a new Conversation Pagination

I have a method on my Supplier model that looks like:

public function getAllDiariesAttribute()
    {
        $products = $this->products();
        $weeks = $products->with('weeks.diary.latestWeek')->get()->pluck('weeks')->collapse()->pluck('diary');

        return $weeks->merge($products->with('diaries.latestWeek')->get()->pluck('diaries'))->flatten()->unique('id');
    }

Products are attached to the diary itself or to the weeks(which are attached to the diary)

Any thoughts on how you could make this an eloquent query to be able to do pagination on the results?

Aug
03
1 week ago
Activity icon

Started a new Conversation Webpack: Trying To Deploy Over Asset Limit

I am over my assets amount of 300 when uploading to vapor so I changed my webpack.mix.js file to :

const mix = require('laravel-mix');
var S3Plugin = require('webpack-s3-plugin')

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js('resources/js/app.js', 'public/assets/js')
    .sass('resources/sass/app.scss', 'public/assets/css')
    .copyDirectory('resources/img', 'public/assets/img')
    .version();

if (mix.inProduction()) {
    mix.version();
    mix.webpackConfig({
        plugins: [
            new S3Plugin({
                // Exclude uploading of html
                exclude: /.*\.html$/,
                // s3Options are required
                s3Options: {
                    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
                    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
                    region: process.env.AWS_DEFAULT_REGION
                },
                s3UploadOptions: {
                    Bucket: process.env.AWS_BUCKET,
                    CacheControl: 'public, max-age=31536000'
                },
                directory: 'public/assets'
            })
        ]
    });
}

if (mix.inProduction()) {
    const ASSET_URL = process.env.ASSET_URL + "/";

    mix.webpackConfig(webpack => {
        return {
            plugins: [
                new webpack.DefinePlugin({
                    "process.env.ASSET_PATH": JSON.stringify(ASSET_URL)
                })
            ],
            output: {
                publicPath: ASSET_URL
            }
        };
    });
}

This uploads all my asset files to amazon s3 however since the files are still there they are trying to deploy to vapor as well, any thoughts on how I can "clean" this directory after the upload but prior to deploying to vapor ?

Aug
01
1 week ago
Activity icon

Replied to Help With Resource

the problem with that is that you Can see that getRatings() is returning a paginate method which however that did get me to try to transform the array on an ->transform :

    public function index(Product $product)
    {
        $ratings = $this->getRatings($product);

        $ratings->transform(function ($item, $key) use($product){
            $item->product = $product;
            return $item;
        });
        return RatingResource::collection($ratings);
    }

while this produces the correct result I am wondering if there is a better way then looping through all the ratings and appending the product onto it.

Activity icon

Replied to Help With Resource

@geordiejackson $this->resource is giving me the resource from $ratings which is done here:

 /**
     * @param Product $product
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getRatings(Product $product): \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        return $product->ratings()
            ->latest()
            ->with('user')
            ->withPivot('score', 'body')
            ->paginate(config('rollitup.pagination.ratingsPerPage'));
    }

this is pulling data from the pivot table. So $this->resource is actually giving back the ratings()

Activity icon

Started a new Conversation Help With Resource

In my product controller I have

   public function index(Product $product)
    {
        $ratings = $this->getRatings($product);
        
        return RatingResource::collection($ratings);
    }
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class RatingResource extends JsonResource {
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'user'  => $this->user,
            'product' => NEED TO HAVE $PRODUCT HERE
            'diary' => [
                'title' => $this->name,
                'url'   => $this->route,
                'comment_count' => $this->comment_count,
                'weeks_count' => $this->weeks_count
            ],
            'created_at' => $this->pivot->created_at,
            'score' => $this->pivot->score,
            'body'  => $this->pivot->body,
        ];
    }
}

In this resource, I need access to the $product since I need to send the product back when an ajax request is hit

Jul
30
2 weeks ago
Activity icon

Replied to Pagination Problem + Eloquent Help

@michaloravec I tried to do has many through but could retrieve the right results is this possible do you think?

Activity icon

Replied to Pagination Problem + Eloquent Help

@michaloravec @jlrdw the problem is that this is in the suppliers model/level and a supplier has many products which is why I thought I could maybe use HasManyThrough

$supplier->products()->with('ratings.user')->get()->pluck('ratings')->sortByDesc('created_at');

but this is returning a collection and you can't paginate a collection you can only paginate an eloquent query.

Activity icon

Started a new Conversation Pagination Problem + Eloquent Help

Help With Pagination

I have a model called "supplier" and this model has many "products"

Each product has a rating with is attached by a pivot table product_id / diary_id

I am currently using this to get the $supplier ratings

public function getRatingsAttribute()
    {
        return $this->products()->with('ratings.user')->get()->pluck('ratings')->sortByDesc('created_at');
    }

which returns this

Illuminate\Support\Collection {#4351
  #items: array:1 [
    0 => Illuminate\Database\Eloquent\Collection {#4359
      #items: array:1 [
        0 => App\Diary {#4364
          +fillable: array:5 [
            0 => "user_id"
            1 => "name"
            2 => "type"
            3 => "grow_medium"
            4 => "published_at"
          ]
          #with: array:2 [
            0 => "media"
            1 => "user"
          ]
          #appends: array:3 [
            0 => "thumbCoverUrl"
            1 => "route"
            2 => "isHarvested"
          ]
          #casts: array:2 [
            "user_id" => "integer"
            "published_at" => "datetime"
          ]
          #connection: "sqlite"
          #table: "diaries"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:8 [
            "id" => "1"
            "user_id" => "1"
            "name" => "Sint illo eveniet sed eaque animi ratione."
            "type" => "outdoor"
            "grow_medium" => "soiless"
            "published_at" => "2020-07-30 18:14:47"
            "created_at" => "2020-07-30 18:14:47"
            "updated_at" => "2020-07-30 18:14:47"
          ]
          #original: array:14 [
            "id" => "1"
            "user_id" => "1"
            "name" => "Sint illo eveniet sed eaque animi ratione."
            "type" => "outdoor"
            "grow_medium" => "soiless"
            "published_at" => "2020-07-30 18:14:47"
            "created_at" => "2020-07-30 18:14:47"
            "updated_at" => "2020-07-30 18:14:47"
            "pivot_product_id" => "1"
            "pivot_diary_id" => "1"
            "pivot_score" => "1"
            "pivot_body" => "this is my comment"
            "pivot_created_at" => "2020-07-30 18:14:47"
            "pivot_updated_at" => "2020-07-30 18:14:47"
          ]
          #changes: []
          #classCastCache: []
          #dates: []
          #dateFormat: null
          #dispatchesEvents: []
          #observables: []
          #relations: array:3 [
            "pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#4367
              +incrementing: false
              #guarded: []
              #connection: "sqlite"
              #table: "product_ratings"
              #primaryKey: "id"
              #keyType: "int"
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:6 [
                "product_id" => "1"
                "diary_id" => "1"
                "score" => "1"
                "body" => "this is my comment"
                "created_at" => "2020-07-30 18:14:47"
                "updated_at" => "2020-07-30 18:14:47"
              ]
              #original: array:6 [
                "product_id" => "1"
                "diary_id" => "1"
                "score" => "1"
                "body" => "this is my comment"
                "created_at" => "2020-07-30 18:14:47"
                "updated_at" => "2020-07-30 18:14:47"
              ]
              #changes: []
              #casts: []
              #classCastCache: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #fillable: []
              +pivotParent: App\Product {#4352
                +fillable: array:5 [
                  0 => "supplier_id"
                  1 => "name"
                  2 => "long_desc"
                  3 => "active"
                  4 => "release_date"
                ]
                #with: array:1 [
                  0 => "likes"
                ]
                #appends: array:2 [
                  0 => "likesCount"
                  1 => "isLiked"
                ]
                #casts: array:4 [
                  "supplier_id" => "integer"
                  "active" => "boolean"
                  "dry_weight" => "integer"
                  "wet_weight" => "integer"
                ]
                #connection: "sqlite"
                #table: "products"
                #primaryKey: "id"
                #keyType: "int"
                +incrementing: true
                #withCount: []
                #perPage: 15
                +exists: false
                +wasRecentlyCreated: false
                #attributes: []
                #original: []
                #changes: []
                #classCastCache: []
                #dates: []
                #dateFormat: null
                #dispatchesEvents: []
                #observables: []
                #relations: []
                #touches: []
                +timestamps: true
                #hidden: []
                #visible: []
                #guarded: array:1 [
                  0 => "*"
                ]
                +mediaConversions: []
                +mediaCollections: []
                #deletePreservingMedia: false
                #unAttachedMediaLibraryItems: []
              }
              #foreignKey: "product_id"
              #relatedKey: "diary_id"
            }
            "user" => App\User {#4376
              #fillable: array:6 [
                0 => "is_admin"
                1 => "name"
                2 => "email"
                3 => "password"
                4 => "api_token"
                5 => "avatar"
              ]
              #with: array:1 [
                0 => "media"
              ]
              #appends: array:1 [
                0 => "thumbAvatarUrl"
              ]
              #hidden: array:2 [
                0 => "password"
                1 => "remember_token"
              ]
              #casts: array:2 [
                "is_admin" => "boolean"
                "email_verified_at" => "datetime"
              ]
              #connection: "sqlite"
              #table: "users"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:10 [
                "id" => "1"
                "is_admin" => "0"
                "name" => "Marcus Prohaska"
                "email" => "[email protected]"
                "email_verified_at" => "2020-07-30 18:14:47"
                "password" => "yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi"
                "api_token" => null
                "remember_token" => "ps9PxiqFLk"
                "created_at" => "2020-07-30 18:14:47"
                "updated_at" => "2020-07-30 18:14:47"
              ]
              #original: array:10 [
                "id" => "1"
                "is_admin" => "0"
                "name" => "Marcus Prohaska"
                "email" => "[email protected]"
                "email_verified_at" => "2020-07-30 18:14:47"
                "password" => "yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi"
                "api_token" => null
                "remember_token" => "ps9PxiqFLk"
                "created_at" => "2020-07-30 18:14:47"
                "updated_at" => "2020-07-30 18:14:47"
              ]
              #changes: []
              #classCastCache: []
              #dates: []
              #dateFormat: null
              #dispatchesEvents: []
              #observables: []
              #touches: []
              +timestamps: true
              #visible: []
              #guarded: array:1 [
                0 => "*"
              ]
              #rememberTokenName: "remember_token"
              +mediaConversions: []
              +mediaCollections: []
              #deletePreservingMedia: false
              #unAttachedMediaLibraryItems: []
            }
          ]
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [
            0 => "*"
          ]
          +mediaConversions: []
          +mediaCollections: []
          #deletePreservingMedia: false
          #unAttachedMediaLibraryItems: []
        }
      ]
    }
  ]
}

The problem is I need to paginate these results but since its already a collection laravel will not allow you to easily do this, could I possibly get by with a HasManyThrough on the $supplier->ratings() then I should be able to paginate that?

Jul
29
2 weeks ago
Activity icon

Replied to Pivot Table Grouping

That worked perfectly @silencebringer thank you very much for your help!

Activity icon

Replied to Pivot Table Grouping

@silencebringer thanks for the help, still having an issue though it looks like: This is what returns from $this->techniques https://share.getcloudapp.com/9ZuXe4PO

/**
     * Determine the techniques used in a diary
     */
    public function getTechniquesAttribute()
    {
        return $this->weeks()->with('techniques')->get()->pluck('techniques', 'number');
    }

Since its week number as the key then a collection of techniques that are used inside that collection would you have to foreach through the first transform?

Jul
28
2 weeks ago
Activity icon

Replied to Pivot Table Grouping

@silencebringer thank you for your response, and I apologize I was not overly clear on the model

That model is the Diary Model the Diary Model Has Man Weeks and the pivot table is technique_id/week_id so

$diary->weeks()->with('techniques')

I think is the proper way but my code just feels ugly.

So in our Diary template we need to be able to show this

image TechniqueName
	   Weeks 1-3,5,9
Activity icon

Started a new Conversation Pivot Table Grouping

I have a pivot table called techniques and this pivot table connects techniques with weeks

public function getTechniquesAttribute()
    {
//NUMBER is the week number for the grouping later on
        return $this->weeks()->with('techniques')->get()->pluck('techniques', 'number');
    }

    public function getStringTechniquesAttribute()
    {
        $techniqueCollection = $this->techniques;

        $selectedTechniques = [];
        foreach ( $techniqueCollection as $weekNumber => $collection )
        {
            foreach ( $collection as $technique )
            {
                $selectedTechniques[$technique->name][] = $weekNumber;
            }
        }

        foreach ( $selectedTechniques as $name => $arr )
        {
            $desc = Str::plural('Week', count($arr));
            $out[] = [
                'name' => $name,
                'image' => 'NA',
                'desc' => $desc . ' ' . $this->rangeToStr($arr)
            ];
        }

        return $out;
    }
	

This code somewhat works but now I need the image column from techniques and it's forcing me to redo this, there has to be a better way to accomplish this I just don't see how.

Range to string just takes an array and implodes the array and does a range check so it will display 1-3,5,8

The end result in blade is:

image TechniqueName
	   Weeks 1-3,5,9
Jul
26
2 weeks ago
Activity icon

Replied to Help With Eloquent

Thank you for your reply @s4muel, I guess I'm wondering if an attribute is the proper place at all since most attributes are just computer but here I am merging 2 relationships into a value.

Jul
23
3 weeks ago
Activity icon

Started a new Conversation Help With Eloquent

Hello,

I have a polymorphic table that has "productables" and I am wondering if I am doing this right:

I created a method on Suppliers model to grab all the diaries linked with the supplier would this be the correct way to do it, the results are coming back as expected but I don't think it should be an attribute?

public function getAllDiariesAttribute(){
        $weeks = $this->products()->with('weeks.diary')->get()->pluck('weeks')->collapse()->pluck('diary');
        $w =  $weeks->merge($this->products()->with('diaries')->get()->pluck('diaries'));
        return $w->filter()->flatten()->all();
    }
Jun
29
1 month ago
Activity icon

Replied to Polymorphic Relationship Help

Still trying to figure this out, anyone have any ideas?

Jun
28
1 month ago
Activity icon

Replied to Polymorphic Relationship Help

@meeshal let me know if this clears some things up: https://share.getcloudapp.com/o0uvZLBY

The only thing it really doesn't show is that productables table the type will either be App\Diary or App\DiaryWeek which links to the respective table with the ID

So the end goal would be to do $suppliers->diaries or $products->diaries which will look through the productables table grab all the diaries and the associated diares from DiaryWeek

Activity icon

Started a new Conversation Polymorphic Relationship Help

Here is a bit of background on the app, I am not 100% sure I should be using a polymorphic relationship here but this is how I have it currently setup.

This application will allow users to create Diaries or journals for growing fruits/vegetables Then every week they can add a week to that journal.

What this part of the application does is when they initially set up their journal they tell the application which products they are using to start this journal lights, room size etc

When they add a week they are also allowed to add products to the week since they might use specific nutrients or change the lighting etc

Initially, I had this setup as a polymorphic table so you can attach products to the diary or the week but now Im thinking it might have been the wrong way to go since its so difficult for me to see which suppliers or products have journals (that the user added)

I think the reason I am struggling because in the polymorphic it looks like this https://share.getcloudapp.com/p9uPXB2g but a DiaryWeek belongs to Diary so there is no easy way to grab all the products that a Diary Uses

My end goal is to be able to go to a supplier and do $supplier->diaries or $product->diaries which will go through the productables table grab all the diaries that are using that supplier/product and then grab all the weeks associate them with the diary and return those diaries.

I hope this makes sense lol

Jun
11
2 months ago
Activity icon

Replied to WYSIWYG Editor On The Laracasts

So on Laracasts it is just a simple text area then you pass that to commonmark to store inside the database? I took a look at the network tab it looks like when you turn the preview on it just sends the body of the text area to markdown and it responds with the formatted HTML?

Trying to think of the best way to do this with VueJS but also need the ability for attachments.

#test

Jun
09
2 months ago
Activity icon

Replied to Vapor Image Optimization

Excellent thanks @bobbybouwmann I will take a look

Jun
08
2 months ago
Activity icon

Started a new Conversation Vapor Image Optimization

We are looking at deploying a site on Vapor, however we have a lot of files that are user uploaded (we can store on s3) but is there a way to use some sort of Image Optimization so we can optimize these files prior to them hitting s3?

I might be wrong but since you can't install any 3rd party software I cant see it being possible and not much in the way of googling has helped.