1. Go to this page and download the library: Download codewiser/casts library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
codewiser / casts example snippets
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\Casts\AsStringable;
use Illuminate\Support\Stringable;
/**
* @property null|Stringable $first_name
* @property null|Stringable $second_name
* @property null|Stringable $family_name
*/
class Username extends Pivot
{
protected function casts(): array
{
return [
'first_name' => AsStringable::class,
'second_name' => AsStringable::class,
'family_name' => AsStringable::class,
];
}
}
use Codewiser\Casts\AsStruct;
use Illuminate\Database\Eloquent\Model;
/**
* @property null|Username $name
*/
class User extends Model
{
protected function casts(): array
{
return [
'name' => AsStruct::using(Username::class)->nullable()
];
}
}
$user->name->first_name;
use Codewiser\Casts\AsStruct;
use Illuminate\Database\Eloquent\Model;
/**
* @property Username $name
*/
class User extends Model
{
protected function casts(): array
{
return [
'name' => AsStruct::using(Username::class)->
use Codewiser\Casts\AsStruct;
use Illuminate\Support\Collection;
/**
* @property null|ContactCollection<int,Contact> $contacts_1
* @property null|Collection<int,Contact> $contacts_2
* @property Collection<int,Contact> $contacts_3
*/
class User extends Model
{
protected function casts(): array
{
return [
'contacts_1' => AsStruct::collects(Contact::class, ContactCollection::class)->nullable(),
'contacts_2' => AsStruct::collects(Contact::class)->nullable(),
'contacts_3' => AsStruct::collects(Contact::class)->
// e.g. Laravel has Europe/London (+01:00) timezone
config()->set('app.timezone', 'Europe/London');
$model = new Article();
$model->date = '2000-01-01T10:00:00+02:00';
echo $model->date->format('c');
// Expecting 2000-01-01T09:00:00+01:00
// Actual 2000-01-01T09:00:00+01:00
use Illuminate\Database\Eloquent\Relations\Relation;
class MyRequest extends FormRequest
{
public function rules(): array
{
return [
'commentable_type' => 'e_type'));
return $class::find($this->integer('commentable_id'));
}
}
use Codewiser\Requests\HasMorphs;
use Illuminate\Database\Eloquent\Relations\Relation;
class MyRequest extends FormRequest
{
use HasMorphs;
public function rules(): array
{
return [
...$this->morph('commentable')
];
}
public function getCommentable(): Model
{
return $this->morphed('commentable');
}
}
use Codewiser\Requests\HasMorphs;
use Illuminate\Database\Eloquent\Relations\Relation;
class MyRequest extends FormRequest
{
use HasMorphs;
public function rules(): array
{
return [
...$this->nullableMorph('commentable', [Post::class, Article::class])
];
}
public function hasCommentable(): bool {
return $this->hasMorph('commentable');
}
public function getCommentable(): ?Model
{
return $this->morphed('commentable');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.