Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Sign in to follow this  

About This Venue

Discuss all things development/developing and projects underway by ZHXScript.

Location

Connecticut, 06260

  1. What's new in this venue
  2. Originally Released: May 2020 Current Version: 1.1 Lottery game allowing members to purchase tickets for a chance to win a portion of a jackpot. Available for Purchase here Please direct all support inquires to the proper support department. Non-technical, pre-sales and general questions can be discussed here.
  3. ETA: Early - Mid July 2020 A new and unique custom PanelScript theme to give your site a unique look from sites who utilize the default theme provided by PanelScript to all clients. This theme will feature a complete theme, supporting all features PanelScript offers & any add-on we build will be supported by this theme. Features Utilizes the latest theme integration techniques for further customization Easy apply custom CSS Modify/Adjust HTML Uses internal block system Translation Support Copyright Free* Complete Mobile & Desktop Friendly and more. Pricing for the custom theme will be announced once we have a demo up. Inquire with me if you are interested in the theme. *Theme is does not show branding by ZHXSCRIPT but will display PanelScript's Copyright/Version data.
  4. Few days ago I posted a Today I would like to provide 3 additional tips you can do with timestamps Not updating the updated_at field By default, when present, the updated_at column of the models record will be updated to the timestamp that the model was updated at. Usually this is just fine, but sometimes you might want to make an update without updating this column. To accomplish this, pass the updated_at column with a value of false $model = Banner::find(1); $model->name = 'Cool Banner Bro'; $model->updated_at = false; $model->save(); Parent Touch & Touch To update the timestamp columns you can also pass them with their values like this $model->update(['updated_at'=> now()]); //or via mass assignment Model::update(['updated_at'=> now()]); A shorter version to update the updated_at column would be $model->touch(); A more advanced usage of touch would be updating both a parent record and its related records updated_at columns. To do this, you define a touches property on the related model. Let's look at a profile model, which belongs to a user class Profile extends Model { protected $touches = ['user']; public function user() { return $this->belongsTo('User'); } } Now, anytime the profile is updated, the user model will also have its updated_at column updated. Ordering Eloquent provides a couple of model scopes that can be used to quickly sort model records by the timestamp fields. Here is a couple examples //Shortcut to: orderBy('created_at', 'desc') Model::latest()->get(); //Shortcut to: orderBy('created_at', 'asc') Model::oldest()->get(); Hope you find these tips useful!
  5. One of the most fascinating features I find with Laravel is the "magic" around "Eloquent Accessors" and the minimum code required to make it work. In terms of code organization & maintenance, I find it to be a great solution. Let's take a look at how accessors work. One of the easiest examples to produce is a typical users table, with separate fields for first name and last name. Without accessors, we would have to concatenate the two fields, throughout the application like this: //inside a blade {{$user->first_name}} {{$user->last_name}} //inside a controller or other php file $name = $user->first_name.' '.$user->last_name; As you can see, that's is a bit ideal, especially through a large application where this code would be present. Updating would be very time consuming, so instead on the model, let's define an accessor. public function getFullNameAttribute() { return $this->first_name.' '.$this->last_name; } Now through out the application we can call this code $user->full_name; As a personal preference, I like to keep my accessors short and simple, I try not to put to much "logic" into my accessors to prevent bugs and other issues. If my accessor requires a lot of logic, I will break it up into additional model methods. What do you think, do you employ Eloquent Accessors in your project?
  6. Great tips, I new to me was the customizing the dates field names, any more tips?
  7. The default expectation on eloquent models is that your models table contain 2 timestamp fields: created_at & updated_at. What a lot of people might not realize is just how easy it is to customize and manipulate these fields and or to disable them. Let's take a look at a few ways to manipulate and customize these fields. Generating tables without timestamps The command to generate a migration php artisan make:migrate CreateNewTable Will produce a migration file with the following function /** * Run the migrations. * * @return void */ public function up() { Schema::create('test', function (Blueprint $table) { $table->id(); $table->timestamps(); }); } To simply exclude the timestamps from the migration and table remove $table->timestamps(); This will generate a new table without timestamps, however Eloquent is still going to expect timestamps to be present. To resolve this, open the tables model and add the following <?php namespace App; use Illuminate\Database\Eloquent\Model; class Test extends Model { public $timestamps = FALSE; //rest of class } Just like that, your eloquent model is not using timestamps and related errors will not be generated. Timestamp Date & Time Format The default format for dates is: Y-m-d H:i:s. To customize the format there is a $dateFormat property available. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON. <?php namespace App; use Illuminate\Database\Eloquent\Model; class Test extends Model { public $timestamps = FALSE; /** * The storage format of the model's date columns. * * @var string */ protected $dateFormat = 'U'; //rest of class } Custom Timestamp Names If a condition ever came that would require you to adjust the name of the timestamp column on any database table there is a very painless way to accomplish this. To do this, just add the corresponding const to your model like so. <?php namespace App; use Illuminate\Database\Eloquent\Model; class Test extends Model { /** * Changes the default field name: created_at * to custom_create **/ const CREATED_AT = 'custom_create'; /** * Changes the default field name: updated_at * to custom_update */ const UPDATED_AT = 'custom_update'; public $timestamps = FALSE; /** * The storage format of the model's date columns. * * @var string */ protected $dateFormat = 'U'; //rest of class } As a reminder, the created_at and updated_at fields do not need to be cast to dates, as they already are and can perform carbon operations directly on them, Hope you enjoyed!
  8.  
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.