Rants and Raves

Welcome to Rants and Raves! I kept hearing that I needed to write, so here is the blog that I ended up starting. It is mostly an accounting of my progression through creating a web site, nuances with code, and tips about what I did to make things work. In there you will find sprinkled about some gems of life, and letting loose fun. Enjoy!

Trucks Aren't Supposed To Tilt That Much
2023-01-04
Got to see something cool today. The whole truck and trailer was tilted to a 45 degree angle to unload a bunch of grain. We aren't really setup to have that kind of movement, but they gave us warning. We were able to move a bunch of stuff around to be supported during the process, but we didn't get everything. Luckily the rest was little and we didn't really have any casualties. It would have been really cool if we had been inside, but they don't allow that.
HTML Dropdown Selects
2023-01-04
Started making use of dropdown selects. https://getbootstrap.com/docs/5.2/forms/select/ In my options I did make use of the hidden attribute for an un-selectable default prompt option. https://www.geeksforgeeks.org/how-to-set-the-default-value-for-an-html-select-element/ I also added a check to see if there already was a value so as to set the selected attribute onto the correct option. https://stackoverflow.com/questions/22822427/bootstrap-select-dropdown-list-placeholder If you also have an HTML input that you've disabled, but still has critical info, there are ways to make sure that the info is available in the back end. My default code ended up with a NULL value for the field, but I opted to check in the controller to determine if I wanted to use the existing DB info, or the request info. https://stackoverflow.com/questions/4045648/submitting-the-value-of-a-disabled-input-field In the controller I did end up wanting to grab just a small portion of DB info, and that is nicely done using the pluck() method. https://laravel.com/docs/9.x/collections#method-pluck If you have a column in the DB that you would like to rename, that is easily done with the renameColumn() function in the migrations. Just make sure your code has been updated to handle the new column name. https://laravel.com/docs/9.x/migrations#renaming-columns And doing MySQL commands, a little reminder on the basic format for the queries: SELECT column FROM table WHERE condition https://www.w3schools.com/mysql/mysql_sql.asp I am getting excited though, I am pretty close to having a functioning site that others will be able to use. There are still a lot of potential improvements to make, but the basic level will be there. Hopefully I will be able to a beta client to help it along.
On The Road Again With Hidden Modes
2023-01-03
After spending some time with family for the holidays we are back on the road. Kicked that off with some audio books, coding, and video games. Just beat Final Fantasy 2 and discovered that in the version I have there is a now a mode called Soul of Rebirth. Basically it is playing as the fallen characters as they make their way through the passage to hell. It is a bit of a challenge as these characters are relatively low on stats, and your previous item store is gone. I do look forward to seeing how that story goes.
Route Parameters And DB Connections
2023-01-02
Not a lot to write about today. I did have a "fancy" route issue where I wanted the route information to be passed to the controller, this can be accomplished via route parameters: https://laravel.com/docs/9.x/routing#route-parameters You will need to be sure to add the route parameter, that is defined in the web route, as a variable that the controller function is expecting to receive: Route::resource('/path/{var}/path', ModelController::class); public function create($var){} I did run into some more issues with using multiple databases, in saving a model to the database. Since this particular model, most models I assume, will always use the same DB I made a dedication connection in the DB config file: 'mysql2' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => 'dbName', 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), ], and set the connection in the model to the new connection: class ModelName extends Model { protected $connection = 'mysql2'; } https://stackoverflow.com/questions/28985472/change-database-connection-in-laravel-model
......
2023-01-01
asd;lkja ;l-popoa l;jwieuucnalk, asdklfj owiujd oudn a si lskdjf..... sa jkkkdiuuw. ... asd oqwu tyask ooiduk al... l;a .... asdfoo woilkh qowueulsl........ asdlfjj;.... asdflll aerfdfkkiu asli aopw 4ju7dj aio9s, iuw92i4 9jjdna.... fs a alsoood, aopp; aisjhfn aki as wkke folj alscb...... aflkjhe aski kasdkuu l;. oiagw p;a lkkjjd !
NEW YEAR'S RAAAAAVE!!!!
2022-12-31
ONE TWO THREE FOUR! UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA UNSCKA
Tables And Forms
2022-12-30
Just a little reminder if you want to user multiple gate checks use @canany: https://laravel.com/docs/9.x/authorization#via-blade-templates There are a lot of Bootstrap style options for tables: https://getbootstrap.com/docs/5.2/content/tables/ If you want to make an entire row of a table a hyperlink with just CSS you can by utilizing the "stretched-link" class. It did also require use of the style "transform: rotate(0)" class. I ended up with: <tr style="transform: rotate(0);"> <td> <a href="link" class="stretched-link"></a> Table content. </td> </tr> https://stackoverflow.com/questions/17147821/how-to-make-a-whole-row-in-a-table-clickable-as-a-link https://getbootstrap.com/docs/5.2/helpers/stretched-link/ Having the disabled HTML attribute comes in really handy when utilizing the same page for display and edit: https://getbootstrap.com/docs/5.2/forms/form-control/#disabled And using a little bit of PHP to set a variable you can make it dynamic: https://stackoverflow.com/questions/13002626/how-to-set-variables-in-a-laravel-blade-template Then you could use something like: @if($mode === 'show') @php ($disabled = 'disabled') @else @php ($disabled = '') @endif <input {{ $disabled }}> if you would like to know what the current or previous URL is, Laravel has a function for that: https://laravel.com/docs/9.x/urls#accessing-the-current-url There seems to be a lot of database manipulation, so do remember that Laravel does have a lot of options there for your migrations: https://laravel.com/docs/9.x/migrations If you have a lot of check boxes and want them to be horizontal use Bootstrap's grid system and wrap the "form-check" HTML in a <div class="col">. It does not work if you try to add the "col" class to the same <div> as the "form-check" class: https://getbootstrap.com/docs/5.2/forms/layout/ To make your form input data a little more easy to loop through when trying to update a model and save to the DB you can use the name attribute of the input element as an array by adding [] to the end of the name. To loop you will need to use the same name for the different inputs: https://hackthestuff.com/article/how-to-store-multiple-checkbox-value-in-database-in-laravel8 If using a "form-check" input then the data returned with a form submission won't contain anything for a false Boolean, and you will need to perform a check to set it manually in the controller with "isset()": https://laracasts.com/discuss/channels/laravel/add-a-checkbox-field-to-user-laravel8 If you have models that have relationships to other models you can save all the data in one go with "->push()" instead of "->save()": https://laravel.com/docs/4.2/eloquent#insert-update-delete
Laravel Migrations Check Existence Of DB, Table, Column, And Foreign Key
2022-12-29
On a continual note of having your migration tables up to snuff, I've had a lot of frustrations with making changes to the DB and then trying to merge the changes back into just a few files. It never seems to fail that when I do there are errors about shit already existing, and I spend time turning them into comments to get the migrations completed just to un comment them for a fresh build. I highly recommend utilizing Laravels hasTable() and hasColumn() functions: https://laravel.com/docs/9.x/migrations#checking-for-table-column-existence Also, checking for the existence of a database: https://stackoverflow.com/questions/33432752/laravel-5-1-checking-a-database-connection https://laravel.com/docs/9.x/database#using-multiple-database-connections The really tricky one came with foreign keys. Laravel does not have a built in function to check the existence of a foreign key, from what I have found. However, it turns out it isn't too hard to make your own function to check this. I utilized the base concepts from this post: https://stackoverflow.com/questions/30504939/check-if-a-unique-index-key-exists-in-laravel From there I made my own function: public function hasForeignKey($table, $column, $db=NULL){ if($db){ DB::purge('mysql2'); \Config::set(['database.connections.mysql2.database' => $db]); } else { DB::purge('mysql2'); \Config::set(['database.connections.mysql2.database' => 'database.connections.mysql.database']); } $keys = DB::connection('mysql2')->select(DB::connection('mysql2')->raw("SHOW KEYS from $table")); $foreign = "$table"."_"."$column"."_foreign"; foreach($keys as $key){ if($key->Key_name == $foreign){ return TRUE; } } return FALSE; } Then call the function where you need it to produce a bool result: if( CreateReferences::hasForeignKey('items', 'model_id', 'demo')) {} It requires a table and column name, and optionally a database name. If a DB is supplied then it will alter the connection to the correct database. Then it pulls a list of the keys (primary, unique, foreign, etc.) from the DB. It assumes the Laravel naming convention of foreign keys to make name to compare against. Then it loops through all the returned keys and checks to see if the Key_name matches the compiled comparison key. It returns a simple bool, true if the comparison was found, false if no match was made. I did have to tell what class the function was stored in when I called it, which was in the same migration table that I was trying to call it from. I'm sure there is a better place to put the function. Offhand it seems to work well enough though.
Work With Foreign Keys With WhereRelation()
2022-12-28
What a pain things are sometimes. I tweaked a few things trying to get the demo site up and running, Some easier than others. I added a route for an informational page, and did not think it needed the full resources of a controller so I used a simple closure similar to this: https://stackoverflow.com/questions/26086710/how-to-return-routes-to-views-in-laravel I also couldn't remember details of how to properly style a Bootstrap button, but got sorted with this: https://getbootstrap.com/docs/5.2/components/buttons/ Another quick do I wanted was to get just a single value from the database without having to deal with a collection that only contains one group of items, but using the ->first() modifier worked like a charm: https://laracasts.com/discuss/channels/eloquent/how-to-select-single-field-and-get-a-single-result-with-eloquent-no-id-know I wanted to use tabs on a page and found a great starting point with W3 Schools: https://www.w3schools.com/bootstrap5/bootstrap_navs.php which I did tweak a little to match Bootstrap's documentation: https://getbootstrap.com/docs/5.2/components/navs-tabs/ My biggest frustration was with trying to figure out how to get the items in a table that hasMany of another table that has the filer. TableA hasMany() of TableB. TableB needs filtered on a column. Need to use filtered TableB to get a list of TableA. I know there's ways to go about this that involve a simple/complex query to get the filtered TableB and then loop through to compile a new list of TableA values. But it seemed to me that using multiple tables and foreign keys in this way would be common enough that Laravel should have a solution. Eventually I found what I was looking for, whereRelation(): https://www.amitmerchant.com/the-whererelation-method-in-laravel-8x/ https://laravel.com/docs/9.x/eloquent-relationships#inline-relationship-existence-queries
Demo Site Framework
2022-12-27
Spent more time prepping the database/models/controllers/migrations/seeders for the next phase, getting a demo site working. Still just framework kind of stuff, but it requires trying to anticipate the full operational structure of the site. One aspect I needed to see if I could get working was the ability to use multiple databases, this would direct the project. Short answer, you can, two different ways. One is to simply setup a second database in the database config file, the other is to modify the database in the code. Some good information/options for utilizing a second database can be found here: https://stackoverflow.com/questions/31847054/how-to-use-multiple-databases-in-laravel I have been favoring the on('database') Eloquent modifier for the time being. For what I am planning I would like to actively change the database in code execution. I used info from this: https://stackoverflow.com/questions/46775265/change-the-database-connection-dynamically-in-laravel I also found the purge command very useful when doing a lot after a database change without needed to set the config every time. On a similar note, I found an interesting read about the dangers of trying to use the config() command inside of a config file here: https://blog.maqe.com/dont-use-laravel-s-config-inside-config-files-40e2c8207225 In the vein of automation and needing more databases I found this page that gives a command that can be run in the migrations to create a new database: https://stackoverflow.com/questions/36931411/laravel-5-2-create-database-from-migration Also in the migrations I needed to drop a column that was not actually necessary: https://laravel.com/docs/9.x/migrations#dropping-columns Remove the unique key from a column, which will require the removal of the foreign key if it has one... which you then have to add back in if you still want the foreign key: https://stackoverflow.com/questions/49811807/how-to-remove-unique-constraint-from-a-column-using-laravel-migrations add foreign keys between database tables: https://stackoverflow.com/questions/28950070/is-it-possible-to-reference-a-foreign-key-in-a-different-database-in-laravel When setting up a table that is going to be a foreign key, make sure that it is nullable otherwise the default value might cause the foreign key to fail. Figured that out from the comments on this page: https://stackoverflow.com/questions/14063652/integrity-constraint-violation-1452-cannot-add-or-update-a-child-row Soft deleting is also something that I wanted to include, which needs to be done in the migrations as well as the model: https://laravel.com/docs/8.x/eloquent#soft-deleting Also in the model I needed to add/adjust the hasXxx(model::class) to handle the different table relations that I made. Found a couple of good reads about which to use where: https://stackoverflow.com/questions/30058949/should-i-use-belongsto-or-hasone-in-laravel https://laravel.io/forum/08-14-2014-once-again-difference-between-hasone-has-many-and-belongstobelongstomany With migrations and models taken care of I wanted to seed some starter stuff into the databases. The basics are here: https://laravel.com/docs/8.x/seeding within one of my seeders I wanted do a DB query for a word as part of a content, found what I was looking for here: https://www.laravelcode.com/post/laravel-8-where-like-query-example-tutorial Keep in mind that '%'.$search.'%' dot notation is for concatenating strings. Another things I ended wanting to do was to do a "complex" DB query where it basically followed the format: this and ( that or that ) but in order to do that I needed to implement advanced where clauses: https://laravel.com/docs/5.2/queries#advanced-where-clauses I also wanted to use a variable inside of the function, which you can pass through using: use ($var) https://laravel.com/docs/5.2/queries#conditional-statements Last for now I also wanted to use the user session info inside of the controller, something like: Auth::user()-> Doable. just gotta add: use Illuminate\Support\Facades\Auth; to the top of the controller: https://stackoverflow.com/questions/25027996/laravel-authuser-in-controller
Id Title Body Post At