Changing Laravel’s default public directory

Sometimes on the servers I work on I need to change the “public” directory to “content.” To do this I create my own “Application.php” class, extending Laravel’s Application class and override the publicPath method.

Next, I update bootstrap/app.php and create the $app instance using my Application class.

I don’t use server.php but, if you did, you’d need to update the following lines in your public directory’s index file too:

Creating an on page Delete Link for a Laravel record in jQuery

*My code snippets plugin seems to be crapping out. Apologies for the formatting. Will try to fix soon.

When creating CRUD pages we’ll eventually need to create delete links for records. In Laravel these links need to fire a post request to an endpoint with two fields:

  • _method which is DELETE
  • _token which has the csrf token

Here’s bit of jQuery code I put together for a recent project to help with this. Once in place, you’ll only need to create a delete link and give it the data-delete attribute with the value of the URL endpoint. Here’s how to use it:

and here is the Javascript to make it all work:

Borderlist – I made a thing

So listen, I made a thing. Yes, it is another to do app thingie but this one is built a bit differently. I work from so many lists on any given day that it is difficult to see everything at a glance.

I want to

  • See multiple lists at a time around the same topic.
  • Make a list, add some items, get stuff done, tick’em off.
  • Delete lists and clear my boards.
  • See it all on my phone without having to download yet another app.

No bells and whistles. No fluff.
I want a disposable Post-It Note system without all the sticky pads.

With these goals in mind I built Borderlist. It’s free, simple, and I use it every day.

Visit Borderlist

Because this is a tech blog, here are some nerdy details. The front end is built with Bulma (CSS) and, once in an account, it is all Vue using a Laravel based API to handle the data.

If you decide to give it a try, let me know what you think.

Setting Up, Creating and Updating a hasOne Relationship in Laravel

Not a lot on creating and updating a hasOne record so here’s what ended up working for me. I have my Site model and a SiteCache. SiteCache should have a single record per Site.

SiteCache migration (the Site migration isn’t that important so I omitted it here)

<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateSiteCachesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('site_caches', function (Blueprint $table) { $table->longText('json_content'); $table->unsignedInteger('site_id'); $table->foreign('site_id')->references('id')->on('sites')->onDelete('cascade'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('site_caches'); } }
create_site_caches_table.php

Site Model

<?php namespace App; class Site extends Base // base is a model that I extend, nothing of note in it { public function cache() { return $this->hasOne(SiteCache::class); } public function setCacheContent($content) { $data = ['json_content' => $content]; if($cache = $this->cache()->first()) { $cache->update($data); } else { $this->cache()->save( new SiteCache($data) ); } return $this; } }
Site.php

SiteCache Model

<?php namespace App; class SiteCache extends Base { public function site() { return $this->belongsTo(Site::class); } }
SiteCache.php

In use

<?php $site = Site::first(); $site->setCacheContent($json);
inUse.php

The creating and updating of the cache record on the SiteCache page isn’t as fluent as I’d like but with a little helper method on the Site model all is good.

Using Foreign Keys with SQLite in Laravel

When using SQLite for in-memory tests, I discovered that foreign keys are not used by default. Not so bad until you test cascading deletes and cry rivers of ones and zeros… Do I really have to make a test database?! I thought this was America! WHHHYYYYYYYYYYY?!?!?!?!?!?!

With a bunch of Googling I found that it is possible to enable foreign keys in SQLite. They are just turned off by default to cause sadness and confusion. To enable, you can use the code below.

if (DB::connection() instanceof Illuminate\Database\SQLiteConnection) { DB::statement(DB::raw('PRAGMA foreign_keys=1')); }

But where to put it? I don’t want that thing chilling in my app. I only need it for testing at the moment. I extend the TestCase class for all my tests so I dropped it in the createApplication method like so:

abstract class TestCase extends Illuminate\Foundation\Testing\TestCase { protected $baseUrl = 'http://localhost'; public function createApplication() { $app = require __DIR__.'/../bootstrap/app.php'; $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); // when using sqlite, allow foreign keys if (DB::connection() instanceof Illuminate\Database\SQLiteConnection) { DB::statement(DB::raw('PRAGMA foreign_keys=1')); } return $app; } }
TestCase.php

I hope that helps someone. Here’s one of the articles that I pulled code from.