petruspetrus's avatar

Laravel architecture

Hi -

Wonder if anybody can help with the following. I'm new to both Laravel and PHP so assuming I know nothing would probably not be a bad starting point :-)

I have a remote database that contains a table holding enquiries from users of a client's website. I want to replicate these enquiries in a local table on my laravel database for processing in a back office system, so each time a user in the back office system opens the 'enquiries' view laravel should poll the remote table, load any new enquiries into the laravel database and then present those and any other enquiries in the laravel database to the user (there may be more elegant solutions but I'm happy with this approach).

So in my web.php I have:

Route::get('/enquiries', 'Enquiry\EnquiryController@extract');

And in my EnquiryController I have:

public function extract() { // log onto remote server and extract enquiries

$production_enquiries = \DB::connection('proddb')->select('select * from wp_enquiries');

// enumerate the returned enquiries and store these into local database foreach($production_enquiries as $enquiry){ \DB::table('enquiries') ->insert(array( 'enquiry_group_id'=> $enquiry ->enquiry_group_id, 'enquiry_newsletter_subscription'=> $enquiry ->enquiry_newsletter_subscription, . . update more fields . 'created_at' => \Carbon\Carbon::now(), # \Datetime() 'updated_at' => \Carbon\Carbon::now() # \Datetime() ) ); } \DB::connection('proddb')->disconnect();

$enquiries = \DB::table('enquiries')->orderBy('enquiry_group_id', 'asc')->paginate(1);

return view('enquiries.index', compact('enquiries')); }

So, this works, after a fashion. The remote server is polled, the laravel table updated and the enquiries are displayed. However, each time I paginate through the enquiries the remote database is re-polled and the enquiries added again. Now, I sort of get that this is because I am reinvoking the @extract everytime I refresh the view through pagination but what I don't understand is where to put the logic to do the database update once and only once.

Leave aside for one moment that my PHP is fairly flakey and I'm trying to learn by doing, at the heart of this I guess is that I don't really understand the Laravel architectural framework well enough to know where to put what and how to separate the logic from the controller . My background is in procedural language stacks and whilst I have programmed in object orientated architectures to a limited extent I'm finding Laravel a bit challenging to figure out.

I'd be grateful for any directions concerning the specifics of the problem itself but also more generally, about best practice in Laravel architectural structures. I need something really that provides a bit more of an end to end approach to building applications that are slightly more complex than the Laravel 5.4 From Scratch. As excellent as that is it is not really helping me understand the architectural complexities of putting an application together.

0 likes
4 replies
NOMGUY's avatar

First of all, what is this?

Route::get('/enquiries', 'Enquiry\EnquiryController@extract');

There's no such way to do it. It is simply,

Route::get('/enquiries', 'EnquiryController@extract');

just go to terminal in your projects directory, and run

php artisan route:list

you'll get to know more about that.

petruspetrus's avatar

NOMGUY - the controller is in a sub-directory of the controllers main directory and so the syntax is okay and it works perfectly fine but thanks for the input

petruspetrus's avatar
public function extract() { 

    // log onto remote server and extract enquiries

    $production_enquiries = \DB::connection('proddb')->select('select * from wp_enquiries');

    // enumerate the returned enquiries and store these into local database 

    foreach($production_enquiries as $enquiry){ 
        \DB::table('enquiries') 
            ->insert(array( 'enquiry_group_id'=> $enquiry ->enquiry_group_id, 
                            'enquiry_newsletter_subscription'=> $enquiry ->enquiry_newsletter_subscription, 
                            //. . update more fields . 
                            'created_at' => \Carbon\Carbon::now(), # \Datetime() 
                            'updated_at' => \Carbon\Carbon::now() # \Datetime() 
                        )
                    ); 
        } 

    \DB::connection('proddb')->disconnect();

    $enquiries = \DB::table('enquiries')->orderBy('enquiry_group_id', 'asc')->paginate(1);

    return view('enquiries.index', compact('enquiries')); 
    }

Sorry, my fault. Too hasty with my copy and paste!

Please or to participate in this conversation.