Mock Config in Laravel

Hi all. If you are testing your application and you need to make your config constant or change it to a specific value for testing, you can mock it simply! The Config facade of Laravel has a method named set() which overwrites the default value of configs like this: Config::set('name', 'Laravel'); So in a testing environment, you can do something like this: public function test_that_home_page_is_working() { Config::set('name', 'Laravel'); $this->get('/')->assertSee("Laravel"); } Generally, Laravel facades have several benefits because of their testability. You can Mock all facades of Laravel easily. Also, if you want to have deeper knowledge about testing tools, you can take a look at PHP Mockery.

September 3, 2022 · 1 min · Hamid

Bootstrap open Accordion after Ajax loaded

Accordion is a nice component of Bootstrap. It uses the Collapse feature of Bootstrap. It works properly under the hood, but sometimes you need to do something different. In my case, I needed to click on the accordion header, then send an Ajax request to get the content of the accordion, and then open it. There was not a straightforward API or documentation for that, and finally I found it. <script> let prevent = true; $("#collapseThree").on("show.bs.collapse", function (e) { if (prevent) { e.preventDefault(); } setTimeout(function () { prevent = false; $("#collapseThree").collapse("show"); prevent = true; }, 1000); }); </script> In this example, I’ve added an event listener on the open state of collapse and I’ve used a setTimeout function to simulate an Ajax request.

June 4, 2022 · 1 min · Hamid

Local Storage for shopping cart in Javascript

In a previous exercise class with students, we created a simple shopping cart with JS. Today I had another lecture and I added a localStorage feature to this project. In this version of the project, the added items can persist in the cart after a refresh and we added another file named cart.html that shows the added cart items. You can see the source code of this project in the v2.0.0 tag in this repo: JsShoppingCart ...

May 23, 2022 · 2 min · Hamid

Simple Shopping Cart with pure Javascript

I’m a code mentor and private programming tutor. Today, my student and I were working on event listeners in JS and I tried to teach how to make a simple and minimal shopping cart. If you are learning JS, this could be a good exercise. You can find the code here. And you can see a demo here. In this work, we added our products like this: <div class="product"> <h3>Product one</h3> <p>Price: 2000</p> <button data-id="1" data-price="2000">Add to cart</button> </div> We know that in the real world we do not do this manually and we generate them with loops using data that we have retrieved from a database. ...

May 18, 2022 · 2 min · Hamid

Simple exercise for Javascript Ajax

I have about 4 years of experience working as a private tutor of programming. About five years ago, I published a blog post on my website in Persian language entitled “Private Laravel Tutor” and people started to call me. I always love teaching people and it was a good opportunity. After a while, the number of students increased and today I have two sessions per day on average. Most of my previous students are now working around the world. ...

May 14, 2022 · 2 min · Hamid

Activate links of Laravel Blade based on current route name

Consider a navigation menu with a bunch of links and you are trying to activate them based on the current active route name. In a normal case, you have to return the currentRouteName() from the controller or maybe in a view composer or any other place. I have written a simple Laravel composer package that makes it a little bit simpler. You can take a look at it here: https://github.com/hamidhaghdoost/active. For installation, use the composer require command like this: ...

May 13, 2022 · 1 min · Hamid