JoaoHamerski

JoaoHamerski

Member Since 1 Year Ago

Experience Points
4,860
Total
Experience

140 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
29
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-in-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
4,860 XP
May
23
4 days ago
Activity icon

Replied to How To Do A Query Search In Multiple Tables?

In fact it is doing the where only on the first query, i tried to use ->toSql() to see and this is the result:

select * from (select * from "table1" where "hash" = ?) union select * from (select * from "table2") union select * from (select * from "table3") union select * from (select * from "table4") . . . . 
Activity icon

Replied to How To Do A Query Search In Multiple Tables?

All my tables have the same columns, are two columns a id and json the only thing that changes are the structure of the json, but each json is a string, what i did was get all table names and do a foreach to do a union with all tables, because actually it is 56 tables. Well, the only solution i found was do a where in each query of table before do the union, but its a little bit ugly i think, if you have a better solution i'll appreciate it.

May
22
5 days ago
Activity icon

Replied to How To Do A Query Search In Multiple Tables?

@tray2 I just noticed that when i do a where search on the query search Laravel just ignores the union tables, he only do the where search on the first table, the other tables he just concat the records, do you know how can i solve it?

I'm doing something like that:

$query = DB::table('table1');

$query = $query->union(DB::table('table2'));

$data = $query->where('json->hash', 2803282938)->get();

So he show all the records from table2 next tables and apply the where clause only on the table1 , some solution for this?

Note: I'm using a SQLite database file.

Activity icon

Replied to How To Do A Query Search In Multiple Tables?

Wow that helped a lot, thanks

Activity icon

Started a new Conversation How To Do A Query Search In Multiple Tables?

For example, i have 20 tables and want to do a query search that perform a search in all tables and return a instance of Query Builder.

May
20
1 week ago
Activity icon

Replied to How To Get A DB Facade Instance After I Use The Method DB::connection()?

But your DbPdo class is a Facade or you instantiate every time you want to use?

May
19
1 week ago
Activity icon

Replied to How To Get A DB Facade Instance After I Use The Method DB::connection()?

Sure, i have been trying it for about 4h with no success, i don't know more what to do.

The problem is that, i need some extra methods with DB facade methods, but i need to set different connections at the same times.

Activity icon

Started a new Conversation How To Get A DB Facade Instance After I Use The Method DB::connection()?

Here is my problem: I want to extend the DB facade to create my own methods, but i need to set a different connection using DB::connection, but when i do:

$DB = DB::connection('sqlite')

dd($DB);

It returns a Illuminate\Database\SQLiteConnection instance instead of a DB Facade instance , so, all my methods i created when extended the DB facade in my custom class are gone, how can i resolve that?

Activity icon

Replied to How To Extends DB Facade Setting The Driver To 'sqlite' And The Database File?

I know that, but what i mean is that i want to manage a sqlite database file with my own methods, i want to extends the DB facade but instantitate a new class where i can set different database files and have my own methods, for example, "showAllTables()", "showAllTableNames()" and etc... How is the best way to do it?

Activity icon

Started a new Conversation How To Extends DB Facade Setting The Driver To 'sqlite' And The Database File?

I want to create a new class to create my own methods, but letting the methods of DB facade avaliable too, but i want to create a new Facade that i call and it already come with a database configured, is it possible?

May
18
1 week ago
Activity icon

Started a new Conversation How List All Tables Of Database Using Sqlite In Laravel?

I know that using MySQL or Postgres i can do DB::select('SHOW TABLES') but using a sqlite database it isn't working. I got this error:

SQLSTATE[HY000]: General error: 1 near "SHOW": syntax error (SQL: SHOW TABLES)

I tried using sqlite syntax with DB::select('.tables') but got a syntax error too.

Activity icon

Replied to [SERVER/PHP] Is It Safe To Change Config Variables At Runtime?

The way i found is that: I set the config file just before call DB::connection('sqlite')

Activity icon

Replied to [SERVER/PHP] Is It Safe To Change Config Variables At Runtime?

I have a databases table where i have lang column and filename column, each file has a different name. But i did this way because when i call DB::connection('sqlite') i need to have a database file in my 'database' key of database.php file, right? This is why i'm changing config variable at runtime, or there is another way to set the database file when i call DB::connection('sqlite')?

Activity icon

Started a new Conversation [SERVER/PHP] Is It Safe To Change Config Variables At Runtime?

For example, i have many database files on my server, they are the same the only thing that changes is the language, so i want to put a option to user choose the language and so i change de config of my config/database.php in 'connections' => [ 'sqlite' => 'database' .... to the path of the database that correspond to the language the user choose, but, i do it on my controller with config(['database.connections.sqlite.database' => $this->getDatabasePath($lang)]) is it safe? Can it have conflict if two of more users choose two or more different langauges at same time? Or each application run a complete different instance in each client machine?

May
16
1 week ago
Activity icon

Replied to Laravel And PHP Extensions

Some classes that appears when you type php -m in console are not the same name you have to instantiate in code.

For example, if i see here what PHP extensions i have when type php -m , i have one that is named zip, but if i try to instantiate zip like $zip = new \Zip() it says class not found, instead, the correct way is $zip = new \ZipArchive() . So, give a try with this class (zip) and tell me what you get.

May
15
1 week ago
Activity icon

Replied to Laravel Singleton Doesn't Work

Thanks, it worked, i didn't read the entire page of the documentation, my bad, but why don't use env helper outside of config files?

Activity icon

Started a new Conversation Laravel Singleton Doesn't Work

So i put this code on the method register() inside the AppServiceProvider:

public function register()
{
    $this->app->singleton(GuzzleHttp\Client::class, function($app) {
        return new Client([
            'base_uri' => env('API_BASE_URI'),
            'headers' => ['X-API-Key' => env('API_KEY')],
            'curl' => [
                CURLOPT_SSL_VERIFYPEER => false
            ]
        ]);
    });
}

but when i do a new instance it simply doesn't work, i know that it doesn't work because on my controller i did:

$client = new GuzzleHttp\Client();

dd($client);

and the debugger page doesnt show the X-API-Key on the headers key that i put on the singleton closure, so, what am i doing wrong?

This is what it shows:

"headers" => array:1 [▼
    "User-Agent" => "GuzzleHttp/6.5.3 curl/7.68.0 PHP/7.4.3"
]

This is what should show:

"headers" => array:1 [▼
    "User-Agent" => "GuzzleHttp/6.5.3 curl/7.68.0 PHP/7.4.3"
    "X-API-Key" => "My API Key Here"
]
May
14
1 week ago
Activity icon

Replied to Laravel Scheduling Isn't Working

@snapey Thanks for the tip, actually my problem was because my system are installed in portuguese brazilian, so i write the path wrong with white space, because "desktop" are "área de trabalho" so it had no effect when i put "/home/user/Área de Trabalho" on cronjob file to find my project that is on desktop.

Activity icon

Replied to Laravel Scheduling Isn't Working

I found the error, i'm beginner in linux sorry, the correct way to implement Laravel scheduling is add Cron entry to cron file, so the command in Laravel documentation * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 has to be added at the end of the file.

Here is how if someone has the same problem: In the terminal type cronjob -e it will ask you what editor you want to open the file, choose the number that correspond to vim.

Now, with the file open, press i , go to the end of the file, and write the command:

* * * * * cd /absolute-path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 be careful, if your path have white space you have to use \ , for example: "/home/user/Desktop/My Project/" => TYPE => "/home/user/Desktop/My\ Project/.

To save the file press ESC and :wq and ENTER

And is it!

Activity icon

Started a new Conversation Laravel Scheduling Isn't Working

So, i'm using Ubuntu 20.0 to run my application, according to documentation, first i run this command on my application directoy php artisan schedule:run >> /dev/null 2>&1 then in app\Console\Kernel.php on schedule method, i put this code:

    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function() {
            DB::table('tasks')->insert([
                ['description' => 'Some description']
            ]);
        })->everyMinute();
    }

Just to test, if some register was inserted on the table at 14:05 i know that it works, but i wait until 14:05 and nothing happens, i checked the table and it was empty, sorry if i'm terrible wrong about how scheduling works, but what i'm doing wrong? I was running php artisan server, and restarted the server after write the code above.

Cron is installed on my ubuntu and enabled.

May
11
2 weeks ago
Activity icon

Started a new Conversation How Can I Dispatch Some Block Of Code When Is A Determined Time Of Every Day In PHP?

For example, everyday at 2PM PST i want to run a code on the server where my app is running that execute automatically some piece of code, how can i do it in PHP? Is it possible?

Activity icon

Replied to How To Connect To A Sqlite Database In Runtime Execution?

Because the user will choose what database file he want, because there is different language translations to different database files, so, i have to change the database file according to the user choises, this is the way the API i'm working with works.

May
10
2 weeks ago
Activity icon

Replied to How To Connect To A Sqlite Database In Runtime Execution?

I was about to try it, thanks, i think it works well, but, how can i change the path to database at runtime? Is it possible?

Activity icon

Replied to How To Connect To A Sqlite Database In Runtime Execution?

I already managed to run the database with SQlite DB Browser, but my question is how to load the database in my laravel application and get the data from this database, it is a file .sqlite3 stored in my public/cache/ directory.

Activity icon

Started a new Conversation How To Connect To A Sqlite Database In Runtime Execution?

So i have a .sqlite3 file that i get from an API and i store this file in public/cache/, i would like to know how to i connect to this database to perform query search and etc...

Apr
20
1 month ago
Activity icon

Replied to How To Perform A Query Search Of All "clients" From A Builder Instance Of "products"

@snapey I just noticed it now, i don't need to start with product, i didn't know how whereHas was used, thanks, your solution works perfectly.

Activity icon

Started a new Conversation How To Perform A Query Search Of All "clients" From A Builder Instance Of "products"

So, i have a Builder instance of "products" with Product::orderBy('purchase_date', 'desc') , and each of my products have a 'client_id' field, now i just want to get all products where the client name is like something, how can i do it?

That is the (ugly) way i implemented it, is this correct or there is a better way to do it?

// Here i get all products
$products = Product::orderBy('purchase_date', 'desc');

// Here i get all id of the clients, remove duplicates with "unique" and sort.
$clients = $products->pluck('client_id')->unique()->sort();

// Now i find all clients passing a array of IDs
$clients = Client::find($clients);

// Here i create a new collection of Clients
$clients = (new Client)->fill($clients->toArray()); 

// The method getBySearch basically return a query search 
// like this: $clients->where($table, 'like', '%' . $query['search'] . '%');
// where i get all clients that matches the search
$clients = $this->getBySearch($request->query(), $clients);

// Now i return all products with the client ids
 $products = Product::where('client_id', $clients->pluck('id'));
       

So, this code gives me all products of a client that have, for example, 'Lu' in his name. There is a better way to do that?

Apr
19
1 month ago
Activity icon

Started a new Conversation How To Use Another Model In A Where Clause?

For example: I want to do a query search like that

$products -> where -> $client->getTotalOwing > 10

I know that is out of correct syntax but i think is the best way to explain, i have a $product model, and i want to do a where query, but i only want get the products where the relationed client with the product has "getTotalOwing > 10", is it possible? The "getTotalOwing" method is on my Client model. I just don't need to use the product attributes, i want to use another model attributes to do the query.

Apr
16
1 month ago
Activity icon

Replied to Is It Possible Using Model Methods To Make A Query?

Look at my reply, is it the correct way to do it maybe, or there is a better way with query scopes?

Activity icon

Replied to Is It Possible Using Model Methods To Make A Query?

@jlrdw i did, but query scopes only uses query variable, and i need to use a model variable to call a method in my model that checks something.

With queries i cant do $query->methodInMyModel().

Activity icon

Replied to Is It Possible Using Model Methods To Make A Query?

I did this:

foreach(Product::all() as $product) {
   if ($product->isPaid()) {
        $products[] = $product;
   }
}

But idk if is the best way, there is another way to do that?

Activity icon

Started a new Conversation Is It Possible Using Model Methods To Make A Query?

For example, i have a method in my model to check if the Product is paid.

public function isPaid()
{
    return $this->getRemainingValue() <= 0 && ($this->value != null);
}

How can i iterate over all registers and get only the models that "is paid" but using this method, without writing a new query. Something like iterate over all registers and return all models that are true using isPaid method.

Mar
23
2 months ago
Activity icon

Replied to Best Way To Set A Sidebar Active While Navigating Between Pages?

@sergiu17 Wow i didn't know that, very much thanks. But for me, the cons of using it, is that everytime a new page is loaded, the sidebar do the animation of 'showing', because the page have to load first and javascript last, so it's a little bit annoying for some people i think, do you think there is a way to fix it?

Activity icon

Started a new Conversation Best Way To Set A Sidebar Active While Navigating Between Pages?

So, i have a sidebar and i want that when the users clicks on the hamburger icon, the sidebar opens, and it keeps opened while user navigate on the site or until he clicks again to close de sidebar.

The way i implemented it:

via Ajax:

$("#btnSidebar").on('click', function() {
    let data = {name: "sidebar_is_active", value: true};

    if ($(this).hasClass('is-active') {
        data.value = false;
    } else {
        data.value = true;
    }

    axios.post('/set-cookie', data, headers);
}); 	

On my route:

Route::post('/set-cookie', '[email protected]');

On my controller:

public function setCookie(Request $request)
{
    Cookie::queue(Cookie::forever($request->name, $request->value));
}

So, it works perfectly, but is this the best way to do it? Or there is other way? Is a good practice making ajax request to server just to set a cookie? What you guys thinkt?

Activity icon

Replied to Can't Set A Cookie To TRUE Or FALSE?

@deansatch

 axios.post('/set-cookie', data, headers)
          .then(function(response) {
            console.log(response);
          })
          .catch(function(error) {
            console.log(error.response);
          });
Activity icon

Replied to Can't Set A Cookie To TRUE Or FALSE?

@deansatch No, i was wrong, it works, but seems that true returns a string "1" and false returns a empty string.

Activity icon

Started a new Conversation Can't Set A Cookie To TRUE Or FALSE?

I'm trying to set a cookie like this:

public function setCookie(Request $request)
{
    Cookie::queue(Cookie::forever($request->name, $request->value));
}

I pass the request via AJAX:

let data = { name: 'name', value: true}

but when i try to return the Cookie i got a empty string, it only works with strings or what?

var_dump(Cookie::get('name')) // string(0) ""
Mar
22
2 months ago
Activity icon

Replied to Best Way To Implement Dynamic Table Attributes In Laravel?

Thanks, i think this will work well.

Activity icon

Started a new Conversation Best Way To Implement Dynamic Table Attributes In Laravel?

I would like to know, for example, if i have a products table and i don't know what will be the attributes, maybe it can be a smartphone (with attributes: display, cpu, gpu ...) or a hdd ( with attributes: memory size, rpm ...), so, what is the best way to implement it? I know that i can save it in a field a descpription with a WYSIWYG editor and put anything i want, but i want to use these attributes to make a search by query string, so, how can i do it? Maybe saving as json in a filed on the table products? In json i can do a search by query string? How would be it?

Mar
20
2 months ago
Activity icon

Replied to Problem When Using 'window.location.href' In JavaScript To Get Edit Route

Just found the problem, it was on my controller, i was returning the route return redirect()->route('clients.show', $client); instead of a return response()->json(['redirect' => route(...)]) and redirect via JavaScript.

Activity icon

Replied to Problem When Using 'window.location.href' In JavaScript To Get Edit Route

@jlrdw Using relative url don't worked as well, this is a extremely weird bug, jquery or javascript (idk) just removes 'edit' of the end of string, how is it possible?

i tried:

url: '/clients/1/edit',

and it returns PATCH http://localhost:8000/clientes/1 405 (Method Not Allowed)

but I DONT SEND REQUEST TO "http://localhost:8000/clients/1" i send to "http://localhost:8000/clientes/1/edit"

Activity icon

Replied to Problem When Using 'window.location.href' In JavaScript To Get Edit Route

Route? Do you mean {{ route('route.name') }}? I already tried even hardcode the route, and jQuery just keeps returning the wrong URL, without the 'edit' in the end, i tried using axios too, but had the same problem.

Activity icon

Started a new Conversation Problem When Using 'window.location.href' In JavaScript To Get Edit Route

I'm using jQuery Ajax to update data, so i use window.location.href in 'url' parameter to get the route, and the method i set to patch, which is the same as my route file, but, seems jQuery is sending the wrong url, i don't understand why.

THE CURRENT URL: http://localhost:8000/clients/1/edit

THE ROUTE FILE:

Route::prefix('clients')->group(function() {
   Route::name('clients.')->group(function() {
       .
       .
       .
       Route::patch('/{client}/edit', '[email protected]')->name('update'); 
    });
}

MY JAVASCRIPT CODE:

$("#btn-update").on('click', function(e) {
      e.preventDefault();
     
      // here i get the data object i need
      let data = getData($("input").is("[name=cpf]"), false);

      $.ajax({
        headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        type: 'PATCH',
        url: window.location.href,
        data: data,
        success: function(data) {
          console.log(data.responseText);
        },
        error: function(data) {
          console.log(data.responseText);
        }
      });

But, here is the error i get:

PATCH http://localhost:8000/clients/1 405 (Method Not Allowed) 

It seems that Query is not sending the complete URL, it should send http://localhost:8000/clients/1/edit (the CURRENT URL), but just sends http://localhost:8000/clients/1

So, why this bug is happening? I already try to check the url using console.log(window.location.href) and it returns the right URL, but when i sendo via ajax jQuery parameter its sends wrong (without the 'edit' at the end), why?

Mar
18
2 months ago
Activity icon

Started a new Conversation How To Connect An API With Laravel?

So my knowledge about APIs are very poor, i would like to know how do i connect with an API in Laravel, how can i return data from a API. For example, this API from a game Destiny 2 (https://bungie-net.github.io/), it says that API Root Path: https://www.bungie.net/Platform, what it means? How can i retrieve data from this API?

Mar
02
2 months ago
Activity icon

Replied to Malformed UTF-8 Characters, Possibly Incorrectly Encoded

That solution works 100% at the moment, but if someone have a best solution i will appreciate it.

Activity icon

Replied to Malformed UTF-8 Characters, Possibly Incorrectly Encoded

@rob897 It works, but if i type other characteres like "Ààãô" it bugs again and returns, for example: "%B ás" => "março à s"

I'm almost sure that is a PHP bug, i have already tried many things, but the only solution was verify if the day/month name corresponds to a word with accents and replace "%B" for the given day/month name manually

public static function replaceExceptions($format, $timestamp)
{
    if (\Str::contains($format, "%B") && date('n', $timestamp) == 3) {
        $format = str_replace("%B", "março", $format);
    }
if(\Str::contains($format, "%A") && date('N', $timestamp) == 6) {
    $format = str_replace("%A", "sábado", $format);
}

if(\Str::contains($format, "%A") && date('N', $timestamp) == 2) {
    $format = str_replace("%A", "terça-feira", $format);
}

return $format;

}

Activity icon

Started a new Conversation Malformed UTF-8 Characters, Possibly Incorrectly Encoded

I get this error when using this code:

strftime("%B", strtotime("2020-03-02 12:25:39"))

The problem is that in portuguese brazilian the month name (03) is "março", so, everytime it returns "março" with special char "ç", i get this error.

But if i try to run this code:

strftime("março", strtotime("2020-03-02 12:25:39"))

Laravel doesnt throw any error, and works perfectly.

Someone knows how to fix this bug?

OBS: I think this is a Windows issue, on Linux everything works perfectly.

Feb
24
3 months ago
Activity icon

Replied to How To Mix A Helpers JS File That Works To All Others Js Files?

@sinnbeck It worked perfectly, thank you so much.

Feb
23
3 months ago
Activity icon

Replied to How To Mix A Helpers JS File That Works To All Others Js Files?

Ah, no, i didn't do it, can you show me an example of how to do it? I mean, i just have in the file:

function f1() {
    ....
}

function f2() { .... }