jthorpe

Experience

2,490

0 Best Reply Awards

  • Member Since 8 Months Ago
  • 20 Lessons Completed
  • 0 Favorites

20th September, 2017

jthorpe left a reply on Laravel 5.5 - Is Debugbar Working After Login For Anyone? • 4 days ago

@ejdelmonico Yep I've got that set properly and the provider is being detected. It even displays properly on the login screen and then only disappears after the redirect. It's after the redirect that I get ERROR: Debugbar exception: Data missing in storage/logs/laravel.log

jthorpe left a reply on Laravel 5.5 - Is Debugbar Working After Login For Anyone? • 4 days ago

@ejdelmonico Yep, I'm using debugbar 3.1 and Laravel 5.5.9.

Any other ideas? Thanks for your help.

jthorpe started a new conversation Laravel 5.5 - Is Debugbar Working After Login For Anyone? • 4 days ago

Has anyone had any issues with barryvdh/laravel-debugbar since Laravel 5.5? Today I created my first 5.5 project, added authentication, added debugbar via composer and then noticed it wouldn't show up after logging in.

I found this in my log: local.ERROR: Debugbar exception: Data missing

There doesn't seem to be any errors other than that. I found other users having the same issue, but there doesn't seem to be a fix: https://github.com/barryvdh/laravel-debugbar/issues/645

Has anyone else had this issue?

13th September, 2017

jthorpe left a reply on SyntaxError: Unexpected Token S On Running Gulp • 1 week ago

@nauf I just ran into this and had to commend your answer. Thanks for saving me a bunch of time.

jthorpe left a reply on Modified Config Settings Seem To Disappear After Changing Route. • 1 week ago

As usual the answer became apparent shortly after posting here (I've been stuck on this for a couple days).

Here's what I did in case anyone else has this issue. Also, if you want to offer any ideas that may make it more streamlined and clean, that's always appreciated.

I changed my controller to just flash the user's selections to the session:

public function changeConnection(Request $request)
    {
        $program = Database::active()->where('brand', $request->brand)
                                     ->where('season', $request->season)
                                     ->where('year', $request->year)
                                     ->first();

        $dbName = $program->database;
        $fullProgram  = $program->brand." ".$program->season." ".$program->year;
}

Then I created a connections trait:

trait ConnectionsTrait   {

    public function setDbConnection()
    {
        $dbName = request()->session()->get('database');
        config(['database.connections.main.database' => $dbName]);
        DB::reconnect('main');
    }
}

The trait needs to be called before each query (this is where I'm sure it can be improved, possibly in a middleware like @vanderb suggested):

public function get()
        {
            $this->setDbConnection();

            //Use DB or Eloquent as needed
        }

Any suggestions to improve are much appreciated. Hopefully this helps someone in the future.

jthorpe left a reply on Modified Config Settings Seem To Disappear After Changing Route. • 1 week ago

I left the database field empty because the plan is to populate it before a query ever happens. So basically this happens before the query:

config(['database.connections.main.database' => $dbName]);

When I check the config setting in the same method, it returns the correct database name:

$configDb = config('database.connections.main.database');

I output that to the console via AJAX and it's returning the correct value.

The issue is that the setting doesn't seem to be remembered after switching to a different route.

jthorpe started a new conversation Modified Config Settings Seem To Disappear After Changing Route. • 1 week ago

I'm currently trying to refactor an older project and I want to implement database switching by writing to the config file. I'm looking to do it this way so that I can use my models for a bunch of different databases.

Here's the relevant portion of my config/database file ():

'default' => 'main',

'connections' => [

        'main' => [
            'driver' => 'mysql',
            'host' => '123.123.12.12',
            'port' => '3306',
            'database' => '',
            'username' => 'notTheActualUsername',
            'password' => 'notTheActualPassword',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

This method is in the controller where I'm trying to change the connection. After the return, I'm spitting the variables to the console and everything is looking correct. At this point config('database.connections.main.database') is returning exactly what I want it to when dumped to the console. I've tried the Config::set method and haven't had success.

public function changeConnection(Request $request)
    {
        $program = Database::active()->where('brand', $request->brand)
                                                 ->where('season', $request->season)
                                                 ->where('year', $request->year)
                                                 ->first();

        $dbName = $program->database;
        $fullProgram  = $program->brand." ".$program->season." ".$program->year;

        config(['database.connections.main.database' => $dbName]);
        DB::reconnect('main');
        $pdoInfo = DB::connection('main')->getPdo();
        $configDb = config('database.connections.main.database');
        $request->session()->put('program', $fullProgram);
        $request->session()->put('brand', $program->brand);
        $request->session()->put('season', $program->season);
        $request->session()->put('year', $program->year);

        return response()->json(['fullProgram' => $fullProgram, 'dbName' => $dbName, 'configDb' => $configDb, 'pdoInfo' => $pdoInfo]);

When I try to actually execute a query after hitting another route, I get this error: Invalid catalog name: 1046 No database selected.

When I route to another controller and just try to read and dump the same config('database.connections.main.database'), I get an empty string which I'm guessing is the problem. So it seems the config is being changed, but then reverting to the initial empty string when I go to another route.

Here's the controller method where I'm just trying to test the new connection (the $request is just there for what normally comes after the dd):


public function get(Request $request)
        {   
            $configDb = config('database.connections.main.database');
            dd($configDb);
    }

There must be something I'm missing here. I know I've seen people do very similar things if not exactly the same thing. Any insight is much appreciated.

30th August, 2017

jthorpe started a new conversation Best Practices - App Key Between Dev And Production? • 3 weeks ago

I'm working on developing an application that requires encrypting a few fields containing personal data. It isn't live yet, so I'm doing some tests on both the production and dev server (Homestead) to make sure everything is working properly in both places.

At one point I need to decrypt the data to see if they've registered before. If I have existing orders from my dev server, they can't be decrypted since the App Key is different.

For a fairly small project, would you make the App Key the same on both dev and production? Or would you setup a test database and modify your git settings so that nothing database related is pushed to the production server?

Thanks for your input!

24th May, 2017

jthorpe started a new conversation Homestead Windows - Having To Reboot After Changing Homestead.yaml • 4 months ago

I've been using Laravel for a little while, but just started using Homestead (on Windows). I followed this guide which made it fairly easy to get started: https://medium.com/@eaimanshoshi/i-am-going-to-write-down-step-by-step-procedure-to-setup-homestead-for-laravel-5-2-17491a423aa

I'm having a strange issue where sometimes when running homestead up --provision (after changing my homestead.yaml) I'll get the following messages and my files won't sync:

homestead-7: Waiting for machine to boot. This may take a few minutes...
homestead-7: SSH address: 127.0.0.1:2222
homestead-7: SSH username: vagrant
homestead-7: SSH auth method: private key
homestead-7: Warning: Connection reset. Retrying...
homestead-7: Warning: Connection aborted. Retrying...
homestead-7: Warning: Connection reset. Retrying...
homestead-7: Warning: Connection aborted. Retrying...
homestead-7: Warning: Connection reset. Retrying...
homestead-7: Warning: Connection aborted. Retrying...
homestead-7: Warning: Connection reset. Retrying...
homestead-7: Warning: Connection aborted. Retrying...

==> homestead-7: Machine booted and ready!

Rebooting my physical PC and then starting everything up seems to resolve the issue, but it's kind of hassle to have to do that every time. Has anyone come across this and found a fix?

Thanks!

24th January, 2017

jthorpe started a new conversation Is It Possible To Set An Eloquent Connection With A Variable? • 7 months ago

Hi,

I'm fairly new to Laravel and I'm trying to stick to its philosophy of modular, re-usable code. Last week I was able to sort out changing my database based on a user input so that all of my controllers could be used with 3 databases with different names, but identical structures. You can see my thread asking about my method here: https://laracasts.com/discuss/channels/code-review/is-this-an-acceptable-way-to-change-database-connections

Now, I'm trying to do the same for some other queries that are using Eloquent instead of DB. I've seen that you can update the connection like so:

protected $connection = 'string';

But this is invalid and throws an error:

protected $connection = $variableConnection;

I've searched and experimented a ton and the closest thing I've seen is setting the connection in every different model and for every different database. I can't see this community doing that, so I must be missing something.

As a quick reference to where my variable is coming from and how I do it with DB:

  • User chooses a brand on the first page
  • AJAX calls a controller that sets that brand as a session variable
  • User clicks a link to see a report which routes to a view and controller
  • The controller uses a trait to get the connection info from the session
  • Query is executed using DB::connection($connection)

jthorpe left a reply on Is This An Acceptable Way To Change Database Connections? • 8 months ago

@goatshark That's actually extremely helpful to know, thank you. I'm definitely going to go through that series.

19th January, 2017

jthorpe left a reply on Is This An Acceptable Way To Change Database Connections? • 8 months ago

Thanks for the quick responses!

@goatshark, here's my ConnectionsTrait:

<?php
namespace App\Traits;

use Illuminate\Http\Request;

trait ConnectionsTrait   {

    public static function dbconnection()
    {
        return request()->session()->get('database');
    }
}

@Niban, where exactly do you put a class like the ClaimTotals ones you mentioned? So far I've seen classes in controllers, and I've seen people mention creating classes outside of controllers, but I'm not sure exactly where the proper place would be to create them.

I'm going to try getting Eloquent models to work with this like you suggested. It's definitely possible that I just don't understand them well enough yet.

Thanks again for your help.

jthorpe started a new conversation Is This An Acceptable Way To Change Database Connections? • 8 months ago

I'm fairly new to Laravel and MVC in general and I'm trying to make sure I follow the guidelines to write proper code.

Over the past couple of days I've been trying to implement switching databases based on a user choosing a company to view a report. I saw a number of different methods, but none seemed to fit my situation or seemed a little over my head since I'm still new to this.

I've also needed to use DB::connection and DB::raw since I need to use a lot of MySQL functions that can't be executed with Eloquent (at least to my knowledge) which has prevented me from using the protected $connection way of switching databases. My databases were also created before I started using Laravel and in such a way that makes it very difficult to setup relationships between the models.

Here's the gist of how I implemented it and why I wanted to do it like this:

All of the databases use the same table structure, so I didn't want to create separate controllers for each.All of my possible databases are stored in database.php and my .env file.

  1. User hits my first view and makes a company selection.
  2. Their selection is sent to a controller via AJAX which stores it in a session variable.
  3. I created a trait to grab the database name from the session variables.
  4. The trait is called in any method that accesses the databases.

Number 4 is where I feel like I'm doing something wrong. It works fine, but having to call the trait that gets the connection in every method feels sloppy, but I'm not sure if there's another way to do it (even if I were to use Eloquent and protected $connection) when my user might be switching databases with every different report they want to see.

Here's an example of one of my controllers for reference:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
use App\Traits\ConnectionsTrait;

class ProcessedSubsController extends Controller
{       

        use ConnectionsTrait;

        public function byWeek()
        {   

            $connection = $this->dbconnection();

            $totals = DB::connection($connection)
                    ->select(DB::raw("
                    SELECT str_to_date(concat(yearweek(submit_date), 'saturday'), '%X%V %W') as `weekEnding`, 
                    count(claimform1_ID) as Total, 
                    sum(case when claim_approved = 1 then 1 else 0 end) as Compliant,
                    sum(case when claim_approved = 0 then 1 else 0 end) as NonCompliant
                    FROM claim_form1
                    WHERE  submit_date like '20%'
                    Group By weekEnding;
                    ") );

            return view('reports.processed', compact('totals','connection'));

        }

}

Any tips are much appreciated. Thanks!

17th January, 2017

jthorpe left a reply on @php Tag Breaking Syntax Highlighting When In @foreach Loop (Sublime Text 3) • 8 months ago

Well, this is embarrassing. It ended up being the semi-colons I left out when creating the variable.

Sorry for cluttering up the forum!

16th January, 2017

jthorpe started a new conversation @php Tag Breaking Syntax Highlighting When In @foreach Loop (Sublime Text 3) • 8 months ago

To start, I'm fairly new to Laravel. If anything I say here is incorrect, feel free to correct me.

I have a query that I needed to execute using DB raw, so I wasn't able to use Eloquent. Because of this, I had to do some calculations in my view. I needed to set a variable and then calculate it based on my @foreach loop, but doing so breaks the syntax highlighting after the @php tag in my loop. Has anyone else had this problem?

I'm also open to better ways to do the calculation without it being in the view if anyone wants to suggest it.

This is my first post here, so if the code blocks look messed up I'll fix them right away.

This is my query:

public function byWeek()
        {

            $totals = DB::select(DB::raw("
                    SELECT str_to_date(concat(yearweek(submit_date), 'saturday'), '%X%V %W') as `weekEnding`, 
                    count(claimform1_ID) as Total, 
                    sum(case when claim_approved = 1 then 1 else 0 end) as Compliant,
                    sum(case when claim_approved = 0 then 1 else 0 end) as NonCompliant
                    FROM claim_form1
                    WHERE  submit_date like '20%'
                    Group By weekEnding;
                    ") );

            return view('reports.processed', compact('totals'));

        }

This is part of my view. In Sublime, everything below the @endforeach has wonky syntax highlighting. It's fine if I don't use @php tags, but I think it's the only way to get my results.

@php
                        $fullTotal = 0
                    @endphp

                    @foreach($totals as $total)
                        <tr>
                            <td>
                                {{ $total->weekEnding }}
                            </td>
                            <td>
                                {{ $total->Compliant }}
                            </td>
                            <td>
                                {{ number_format((($total->Compliant) / ($total->Total) * 100), 2, '.', '') }}%
                            </td>
                            <td>
                                {{ $total->NonCompliant }}
                            </td>
                            <td>
                                {{ number_format((($total->NonCompliant) / ($total->Total) * 100), 2, '.', '') }}%
                            </td>
                            <td>
                                {{ $total->Total }}
                            </td>
                        </tr>
                        @php
                            $fullTotal += $total->Compliant
                        @endphp
                    @endforeach

Thanks!

13th January, 2017

jthorpe left a reply on Issues With @Blade To Excel • 8 months ago

Did you ever end up figuring this out? I'm running into the exact same problem and can't find any other instance of this happening.

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.