I'm trying to use Spatie/Laravel-data package for the first time, and most importantly this is for an interview assignment. I really wanted to this work to be more attractive to the interviewer so instead of just using the normal way I wanted to use Spatie/Laravel-data package.
So, My problem is I did follow their introduction videos and I could replicate all the things in the video but in any of the tutorials or materials I couldn't find how to use DTOs with file uploading. Specially I want to upload multiple files via API
This is what I have at the moment.
class DamageReportData extends Data
{
public string $name;
#[Email]
public string $email;
public string $description;
public float $latitude;
public float $longitude;
// #[Rule(['mimes:jpeg,jpg,png,gif,csv,txt,pdf|max:2048'])]
// public UploadedFile $uploadedFiles;
public ?Carbon $date;
}
until I keep commenting on those two lines it all works perfectly. otherwise, I'm getting an error
Error: Typed property App\Data\DamageReportData::$uploadedFiles must not be accessed before initialization in file D:\xampp\htdocs\fixico\vendor\spatie\laravel-data\src\Transformers\DataTransformer.php on line 73
If someone can guide me on how to do this properly will be a huge advantage for me and I really appreciate your help.
Related
I am new to unity and I was wondering if there is any way to call a Public TextMeshPro Because I have been trying to make a score timer Current Script For example I use
public Text ScoreText; . So is there any way to work to do something like Public Tmp_Text ScoreText; or something Like That? (Sorry I am pretty new)
I wish you good luck on your journey of learning unity.
There are several things you need to keep in mind to solve your problems.
Follow tutorials and learn from them.
Check unity api to get references on how specific modules/commands function.
In your case that would be this reference.
Here's the shortest way to do what you want:
add using TMPro;to the very top of your script
declare your variable public TextMeshProUGUI scoreText
use your variable in code to set text;
void AddPoints(int pointsEarned)
{
currentScore += pointsEarned;
scoreText.SetText(currentScore.ToString());
}
I have a really large database that I want to introduce testing into and I have been experimenting on ways to approach it.
If I were to run the full migrate:fresh it would take about a minute to create the entire schema. To seed this database it would likely take >5 minutes. My thought was to preload a DB schema into a test database ahead of time, before the tests even start running. Then I was thinking I would seed portions of data as I need it.
This project has multiple domains and it is a DDD design. I'm currently thinking that each domain will have at least one testcase that extends laravels BaseTestcase.
The problem I'm having is that I am not able to clear the data from the database. Here's an example of what a testcase might look like for one of my domains:
use CreatesApplication;
use RefreshDatabase;
use ActingAsUser;
use HasTestHelpers;
/**
* Refresh a conventional test database.
*
* #return void
*/
protected function refreshTestDatabase()
{
if (! RefreshDatabaseState::$migrated) {
$this->artisan('db:seed --class=CourseTestSeeder');
$this->app[Kernel::class]->setArtisan(null);
RefreshDatabaseState::$migrated = true;
}
$this->beginDatabaseTransaction();
}
public static function tearDownAfterClass(): void
{
CourseTestSeeder::clear();
}
This particular domains depends on Course data. I put together a CourseTestSeeder that will seed all the data needed for this domain. This part works great. The problem is that when I try to do a tearDownAfterClass I receive the following error:
Exception in tearDownAfterClass
Target class [db] does not exist.
at vendor/phpunit/phpunit/phpunit:98
94▕ unset($options);
95▕
96▕ require PHPUNIT_COMPOSER_INSTALL;
97▕
➜ 98▕ PHPUnit\TextUI\Command::main();
99▕
How can I clear the db after my tests run? Is there a better way to do this?
If this is not a good approach, I also welcome feedback on that.
I need to integrate Slider Revolution editor into my Laravel (5.5) app.
I've put the editor in public/revslider/ folder to be able to use the visual editor. I also created a helper class to "communicate" with it and be able to use it inside my Blade views:
namespace App\Helpers;
include( public_path('revslider/embed.php') );
class Slider{
/**
* This function is called where you want to put your slider
*/
public static function make($slider){
return \RevSliderEmbedder::putRevSlider( $slider );
}
/**
* This function is called inside <HEAD> tag to include all
* SR assets (js/css/font files)
*/
public static function head(){
return \RevSliderEmbedder::headIncludes(false);
}
}
The SR's PHP code does not use namespaces. In fact it is a strange mix of Code Igniter, Wordpress and vanilla php.
The problem is it is trying to declare a translation function __(...):
if( ! function_exists('__'))
{
function __($string = '')
{
....
}
}
and since there is already such Laravel's helper function, it does not redeclare it and tries to use Laravel's __() function. And that obviously causes errors.
I temporarily managed to fix this problem by changing the name of SR's __() function (and all references to it). But of course it is not a best way to solve this problem, since I will be unable to use SR's automatic updates or will be forced to do these changes after every update.
So my questions are:
Is there any good way of integrating such "bad" code into your project, invoking it safely without conflicts? Is there any way of isolating such code and avoid clashes? By "bad code" I mean code that does not follow strict OOP/PSR rules present in projects like Laravel.
What is the best way to include "external" PHP code? I've just used plain include() inside of my helper class' file, but is there a better/cleaner way? Like, I don't know, loading it through composer?
I was trying to get this working in a typical belongsTo relation. However it keeps saying that the column is not set in the model, even if looking in the actual database it is there.
I have tried to look at the source code as well as try many approaches to bypass this issue, however nothing seems to do anything.
public function modifiedBy()
{
return $this->belongsTo('\Modules\Users\Model\User', 'modified_by');
}
public function createdBy()
{
return $this->belongsTo('\Modules\Users\Model\User', 'created_by');
}
This is the code inside the model, I use PSR-0 to define modules, better splitting up logic (no issues with that) but using this it would give an error of
Undefined property: \Modules\Module\Model\CurrentModel::$modified_by
This is coming from a seed to push some initial info into the database.
$user = Sentinel::findById(1);
$model = new CurrentModel;
$model->modifiedBy()->associate($user);
$model->save();
This is basically how it goes together, I have tried for some time to figure out what is wrong but I am calling blanks. Any ideas?
Found out a solution. Not a fix though but I would consider this an issue with laravel so I may look into adding it as a bug report (although this could be fixed in laravel 5?).
Basically with modified_by I need to define the column it is using and not let laravel automatically generate it (in order to do this "cleanly"). However the "bug" (only calling it a bug as currently I can only see this as an unintended problem) makes it so you cannot define the column it will be using, you have to let laravel decide it for you.
So I changed the functions to look like this:
public function modifiedby()
{
return $this->belongsTo('\Modules\Users\Model\User');
}
This makes laravel assume the column is modifiedby_id, but by changing my migrations to reflect that there was no more error.
I have been trying to figure out another way of handling i18n within FuelPHP (see here).
I decided to import the Symfony2 Translation component (using composer) to Fuel as a vendor and manage i18n with xliff files.
Here is my (simplified) code:
use \Symfony\Component\Translation\Translator;
use \Symfony\Component\Translation\MessageSelector;
use \Symfony\Component\Translation\Loader\XliffFileLoader;
...
class I18N
{
private static $translator = NULL;
....
public static function get($key)
{
# Load and configure the translator
self::$translator = new Translator('en_GB', new MessageSelector());
self::$translator->addLoader('xliff', new XliffFileLoader());
self::$translator->addResource('xliff', 'path/to/xliff/file', 'en');
# Get the translation
$translation = self::$translator->trans($key, $params);
# Return the translation
return $translation;
}
}
So at first I thought that was working great since I was testing it on a very small xliff file but now that I've generated the complete xliff catalogue (about 1400 entries) for my entire application, each request is really slow.
So the question is, is there a way to cache translations when using the Translation component the same way the whole Symfony2 Framework caches it natively?
The Translator Class from the FrameworkBundle has a constructor that accepts options in which you can define the cache_dir. Anyway I can achieve that using the Translation component?
Thanks for any help on that matter.
So what I did was to generate my own cache from xliff files, if it doesn't exist, which is nothing more than the translations as a php array and make the Translator Component load resources as ArrayLoader instead of XliffFileLoader. It's lightning fast now. Thanks to Touki in the comments for your interest.