Eloquent: Recursive hasMany Relationship with Unlimited Subcategories

Quite often in e-shops you can see many level of categories and subcategories, sometimes even unlimited. This article will show you how to achieve it elegantly with Laravel Eloquent.

We will be building a mini-project to views children shop sub-categories, five level deep, like this:


Database Migration

Here’s a simple schema of DB table:

Schema::create('categories', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->unsignedBigInteger('category_id')->nullable(); $table->foreign('category_id')->references('id')->on('categories'); $table->timestamps(); }); 

We just have a name field, and then relationship to the table itself. So most parent category will have category_id = NULL, and every other sub-category will have its own parent_id.

Here’s our data in the database:


Eloquent Model and Relationships

First, in app/Category.php we add a simple hasMany() method, so category may have other subcategories:

class Category extends Model { public function categories() { return $this->hasMany(Category::class); } }

Now comes the biggest “trick” of the article. Did you know that you can describe recursive relationship? Like this:

public function childrenCategories() { return $this->hasMany(Category::class)->with('categories'); }

So, if you call Category::with(‘categories’), it will get you one level of “children”, but Category::with(‘childrenCategories’) will give you as many levels as it could find.


Route and Controller method

Now, let’s try to show all the categories and subcategories, as in the example above.

In routes/web.php, we add this:

Route::get('categories', 'CategoryController@index');

Then, app/Http/CategoryController.php looks like this:

public function index() { $categories = Category::whereNull('category_id') ->with('childrenCategories') ->get(); return view('categories', compact('categories')); } 

As you can see, we’re loading only parent categories, with children as relationships. Simple, huh?


View and Recursive Sub-View

Finally, to the Views structure. Here’s our resources/views/categories.blade.php:

<ul> @foreach ($categories as $category) <li></li> <ul> @foreach ($category->childrenCategories as $childCategory) @include('child_category', ['child_category' => $childCategory]) @endforeach </ul> @endforeach </ul>

As you can see, we load the main categories, and then load children categories with @include.

The best part is that resources/views/admin/child_category.blade.php will use recursive loading of itself. See the code:

<li></li> @if ($child_category->categories) <ul> @foreach ($child_category->categories as $childCategory) @include('child_category', ['child_category' => $childCategory]) @endforeach </ul> @endif

As you can see, inside of child_category.blade.php we have @include(‘child_category’), so the template is recursively loading children, as long as there are categories inside of the current child category.


And, that’s it! We have unlimited level of subcategories – in database, in Eloquent relationships, and in Views.

via Laravel Daily
Eloquent: Recursive hasMany Relationship with Unlimited Subcategories

Security Researcher’s ‘NULL’ Vanity Plates Cause Glitch That Lands Him $12,000 in Parking Tickets

You can’t put a price tag on the value of sticking it to the man—specifically, trying to own an official surveillance system. Except maybe when you are owned right back.

A security researcher going by the name of Droogie attempted to fuck with Automated License Plate Reader (ALPR) systems by customizing a vanity California license plate with the word “NULL.” The word was a play on the term used in programming to indicate no value, and Droogie hoped that it would mess with the systems.

ALPR systems involve surveillance cameras mounted on cop cars and around cities that collect a vehicle’s license plate as well as the time, date, and location. And the collection of this data isn’t relegated to suspects—anyone’s vehicle is subject to being surveilled and subsequently loaded into a comprehensive database with robust information on their real-time whereabouts. It’s also presently unclear all the ways in which government agencies and private companies are using (and potentially abusing) this data.

Droogie presented his misadventure at the DEF CON security conference in Las Vegas on Saturday, in which he explained that while his customized plate was successful in confusing the systems, it resulted in thousands of dollars of other people’s parking tickets.

When he went to renew his tags on the DMV website, the website informed him that his license number was invalid, Mashable reported. Droogie then started receiving thousands of dollars in parking tickets, totaling over $12,000, addressed to him. He told the convention audience that a processing center was assigning “NULL” to vehicles with outstanding tickets that the DMV didn’t have license plate information on. And so Droogie received all of those unassigned tickets.

The DMV and LAPD reportedly told him to change his plates (he didn’t), but eventually the processing center erased the fines that were mistakenly sent to Droogie. But the system is still operating as it was prior to the erasure of his fines, and the system has since assigned him over $6,000 more in tickets.

“I was like, ‘I’m the shit, I’m gonna be invisible,’” Droogie told the crowd at DEF CON, Mashable reported. “Instead, I got all the tickets.”

Droogie’s stunt is a powerfully comedic statement about a mass surveillance system being deployed without the consent of civilians, and until recently, with little to no oversight. In June, a California legislative committee voted to enforce a state audit into ALPR systems used by cops. Leading up to that, reports indicated that these systems were being exploited by Immigration and Customs Enforcement and potentially other government agencies and that agencies often obfuscated the honest accounts of how the data gleaned from these readers was being used.

“These systems wouldn’t work at all if the government did not require drivers to post identifying numbers in public view,” the Electronic Frontier Foundation wrote in a blog post. “But unlike an officer writing down plate numbers by hand, the collection and storage on a massive, automated scale can reveal intimate details of our travel patterns that should be none of the government’s business.”

via Gizmodo
Security Researcher’s ‘NULL’ Vanity Plates Cause Glitch That Lands Him $12,000 in Parking Tickets

Archaeological dig in Jerusalem unearths evidence of biblical Babylonian conquest

Shimon Gibson, co-director of the Mount Zion Archaeological Project, sets the scene at the Jerusalem site. (GeekWire Photo / Alan Boyle)

One month after offering up archaeological evidence to back up a contested claim about the First Crusade, researchers say they’ve found traces of the Babylonian conquest of Jerusalem in a deeper layer of their excavation on Mount Zion.

The newly reported find demonstrates how the site, just outside the walls of the Old City’s Tower of David citadel, serves as a “time machine” documenting the twists and turns of Jerusalem’s history.

The Babylonian conquest, which dates to the year 587 or 586 BCE, is one of the major moments of Jewish history. As detailed in the biblical Book of Kings, the Babylonians laid siege to Jerusalem for months, eventually broke through the walls and burned “all the houses of Jerusalem,” including Solomon’s Temple.

After the fall of Jerusalem, the Jewish people were sent into exile – an event that Jews commemorate with mourning and fasting every year on the ninth day of the Hebrew month of Av. This year’s Tisha B’Av observance began at sundown tonight.

During this year’s Mount Zion excavation, which is managed by the University of North Carolina at Charlotte, researchers came across a layer of material that included burnt wood and ash, potsherds, lamps and arrowheads dating from the time of the conquest. They also found a piece of period jewelry – a tassel or earring made of gold and silver.

A tassel or earring made of gold and silver was found amid a layer of ashen material linked to the Babylonian conquest of Jerusalem in the sixth century BCE. (Mount Zion Archaeological Expedition  / Rafi Lewis)

An area around the dig yielded up signs of what appears to have been a substantial Iron Age structure, but that particular area has yet to be fully excavated.

Today, the focus of the Mount Zion Archaeological Project is a triangular wedge of ground that lies between a busy Jerusalem thoroughfare and the citadel’s walls. But at the time of the Babylonian siege, the area was a neighborhood within the fortifications of the Iron Age city, said Shimon Gibson, a co-director of the excavation and a professor of history at UNC Charlotte.

By itself, the ash wouldn’t serve as conclusive evidence of the city’s biblical destruction, Gibson said.

“For archaeologists, an ashen layer can mean a number of different things,” he explained in a news release. “It could be ashy deposits removed from ovens, or it could be localized burning of garbage.”

But the fact that the ash and debris were mixed with Scythian-type bronze and iron arrowheads that date to the sixth century BCE, plus the presence of a precious bauble from the same era, helps strengthen the case.

“Nobody abandons golden jewelry, and nobody has arrowheads in their domestic refuse,” Gibson said.

The clay artifacts, which include the high-based pinched lamps that were common at the time of the siege, help to date the deposit.

“It’s the kind of jumble that you would expect to find in a ruined household following a raid or battle,” Gibson said. “Household objects, lamps, broken bits from pottery which had been overturned and shattered … and arrowheads and a piece of jewelry which might have been lost and buried in the destruction.”

Miles Shen with lamp
One of the students of UNC Charlotte’s Levine Program, Miles Shen, holds a lamp dating from the Iron Age (Mount Zion Archaeological Expedition / James Tabor)

Gibson said he likes to think that the site represents one of the “great houses” mentioned in 2 Kings 25:9. “This spot would have been at an ideal location, situated as it is, close to the western summit of the city with a good view overlooking Solomon’s Temple and Mount Moriah to the northeast,” he said.

But he and the rest of the team are proceeding carefully.

“We are slowly taking the site down, level by level, period by period, and at the end of this last digging season, two meters of domestic structures from later Byzantine and Roman periods have still to be dug above the Iron Age level below,” Gibson said. “We plan to get down to it in the 2020 season.”

Excavation co-director Rafi Lewis, an archaeologist at Ashkelon Academic College and Haifa University, said the historical context that surrounds the discoveries at the Mount Zion site adds to the project’s appeal.

“It is very exciting to be able to excavate the material signature of any given historical event, and even more so regarding an important historical event such as the Babylonian siege of Jerusalem,” he said in the news release.

This season’s other notable discovery relates to another battle at Jerusalem’s walls – the siege by the Crusaders in 1099. The UNC Charlotte team excavated the outlines of a defensive ditch that, combined with artifacts including arrowheads, crucifixes and a piece of jewelry found amid a layer of burned debris, served to confirm a long-debated chapter in the saga of the First Crusade.

The team also uncovered vaulted basements dating from the time of Herod the Great, and a Byzantine street that was the continuation of a thoroughfare in Jerusalem known as the Cardo Maximus. Previous finds at the site include a rare gold coin bearing the visage of Nero as a young Roman emperor, and a first-century Jewish mansion that came complete with a bathtub.

The Mount Zion archaeological project is conducted by researchers, students and volunteers under the direction of Gibson, Lewis and James Tabor, a professor of religious studies at UNC Charlotte. The site is within an Israeli national park near the Old City’s Zion Gate. The project’s sponsors include Aron Levy, John Hoffmann, Cherylee and Ron Vanderham, Patty and David Tyler. Sheila Bishop of the Foundation for Biblical Archaeology is a facilitator for the project. 

GeekWire’s Alan Boyle traveled to the Mount Zion site last month as part of a field trip associated with the World Conference of Science Journalists and organized by CERN with support from the Open SESAME consortium.

via GeekWire
Archaeological dig in Jerusalem unearths evidence of biblical Babylonian conquest

Napolitano: The 2nd Amendment Isn’t A Gift From The Government


AP Photo/Richard Drew

Judge Andrew Napolitano is bringing the heat in a stinging column for the Washington Times, rebutting and refuting the arguments by gun control activists that they can simply ignore the text of the 2nd Amendment and Supreme Court decisions and enact whatever anti-gun laws they want.

The U.S. Supreme Court has twice ruled in the past 11 years that the right to keep and bear arms is an individual pre-political liberty. That is the highest category of liberty recognized in the law. It is akin to the freedoms of thought, speech and personality. That means that the court has recognized that the framers did not bestow this right upon us. Rather, they recognized its pre-existence as an extension of our natural human right to self-defense and they forbade government — state and federal — from infringing upon it.

It would be exquisitely unfair, profoundly unconstitutional and historically un-American for the rights of law-abiding folks — “surrender that rifle you own legally and use safely because some other folks have used that same type of weapon criminally” — to be impaired in the name of public safety.

It would also be irrational. A person willing to kill innocents and be killed by the police while doing so surely would have no qualms about violating a state or federal law that prohibited the general ownership of the weapon he was about to use.

The judge is absolutely right. And frankly, we know that even if an “assault weapons ban” managed to get millions of legally owned guns out of the hands of Americans (which it wouldn’t), the monsters in our midst would just adapt. We just saw a twisted individual take more than 30 lives by setting fire to a studio in Japan. We’ve seen a wanna-be jihadi mow down 86 people and injure more than 400 with a truck in France. We don’t think we’d see new tactics used by the twisted individuals here at home intent on destroying as many innocent lives as possible?

The judge also had a strong rebuke to those proposing or supporting red flag laws.

The president also offered his support for “red flag” laws. These horrific statutes permit police or courts to seize guns from those deemed dangerous. Red flag laws are unconstitutional. The presumption of innocence and the due process requirement of demonstrable fault as a precondition to any punishment or sanction together prohibit the loss of liberty on the basis of what might happen in the future.

In America, we do not punish a person or deprive anyone of liberty on the basis of a fear of what the person might do. When the Soviets used psychiatric testimony to predict criminal behavior, President Ronald Reagan condemned it. Now, the president wants it here.

We have civil commitment laws on the books in states across the nation. If we believe that someone is truly a danger to themselves or others, isn’t that a more appropriate place to seek help rather than a court hearing where a judge only hears one side of the case before making a determination about whether or not someone poses a threat to themselves or others?

As Napolitano points out, if one of our rights is gutted by ignoring the Constitution, you set a precedent to the same to any or all of the rest. If you think the 2nd Amendment is old or outdated, don’t ignore the Constitution. Abide by it. Try to repeal the 2nd Amendment. Of course that’s hard, which is one reason we haven’t seen a serious effort. We’re also dealing with some folks who really don’t care much about the Constitution. It’s an old and outdated document in their opinion, which should be ignored whenever possible and completely changed at the first given opportunity. Unfortunately that means that Napolitano’s column, as good as it is, will likely fall on deaf ears.

The post Napolitano: The 2nd Amendment Isn’t A Gift From The Government appeared first on Bearing Arms.

via Bearing Arms
Napolitano: The 2nd Amendment Isn’t A Gift From The Government

4 Pro-2A Websites You Should Subscribe To, Join or Visit Everyday

Opinion

Pro Gun 2A Iceberg Second Amendment iStock-972964808
With your support, each one of these organizations is an anti-gun titanic killer.

USA – -(AmmoLand.com)- These pro-gun websites need the support of gun owners to exist. Without us, they would not be able to survive. Each one does an immeasurable amount of good in preserving our rights. Please visit each one and subscribe to their email lists as it is the only way they can keep in touch with you and not have big-tech shadowban them out of view.

Crime Prevention Research Center: www.crimeresearch.org

Crime Prevention Research Center
Crime Prevention Research Center

Anti-gunners hate this group with a passion that alone is a reason to support them. The only tool we have is facts, and I’ll be honest. I use his data for a lot of my articles.
When you hear that the U.S. has the most mass shooting and that we are a crazed gun culture, the proof that this is a lie comes from Dr. John Lott. (The U.S. ranks 11th in mass shooting among industrialized nations and 64th overall, here is the link.)

The Second Amendment Foundation: www.saf.org

Second Amendment Foundation
Second Amendment Foundation

The Heller and McDonald rulings from the SCOTUS were initiated and driven by the 2nd Amendment Foundation. On a national level, these guys are the top dog. They don’t really lobby or do community activism, but they do smack the anti-gunners in court, and they’re the best there are.

The list of legal cases they’re involved in their list of wins dwarfs almost every other pro-gun group.

The Friends of the NRA: www.friendsofnra.org

The Friends of the NRA
The Friends of the NRA

A true grassroots organization, and insulated from the much of the politics of the NRA itself the FNRA is the charity arm of the NRA.

I chair the Los Padres chapter in Ventura County. So from personal experience, here is what the FNRA funds. The Boy Scouts, JROTC, and Women on Target.. etc.
The FNRA’s goal is to grow the shooting sports and bring new people into the sport of shooting.

The Friends of the NRA Achievement Numbers 2019
The Friends of the NRA Achievement Numbers 2019

In other words, the FRNA invests in our future.

Most important, the allocation of monies for the grants is done on a local basis; and the FNRA ranks as having one of the lowest ratios of operating cost to grant allocation. (more money is spent on grants than most any other charity.)

So pick an event and attend (4 million other people have) or join the 13,000 volunteers that make these events possible.

AmmoLand News: www.ammoland.com

AmmoLand News
AmmoLand News

Very quietly over the past few years, AmmoLand News has become a leading voice in the shooting community. AmmoLand News has almost 4 million unique monthly visitors and is steadily growing. Covering all the important topics that the mainstream media refuses to touch, they are one of the only true news outlets that have not been corrupted. Even the biased MediaBiasFactCheck.com reports that AmmoLand News’ Factual Reporting as: HIGH

So if you’re not subscribed to AmmoLand News you should, you’ll find news and opinion here that is untainted by bias and open to all viewpoints. Share this and other articles you like here, and encourage others to sign up as well. They are one of these organizations that is most at risk of censorship by google, facebook, and social media overlords.

Share this article on your social media.

There are a lot of groups you can belong to; some are little more than websites, others nothing but scams that drain funds from the groups that actually do the work. These four, however, will make a difference. Get involved with any of these groups, and you too will help make a difference.

Every one of the leading Democrat presidential candidates has called for gun bans and confiscation. The time to get involved is now. A small effort today equals the preservation of our rights tomorrow.


About Don McDougall

Don McDougall
Don McDougall

Don McDougall is an NRA instructor and member of the Los Padres “Friends of the NRA” committee. If he’s not at the range, you will find him setting the record straight with on gun issues and gun safety on AmmoLand Shooting Sports News.

The post 4 Pro-2A Websites You Should Subscribe To, Join or Visit Everyday appeared first on AmmoLand.com.

via AmmoLand.com
4 Pro-2A Websites You Should Subscribe To, Join or Visit Everyday