Spyralex

Member Since 5 Months Ago

Experience Points
9,580
Total
Experience

420 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
88
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.

  • Community Pillar

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

Level 2
9,580 XP
May
11
2 days ago
Activity icon

Replied to Product::make Does Not Exist. BadMethodCallException Exception

Ok I find out the issue. I had my model Product but also named my component Product. So when I was using the Product::make() inside the component it was actually searching for a make function inside there and not in the Eloquent model.

I renamed my component from Product to ProductComponent and now it works.

Activity icon

Replied to Product::make Does Not Exist. BadMethodCallException Exception

There are not much to explain. In the video he presents this as the null object pattern for models in Livewire. No extra set up or code.

Activity icon

Replied to Product::make Does Not Exist. BadMethodCallException Exception

According the video here: https://laravel-livewire.com/screencasts/s7-edit-modal (25:36)

You don't have to add a make function in the model.

Activity icon

Started a new Conversation Product::make Does Not Exist. BadMethodCallException Exception

I am using the null object pattern as Caleb shows in his tutorials (Binding data tables series - Edit modal episode) and I am getting BadMethodCallException exception: Product::make does not exist.

The code is as follows:

public function mount() {
    $this->product = Product::make()
}
May
09
4 days ago
Activity icon

Started a new Conversation Validation Exception Of Subcomponent Bubble Up To Parent Component

I have a product component with fields: title and description. Inside there there is also a FileUpload subcomponent.

Considering that the FileUpload has a $files variable that is required when the user tries to create a new product without files I want the file validation exception to bubble up to the product component so to cancel the creation of the product.

How can I do this?

class Product extends Component
{
   public $title;
   public $description;

	public function rules()
   {
	    return [
           'title' => 'required',
           'description' => 'required',
        ];
   }
}
class FileUpload extends Component
{
   public $files = [];

	public function rules()
   {
       return [
           'files' => 'required',
       ];
   }
}
May
04
1 week ago
Activity icon

Started a new Conversation Get Parent Relation Of An Object Dynamically

I have three models

Product, Variant and Option.

class Product {
	public $id;

    public function variants(): HasMany
    {
        return $this->hasMany(Variant::class);
    }
}

class Variant {
	public $id;
	public $product_id;

    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class);
    }

    public function options(): HasMany
    {
        return $this->hasMany(Option::class);
    }
}  

class Option {
	public $id;
	public $variant_id;

    public function variant(): BelongsTo
    {
        return $this->belongsTo(Variant::class);
    }
} 

I want to know if there is a way to for an instance of Variant to get parent (Product) relationship and for Option the parent (Variant) relationship with one line of code. Is there anything like the below?

$instance->parent(); 

I want to avoid writing

If (get_class($instance) === 'Variant' ) {
	$instance->product();
} else if (get_class($instance) === 'Option' ) {
	$instance->variant();
} 
May
03
1 week ago
Activity icon

Started a new Conversation Model Factory Increment Value And Reset For Each Test

I have my ProductFactory as below:

class ProductFactory extends Factory
{

    protected $model = Product::class;

    public function definition(): array
    {
        static $order_column = 1;
        return [
            'title' => $this->faker->sentence,
            'order_column' => $order_column++,
        ];
    }

}

This works as it increments the order_column upon creation of two or more tests. However, I want this static variable to be reset for each of my test function ie.

public function testCanCreateProductsOfStatusDraft() {
	Prouct::factory()->count(2);
}
 // This will create
[
	[
	'id' => 1,
	'title' => 'product1',
	'order_column' = 1
	],
	[
	'id' => 2,
	'title' => 'product2',
	'order_column' = 2
	],
]



public function testCanCreateProductsOfStatusComplete() {
	Product::factory()->count(2);
}
 // This will create
[
	[
	'id' => 1,
	'title' => 'product1',
	'order_column' = 3  => I want this to be 1
	],
	[
	'id' => 2,
	'title' => 'product2',
	'order_column' = 4  => I want this to be 2
	],
]
Apr
30
1 week ago
Activity icon

Replied to How To Remove Item From Nested Array Using Collections

Isn't this for arrays with custom keys only?

Activity icon

Started a new Conversation How To Remove Item From Nested Array Using Collections

I have the following nested array structure:

$products = [
  0 => [
    'id' = 12,
    'title' = 'Product12',
    'options' = [
        0 => [
            'id' = 21,
            'title' = 'Option21'
        ],
        1 => [
            'id' = 22,
            'title' = 'Option22'
        ]
    ]
  ]
]

I want to delete option with id='21'. I could use a double foreach, however how can I do it using Collections?

Apr
28
2 weeks ago
Activity icon

Replied to Livewire Does Not Respect The Array's Element Order

Unfortunately not. I got the same results.

Activity icon

Replied to Livewire Does Not Respect The Array's Element Order

Yes it worked. But why this happens? In any case the outer index of the array is integer either with keyBy('id') or not.

How can I do it considering that I want to maintain the keyBy('id') relationship?

Activity icon

Started a new Conversation Livewire Does Not Respect The Array's Element Order

I have an array and trying to pass it to the view.

$products = Product::orderBy('sort_order')->get()->keyBy('id')->toArray();

That returns in php

$products = [
	2 => [
		   "id"=2,
           "title"="Product2",
           "sort_order"=1,
     ],
     1 => [
           "id"=1,
           "title"="Product1",
           "sort_order"=2,
     ]
]

However in blade the order of the array is displayed as:

$products = [
     1 => [
           "id"=1,
           "title"="Product1",
           "sort_order"=2,
     ],
     2 => [
           "id"=2,
           "title"="Product2",
           "sort_order"=1,
     ]
]

How can I tackle this?

Apr
26
2 weeks ago
Activity icon

Started a new Conversation Emulate Database Connection Error On Phpunit

In order to handle the database error connection during a database interaction using eloquent I am using a try - catch statement.

Now I want to turn off the database in one of my phpunit tests to test that behavior. How can I do that?

Apr
22
3 weeks ago
Activity icon

Started a new Conversation Test A Notification Is Shown That Is Outside Of Livewire Component

I have a module Product\Update that when is updated an event is dispatched using

        $this->dispatchBrowserEvent('notify', '`Product updated successfully.');

This event is send to the browser and a toaster notification is poped-up. The toaster notification is outside of the Product\Update component but in the html <body>.

As I checked the below code fails because it only checks the code inside the Livewire module while my toaster is outside of it.

Livewire::test(Product\Update::class)
            ->set('title', 'Mike')
            ->call('submitForm')
			->assertSee('Product updated successfully.');

How can I test this behavior?

Activity icon

Replied to Override Eloquent's Save Method On Pivot Table

Ok so I guess in that case using the below pattern is unavoidable, right?

$categoriesArray = [ 
    1 => ['updated_by' => Auth::id()],
    2 => ['updated_by' => Auth::id()],
    ...
];
$product->categories()->sync($categoriesArray); 
Apr
16
3 weeks ago
Activity icon

Replied to Override Eloquent's Save Method On Pivot Table

Thanks for the reply @martinbean. Observer works great when saving a Product (using a ProductObserver) or a Category (using a CategoryObserver) however when I am using

$product->categories()->sync($categoriesArray); 

The ProductCategoryObserver is not triggered. Why is this happening?

Apr
15
4 weeks ago
Activity icon

Started a new Conversation Override Eloquent's Save Method On Pivot Table

I have three tables: Products, Categories and a pivot one CategoryProduct.

I have an attribute updated_by and I want this to be automatically set to Auth::id() upon all create/update requests.

So far I am able to save data by using the sync() method ie.

$categoriesArray = [ 
    1 => ['updated_by' => Auth::id()],
    2 => ['updated_by' => Auth::id()],
    ...
];
$product->categories()->sync($categoriesArray); 

I want to simplify the above by overriding the save method on CategoryProduct model ie.

class CategoryProduct extends Model
{

    protected $table = 'category_product';
    protected $fillable = ['product_id', 'category_id', 'created_by', 'updated_by'];

    public function save(array $options = array())
    {
        $this->updated_by = $this->updated_by ?: Auth::id();

        parent::save($options);
    }
}

So that I can save my categories like so:

$categoriesArray = [1, 2, 3];
$product->categories()->sync($categoriesArray); 

However for some reason the save method is not triggered upon saving resulting in a MySQL error. General error: 1364 Field 'updated_by' doesn't have a default value.

Any ideas?