Buy your loved one the ultimate gift. Lifetime gift certificates are $100 off.

vladshoob

Experience

16,560

1 Best Reply Awards

  • Member Since 1 Year Ago
  • 158 Lessons Completed
  • 0 Favorites

1st December, 2017

vladshoob started a new conversation How To Validate .docx File? • 1 week ago

I'm trying to upload .docx file with this code:

        $validated = $request->validate([
            'file' => 'mimes:application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        ]);

dd($request) shows me this mime for a file, but for some reason it is not validated. I tried to add zip, app.../msword, docx mimes to validation rules, but not successfully.

Thanks in advance.

29th November, 2017

vladshoob left a reply on Filter Products By HasMany Relation • 2 weeks ago

SOLVED using crossjoin.

$query = $this->builder->Join('subproduct_attributes AS first', 'first.subproduct_id', '=', 'subproducts.id')
                                ->select('subproducts.*',
                                        DB::raw('first.subproduct_id AS sub_id'),
                                        DB::raw('first.property_id AS prop_id'));

        $previous_table = 'first';
        $i = 1;
        foreach ($array as $slug => $attribute) {
            $keys = [];

            foreach ($attribute as $property_key => $value) {
                                // Quick check for property existence
                if (AttributeProperty::find($property_key)) {
                    $keys[] = $property_key;
                }
            }

            // if it is first iteration do not crossjoin
            if ($i == 1) {
                $query->whereIn('first.property_id', $keys);
            } else {
                $query->crossJoin("subproduct_attributes AS $slug", function ($join) use ($keys, $slug, $previous_table) {
                    $join->on("$slug.subproduct_id", '=', "$previous_table.subproduct_id")
                            ->whereIn("$slug.property_id", $keys);
                });;

                $previous_table = $slug;
            }

            $i++;
        }

vladshoob left a reply on Filter Products By HasMany Relation • 2 weeks ago

Some idea came at night

SELECT t1.id, t1.subproduct_id, t1.property_id as pr_id1, t2.property_id as pr_id2 
FROM
        (SELECT * FROM subproduct_attributes WHERE (property_id = 25 OR property_id = 39)) t1
CROSS JOIN
        (SELECT * FROM subproduct_attributes WHERE (property_id = 29)) t2
ON t1.subproduct_id = t2.subproduct_id

28th November, 2017

vladshoob left a reply on Filter Products By HasMany Relation • 2 weeks ago

Nope. It was not rights solution. This one works for 1 property per attribute, but not if 2 properties are there.

Please help, 2 days spent on basic filtering products.

vladshoob left a reply on Filter Products By HasMany Relation • 2 weeks ago

Guys, I figured it out. I help it will help somebody.

$query = $this->builder->Join('subproduct_attributes', 'subproduct_attributes.subproduct_id', '=', 'subproducts.id')
                                ->select('subproducts.*', 'subproduct_attributes.subproduct_id', DB::raw('COUNT(subproduct_attributes.subproduct_id)'))
                                ->groupBy('subproducts.id')
                                ->whereIn('subproduct_attributes.property_id', $keys)
                                ->havingRaw("COUNT(subproduct_attributes.subproduct_id) = $keys_count");

vladshoob left a reply on CRUD Help • 2 weeks ago

First of all, you will need a form's input to reasch ::delete route with:

<input type='hidden' _method='DELETE'>

Secondly you may use boot method on your user model like this:

    protected static function boot()
    {
        parent::boot();

        static::deleting(function ($product) {
            $product->productPhotos->each->delete();
        });
    }

Modificate per your needs.

vladshoob started a new conversation Filter Products By HasMany Relation • 2 weeks ago

Good time of yours, guys. Just can't figure it out.

Have Products, Attributes, Properties(attribute_id), Matches(attribute_id, 'property_id') models. (Names are for example.)

Attributes are: 'size', 'type'..

Properties are 'S', 'M'... 'type 1', 'type 2'...

I have filters like in Jeffrey Way's "Let's build a forum' series.

What I'm trying to accomplish is crossroad filters. In common language: get products that have size S of both types / get products that have S and M sizes of type 1...

// $keys = [1, 2, 3]; // ids of properties: 1 - S, 2 - M, 3 - type 1, 4 - type 2

$query = $this->builder->whereHas('attributes', function ($query) use ($keys) {
      $query->where('matches.property_id', $keys);
});

$query = $this
    ->builder
    ->join('matches', 'matches.product_id', '=', 'products.id')
    ->select('matches.property_id', 'products.*');

foreach ($keys as $key) {
    $query = $query->where('subproduct_attributes.property_id', $key);
}

With both examples above I have save result, it is giving me all products with given sizes and with given types, without crossroading them.

Thanks in advance, you are awesome community.

vladshoob left a reply on Query: Search Products Where Subproducts Has A Value • 2 weeks ago

I think I found solution.

$value = '00-100';
$products = Product::join('subproducts', 'products.id', '=', 'subproducts.product_id')
                ->where(('subproducts.article_code', 'like', "%$value%")
        ->select('products.*');

22nd November, 2017

vladshoob left a reply on Query: Search Products Where Subproducts Has A Value • 3 weeks ago

@RamjithAp

this way I will find only subproducts, but I need to return $products eloquent collection.

vladshoob started a new conversation Query: Search Products Where Subproducts Has A Value • 3 weeks ago

Hello everyone, hope for little hint how to do next.

I have $products and related $subproducts, relationship (product has many subproducts) is in place.

What is correct way to find products where subproducts has some value.

$value = '00-100';
$products = Product::join('subproducts', 'products.id', '=', 'subproducts.product_id')
                ->where(('subproducts.article_code', 'like', "%$value%");

The problem is the correct value is found. But I loose all my with relations in process of this query. Let me explain: product has 10 subproducts, I make described query, and then I don't have access to $product->subproducts.

Thanks in advance.

17th November, 2017

vladshoob left a reply on Order By Own Key • 3 weeks ago

I beleive it is the answer. But I'm not strong in queries.

https://laracasts.com/discuss/channels/eloquent/custom-orderby-in-laravel-query-builder

vladshoob left a reply on Order By Own Key • 3 weeks ago

Update.

$user_unit = Unit::first();

$units = Units::all();
$units = collect($units);

$key = $units->search($user_unit);
$units->pull($key);

$units->prepend($user_unit);
dd($units); // you will have in $units all your Units with prepended $user_unit

Ahh.. you have pagination...

vladshoob left a reply on Order By Own Key • 3 weeks ago

Maybe, you can sort collection.

// get units of user
// delete them from $units collection
// prepend user's units to $units collection

vladshoob left a reply on Order By Own Key • 3 weeks ago

You may do relationship and attach it to Unit.

Unit.php

protected $with = ['user'];

public function user()
{
    return $this->hasOne( ... ); // specific user
}

Then when you call

@foreach ($units as $unit) {
    // some stuff
        {{ $unit->user->name }}
    // some stuff
@endforeach 

Hope it will be helpfull.

vladshoob left a reply on Get URI From Route Name • 3 weeks ago

Neigher of methods worked for me. I was able to find URI by this:


uri_for(route('ua.index'))->getPath();

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.