jlanzas

Experience

3,130

0 Best Reply Awards

  • Member Since 8 Months Ago
  • 23 Lessons Completed
  • 0 Favorites

19th September, 2018

jlanzas left a reply on Obtain Specific Attributes From Collection • 2 days ago

Thank you @Leandro_Haruki && @D9705996 for the help and the time.

jlanzas left a reply on Obtain Specific Attributes From Collection • 2 days ago

Has anyone faced this issue?

jlanzas left a reply on Obtain Specific Attributes From Collection • 2 days ago

This is how my dd looks:

Collection {#855 ▼
    #items: array:32 [▼
        0 => Product {#1104 ▶}
        1 => Product {#1103 ▶}
        2 => Product {#1099 ▶}
       3 => Product {#1095 ▶}
    ]
}

jlanzas started a new conversation Obtain Specific Attributes From Collection • 2 days ago

I have the following collection:

[{
        id: 1,
        sku: null,
        code: "000000000000",
        name: "Galaxy S",
        description: null,
        maker: "Samsung",
        model: "S",
        serial: null,
        price: "3500.0000000000",
        image: null,
        status: true,
        created_at: "30/08/2018",
        updated_at: "2018-08-30 23:02:27",
        deleted_at: null
    },
    {
        id: 1,
        sku: null,
        code: "000000000000",
        name: "Galaxy S2",
        description: null,
        maker: "Samsung",
        model: "S",
        serial: null,
        price: "3500.0000000000",
        image: null,
        status: true,
        created_at: "30/08/2018",
        updated_at: "2018-08-30 23:02:27",
        deleted_at: null
    },
    {
        id: 1,
        sku: null,
        code: "000000000000",
        name: "Galaxy S3",
        description: null,
        maker: "Samsung",
        model: "S",
        serial: null,
        price: "3500.0000000000",
        image: null,
        status: true,
        created_at: "30/08/2018",
        updated_at: "2018-08-30 23:02:27",
        deleted_at: null
    },
    {
        id: 1,
        sku: null,
        code: "000000000000",
        name: "Galaxy S4",
        description: null,
        maker: "Samsung",
        model: "S",
        serial: null,
        price: "3500.0000000000",
        image: null,
        status: true,
        created_at: "30/08/2018",
        updated_at: "2018-08-30 23:02:27",
        deleted_at: null
    },

]

I had the following code to get specific attributes:

$data = $products->map(function ($product) {
    return $product->only(['id', 'name']);
});

The only thing is that only is returning an array which conflicts with another function I have in my app where it loops the attributes. So I'm getting the following error:

Call to a member function getAttributes() on array

10th September, 2018

jlanzas left a reply on Dynamically Add Select, With, Or Withcount To A Model • 1 week ago

Thank you very much @staudenmeir

jlanzas started a new conversation Dynamically Add Select, With, Or Withcount To A Model • 1 week ago

Is is possible to dynamically add these functions to a model, for example: I have a custom helper class which receives a class, columns, relations, with-count parameters and the idea is to return a model response from the class..

function getModel($model, $columns = null, $relations = null, $withCount = null)
{
    if (isset($columns)) {
        $model::select($columns);
    }

    if (isset($relations)) {
        $model->with($relations)
    }

    // Same logic with withCount
}

This is how I call this helper function,

$relations = ['branch'];
$columns = ['id', 'branch_id', 'code', 'name'];
$withCounts = ['products'];

$data = getModel(Warehouse::class, $columns, $relations, $withCounts);

My relationships and everything is working, the reason I want to know if adding functions dynamically is possible is because I have several if in my logic like this...

if (isset($columns) && isset($relations) && isset($withCount)) {
            $model = $model::select($columns)->with($relations)->withCount($withCount)->get();
        } elseif (isset($columns) && isset($relations) && !isset($withCount)) {
            $model = $model::select($columns)->with($relations)->get();
        } elseif (isset($columns) && !isset($relations) && isset($withCount)) {
            $model = $model::select($columns)->withCount($withCount)->get();
        } elseif (isset($columns) && !isset($relations) && !isset($withCount)) {
            $model = $model::select($columns)->get();
        } elseif (!isset($columns) && isset($relations) && isset($withCount)) {
            $model = $model::with($relations)->withCount($withCount)->get();
        } elseif (!isset($columns) && isset($relations) && !isset($withCount)) {
            $model = $model::with($relations)->get();
        } else {
            $model = $model::all();
        }

6th September, 2018

jlanzas left a reply on Dynamically Loop Relationships • 2 weeks ago

True :)

jlanzas left a reply on Dynamically Loop Relationships • 2 weeks ago

Accesor changes the field when saving, mutator changes when getting information

jlanzas left a reply on Dynamically Loop Relationships • 2 weeks ago

this is not a relationship, is a mutator of the model.

jlanzas started a new conversation Dynamically Loop Relationships • 2 weeks ago

Is it possible to dynamically loop relationships from a modelo? This is what a will like to accomplish, I have the following append in many of my model

Product.php

protected $appends = [
    'permissions'
];

Which is working great but I don't need this $append to be added when Eager Loading. I know this setAppends([]) removes this field.

What I want to do is something like

foreach ($model->relationships() as $relationship) {
    $relationship->setAppends([]);
}

Since, this is going to be a global function I don't specifically know the amount of relations there have been set. So that the reason I want to loop the model dynamically.

4th September, 2018

jlanzas left a reply on Custom Intermediate Table Models • 2 weeks ago

Thank you @tykus and @staudenmeir for the time and help.

jlanzas left a reply on Custom Intermediate Table Models • 2 weeks ago

No I had,

Warehouse::with('branch')->withCount('products')->select('id', 'name')->get()

jlanzas left a reply on Custom Intermediate Table Models • 2 weeks ago

Found the problem, apparently adding select->('id', 'column1', 'column2') does not play well withCount

jlanzas left a reply on Custom Intermediate Table Models • 2 weeks ago

select `warehouses`.*, (select count(*) from `products` inner join `product_warehouse` on `products`.`id` = `product_warehouse`.`product_id` where `warehouses`.`id` = `product_warehouse`.`warehouse_id`) as `products_count` from `warehouses`

Which does return the column but my result is not showing anything.

jlanzas left a reply on Custom Intermediate Table Models • 2 weeks ago

Sure I forgot to added, but here it is

public function products()
{
    return $this->belongsToMany('App\Inventory\Product')
        ->withPivot('stock', 'average_cost', 'total_cost', 'minimum', 'ideal', 'maximum')
        ->using('App\Inventory\ProductWarehouse');
}

jlanzas started a new conversation Custom Intermediate Table Models • 2 weeks ago

How can I implement withCount on a many-to-many relationship that has Custom Intermediate Table Model

I have the following models

class Product extends Model
{
}
class Warehouse extends Model
{
}
class ProductWarehouse extends Pivot
{
}

I had before Warehouse::withCount('products') which returned a {relation}_count. This doesn't return the count when I added the custom intermediate table model

31st August, 2018

jlanzas left a reply on Ignore $append When Eagerloading • 2 weeks ago

I have tried constraining the eager load but regardless of the fields I select I get the $appends attributes added.

jlanzas started a new conversation Ignore $append When Eagerloading • 2 weeks ago

I have the following model

class Unit extends Model
{
    protected $fillable = [
        'abbreviation',
        'name',
        'status'
    ];

    protected $casts = [
        'status' => 'boolean',
    ];

    protected $appends = [
        'permissions'
    ];

    public function getPermissionsAttribute($value)
    {
         // Login that check user permissions for this model
    }
}

When I call this model everything works. However, my issue comes when I want to eager load the model mention above. Like so:

$products = Product::with(['unit']);

I'm getting the $appends variable there too. Is there anyway I can remove the $append when eager loading?

27th July, 2018

jlanzas left a reply on Copy Config File To Storage • 1 month ago

I ended up using native php copy

jlanzas started a new conversation Copy Config File To Storage • 1 month ago

Hello everyone, quick question: Is it possible to copy a config file to the Storage folder? This is my code:

Storage::copy(config_path('zeusconfiguration.php'), storage_path('app/zeusconfiguration.php));

Im getting the following error:

File not found at path: Users/aguilar/projects/zeus/config/zeusconfiguration.php

27th June, 2018

jlanzas left a reply on Collection Apply Where Case Insensitive • 2 months ago

I ended up using filtering with strcasecmp this is what I did incase someone finds it helpful...

function filterCollection($collection, $attribute, $toSearch)
{
    return $collection->filter(function ($value, $key) use ($attribute, $toSearch) {
        return strcasecmp($value->{ $attribute }, $toSearch) == 0;
    });
}

26th June, 2018

jlanzas started a new conversation Collection Apply Where Case Insensitive • 2 months ago

I'm trying to apply a where clause to a collection but it seems it is case sensitive... for example:

$this->units->where('abbreviation', 'km')->count()

returns 0 since my collections has "KM" in uppercase.

Anyway I can apply a where case insensitive.

25th April, 2018

jlanzas left a reply on B"string" Error • 4 months ago

I got it working, I can't remember where I found an example of a MSSQL database connection. But it turns out i had 'characterset' => 'utf8'

'local' => [
    'driver'    => 'sqlsrv',
    'host'      => env('DB_HOST'),
    'database'  => 'localhost',
    'username'  => env('DB_USERNAME'),
    'password'  => env('DB_PASSWORD'),
    'charset'   => 'utf8',
    'collation' => 'SQL_Latin1_General_CP1_CI_AS',
    'prefix'    => '',
],

Just changed that property and everything is working fine.

jlanzas left a reply on B"string" Error • 4 months ago

I have the following response

array:3 [▼
  0 => {#1156 ▼
    +"observaciones": b"Some text"
  }
  1 => {#1157 ▼
    +"observaciones": "Text"
  }
  2 => {#1158 ▼
    +"observaciones": "Test"
  }
]

And I get the following error: (1/1) InvalidArgumentException Malformed UTF-8 characters, possibly incorrectly encoded

23rd April, 2018

jlanzas left a reply on B"string" Error • 4 months ago

has anyone run into this problem?

20th April, 2018

jlanzas left a reply on B"string" Error • 5 months ago

@lostdreamer_nl it does affect when I want to return a JSON response. I get

jlanzas left a reply on B"string" Error • 5 months ago

Everything seems to work on my production environment which is Linux.

jlanzas started a new conversation B"string" Error • 5 months ago

Laravel is returning some strings as binary. I have found that it happens on string that contain the following character ñ This doesn't happen with other special characters like `é í ?d it only happens on my MacOS which is running High Sierra.

I'm working with SQL SERVER 2016 and the oficial Microsoft PDO libraries for PHP.

12th April, 2018

jlanzas left a reply on Tinker - Could Not Find Driver • 5 months ago

Thank you @Helmchen the drivers were missing. I don't know why my extensions got disabled.

11th April, 2018

jlanzas left a reply on Tinker - Could Not Find Driver • 5 months ago

@rumm.an Im working on a Mac. The weird thing is that it used to worked. I dont know it doesnt anymore.

jlanzas left a reply on Tinker - Could Not Find Driver • 5 months ago

@Cronix my app works just fine. My tinker is not.

jlanzas left a reply on Tinker - Could Not Find Driver • 5 months ago

@Cronix a simple search

Psy Shell v0.8.18 (PHP 7.1.14 — cli) by Justin Hileman
>>> $user = App\User::find(1)
Illuminate\Database\QueryException with message 'could not find driver (SQL: select top 1 * from [users] where [users].[id] = 1)'
>>>

jlanzas started a new conversation Tinker - Could Not Find Driver • 5 months ago

I'm trying to interact with my mssql server from tinker but I'm getting the following error:

Illuminate\Database\QueryException with message 'could not find driver

My application works ok, only tinker is giving me that error

27th March, 2018

jlanzas left a reply on Filter Results • 5 months ago

thank you @lostdreamer_nl and @tykus both your answers helped me get where I need it.

jlanzas left a reply on Filter Results • 5 months ago

The filtering will only be based on $unit instance.

jlanzas started a new conversation Filter Results • 5 months ago

I have the following data:

Unit::with('products')->get();

Based on that initial collection I need to filtered it. I need a way to search all columns without naming each one in a

->where
->orWhere

So, I was trying the following:

$filtered = $this->units->filter(function ($unit) use ($filter) {
    return $unit->contains($filter);
});

But I keep getting:

BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::contains()

Can anyone help me out.

27th February, 2018

jlanzas left a reply on Boolean Field Returns Empty When Set To False In Blade • 6 months ago

Thank you very much @sutherland appreciate the time.

jlanzas started a new conversation Boolean Field Returns Empty When Set To False In Blade • 6 months ago

Is there a reason why my boolean field returns an empty string when I used in in blade? When I use the following code:

var xxx = "{{ Auth::user()->alertConfigs->progress_bar }}"

and it sets to true (1) I get a 1 in my blade view. I get:

var xxx = "1"

However, when I changed its value to False (0) I'm getting and empty string.

var xxx = ""

Can anyone help me out.

17th February, 2018

jlanzas left a reply on Pass Variable To Api Resource Collections • 7 months ago

Thank you for the answers guys... @skliche I'm working with datatables and pagination. So I need to send back, the page number, total pages, item count, per-page amount, sort field and sort order.

I can get a few fields like the item count, total pages, but the page number is send on a variable the same goes for the per-page amount and the sorting field and order.

So thats the reason I want to send those fields to the ResourceCollection.

jlanzas started a new conversation Pass Variable To Api Resource Collections • 7 months ago

Is there anyway I can pass variable to a resource collection? I have been looking around but can't figure it out.

Any help will be greatly appreciated.

8th February, 2018

jlanzas started a new conversation Permissions Optimization In Blade • 7 months ago

I'm currently working on a system which uses Roles & Permissions to control its security. I have implemented Bouncer successfully. However, there are times where an user will have access to only one "report" inside the reports submodule of a bigger module. Instead of creating a role with a single permission I have added that permission directly onto the user. And everything works ok. No problem there.

Here is my question. Lets say I have a module call Purchases. Inside Purchases I have Products, Orders, Suppliers, Reports, etc. In my blade I have the following syntax:

@if( $user->isAn('role1', 'role2', 'etc'))
<h1>Purchases</h1>
<ul>
    @can('view-orders')
    <li>
        Icon - Orders
    </li>
    @endcan

    @can('view-suppliers')
    <li>
        Icon - Suppliers
    </li>
    @endcan

    @can('view-reports')
    <li>
        Icon - Reports
        <ul>
            <li>Report 1</li>
            <li>Report 2</li>
            <li>Report 3</li>
        </ul>
    </li>
    @endcan
</ul
@endif

The problem now is that my user may only have access to reports directly without needing to be assign to a role. Looking for roles assigned to an user is no longer the best approach.

I found a solution but its to search every single permission at the begging just to paint the main module and the search the submodule individually.

Any ideas will be greatly appreciated and I hope I have explain myself.

Edit Your Profile
Update

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