haakym

Experience

19,540

1 Best Reply Awards

  • Member Since 2 Years Ago
  • 320 Lessons Completed
  • 1 Favorite

20th January, 2017

haakym started a new conversation Preparing The Database Before A Test For A Model With Many Dependencies, Is There A Better Way? • 1 day ago

I am writing a test for a page that performs updates, the test simply checks that one of the model's attributes hasn't changed prior to submitting the form in order to prevent two users from updating the model if they had it both open in their browser at the same time.

The test requires the use of a model in my Laravel web app: App\Models\Application and I am using factories to create some fake data for this model. The test simply checks that one of the model's attributes hasn't changed prior to submitting the update form that will change it.

The Application model has many belongsTo relationships, which in turn requires for the related model to exist prior to the creation of the Application model. For example, the Application model has a belongsTo relationship with Applicant, so I need to create an Applicant model with a factory prior to creating an Application and an Applicant also has belongsTo relationships with User, which I also need to create before ... you get the idea.

So this has left me wondering if I am taking the wrong approach? Is there a more simple approach where I can simply create the model that is under test?

Thanks for any advice.

29th September, 2016

haakym left a reply on Using Vue.js In Laravel 5.3 • 3 months ago

Did you solve this? I am facing the same question...

@Raido

There's a response to my question here if you're interested: http://stackoverflow.com/questions/39708680/using-vue-js-in-laravel-5-3

27th September, 2016

haakym left a reply on Using Vue.js In Laravel 5.3 • 3 months ago

I'm going to repeat the question again as based on some of the answers it seems as I didn't get my point across. Hope this makes things clearer.

In Laravel projects prior to 5.3 I've utilised Vue.js using the script tag like this:

<script type="text/javascript" src="../js/vue.js"></script>

I would then create a Vue instance specific for that page like this:

<script>
    new Vue({
      el: '#app',
      data: {
        message: 'Hello Vue.js!'
      }
    });
</script>

and then bind it to the relevant div#id in my HTML.

Now, in Laravel 5.3 Vue.js comes bundled and I am fully aware that I can use components as described in the docs, however, my question is if I want to create a Vue.js instance like I just mentioned, i.e. where I create a Vue.js instance strictly for a given page (not a component) how do I do it?

Do I set it up like I used to by importing the vue.js library in a script tag firstly or can I use generated app.js?

Am I not supposed to do it, should I be creating components for everything?

For me, it doesn't make sense to make a component for something I am only using once - I thought the purpose of components was that they are reusable. As mentioned in the Vue.js docs:

Components are one of the most powerful features of Vue.js. They help you extend basic HTML elements to encapsulate reusable code.

Any advice would be appreciated, thanks!

haakym left a reply on Using Vue.js In Laravel 5.3 • 3 months ago

@ejdelmonico Thanks for your reply. So are you saying that all interactions with Vue.js should be via components?

26th September, 2016

haakym left a reply on How To "DRY Up" My Tests Set Up • 3 months ago

@ohffs

Haha, at least you're honest!

Thanks for your insights and advice, very helpful! I think I'll probably reach for traits once I get a few more tests knocked out and can begin getting an idea of how I can structure the setup more wisely.

haakym left a reply on How To "DRY Up" My Tests Set Up • 3 months ago

@ohffs Thanks for your reply.

I think that's a good approach to take. How would you go about pulling in the global setup when you need to utilise it?

I like the method name you chose for delete shred() - haha!

haakym started a new conversation Using Vue.js In Laravel 5.3 • 3 months ago

In previous Laravel projects, I've dropped Vue.js into a page and created a Vue instance specific for that page. Something like this at the bottom of my page:

<div id="app">
    @{{ message }}
</div>
...
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.27/vue.js"></script>
<script>
        new Vue({
          el: '#app',
          data: {
            message: 'Hello Vue.js!'
          }
        });
    </script>

Now in Laravel 5.3 Vue.js comes bundled up with it and I have got a slight grasp on how I can create components which I can then drop into a page. However, I am struggling to understand how I can create a Vue.js instance like the aforementioned, i.e. where I just need to drop a vue instance into a page instead of creating a whole component.

Can I utilise Vue that gets incorporated into app.js when running gulp or do I need to pull in Vue as I did before with the script tag?

Thanks!

haakym left a reply on Vou-Router • 3 months ago

@LJ90

I maybe should mention that the vue-devtools in my chrome browser do not appear in the browser (and I have absolutely no idea why), so I cannot check for errors.

I have had this issue before when I'm using minified vue vue.min.js - try using it when it's not minified and see if that works.

haakym left a reply on How To "DRY Up" My Tests Set Up • 3 months ago

@ohffs I haven't actually. I'll be sure to check it out, thank you.

@ifpingram Thanks for your response. You really addressed my concerns there!

Perhaps my question should have been, should I be DRYing up my test setup - not how. I think I had the idea that a particular approach may have tackled the issues you mentioned more effectively. For now, I think I'll leave my tests alone and see how things pan out once I've written some more tests. Thanks again.

haakym started a new conversation How To "DRY Up" My Tests Set Up • 3 months ago

Hey everyone. I'm trying to practice a little TDD after watching Adam Wathan's videos at Laracon etc. I've got about three test classes so far and I seem to be doing a lot of setup that involves adding data to the DB that's required for the test. I would like to know what would be the best approach to clearing up such repetitive code from my tests and to further utilise this as my number of tests increase.

Some of the methods from my tests with similar setup flow looks like this:

  1. create a user(s)
  2. create a department the user(s) belongs to
  3. create a resource related to the user(s)

AuthenticationTest.php

/** @test */
public function user_is_logged_in()
{
    $user = factory(User::class)->make([
        'username' => 'jsmith',
        // ... some data
    ]);

    $department = factory(Department::class)->create(['name' => 'IT Department']);
    
    $department->users()->save($user);

    // sets $_SERVER['REMOTE_USER'] variable via SSPI
    $this->setRemoteUser($user->username); 

    $this->visit('/')
        ->assertEquals($user->username, Auth::user()->username);
}

AccountTest.php

/** @test */
public function user_can_view_their_account()
{
    $user = factory(User::class)->make([
        // ... some data
    ]);

    $department = factory(Department::class)->create(['name' => 'IT Department']);
    
    $department->users()->save($user);

    $employeeDetails = factory(EmployeeDetail::class)->make([
        // ... some data
    ]);
    
    $user->employeeDetails()->save($employeeDetails);

    Auth::login($user);
    
    $this->visit('/account')
         ->see($user->name)
         ->see($user->department->name)
         ->see($user->employeeDetails->start_date->format('d-m-Y'))
         ->see($user->employeeDetails->employee_type);
}

UserEmployeeRequestTest.php

/** @test */
public function user_can_view_their_requests()
{
    $employee = factory(User::class)->make([
        // ... some data
    ]);

    $manager = factory(User::class)->make([
        // ... some data
    ]);

    $department = factory(Department::class)->create(['name' => 'IT Department']);
    
    $department->users()->save($employee);
    $department->users()->save($manager);

    $employeeRequest = factory(EmployeeRequest::class)->make([
        // ... some data
        'manager' => $manager->id,
    ]);
    
    $employee->employeeRequests()->save($employeeRequest);

    Auth::login($employee);
    
    $this->visit('/employee/employee-requests')
        ->see(/* data from employee request */);;
}

So, as far as I'm aware, I have some of the following options:

  1. Extract the setup to a method on a base class
  2. Extract to a trait(s)
  3. Extract to a setUp() method on the class and call parent::setUp()

I'd appreciate any recommended ways to clear up the repetitive code while keeping it flexible enough to scale as I write more tests that may require different or more set up data. Thanks!

22nd September, 2016

haakym left a reply on Submitting Form In Vue.js With Ajax • 3 months ago

@sorcjc What does your console say - any errors?

22nd July, 2016

haakym started a new conversation Add Watermark/stamp To PDF With Preview • 5 months ago

In an application I'm building the admin users are currently downloading a PDF file then manually adding 3 image files as a stamp/watermark then re-uploading that PDF.

If at all possible, I would like to add this functionality to the web application. The functionality I'm looking for would allow the user to view the PDF in the webpage and decide where the stamp is placed using up/down, left/right buttons then once decided they could place the stamp in the desired location onto the PDF and add another if needed. The reason for this is because sometimes the stamp may not be visible if added to the same place each time due to content in the PDF.

So far I've looked at FDPF which I've used before to merge two PDF files together - I'm guessing this would help me achieve the watermark part, but how could I go about doing the preview part?

Any ideas or suggestions are welcome. I'm not restricted to PHP, any reasonable solution in PHP, JS or a script that could run in a Laravel application would be fine.

Thanks!

23rd May, 2016

9th May, 2016

haakym left a reply on Correct Approach To Creating Record Then Immediately Adding One-to-one Relation • 8 months ago

I'm trying this code:

$applicant = Applicant::create($this->applicantDataFromRequest($request));

$application = new Application([
    'status'           => 'pending',
    'reference_number' => $request->get('passport_number'),
    'organisation_id'  => $request->get('organisation'),
    'qualification_id' => $request->get('qualification') ?: null,
]);

$applicant->application()->save($application);

dd('done');

And still getting the same error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`attestation`.`applications`, CONSTRAINT `applications_applicant_id_foreign` FOREIGN KEY (`applicant_id`) REFERENCES `applications` (`id`) ON DELETE CASCADE) (SQL: insert into `applications` (`status`, `reference_number`, `organisation_id`, `qualification_id`, `applicant_id`, `updated_at`, `created_at`) values (pending, 5730970933C7B, 24, 1, 22, 2016-05-09 14:56:25, 2016-05-09 14:56:25))

haakym left a reply on Correct Approach To Creating Record Then Immediately Adding One-to-one Relation • 8 months ago

@pmall

Thanks, I updated the question from:

$application = $applicant->applications()->create([
    // data
]);

to:

$application = $applicant->application()->create([
    // data
]);

haakym left a reply on Correct Approach To Creating Record Then Immediately Adding One-to-one Relation • 8 months ago

@desaijay123 so I would need to pass it a new instance rather than array in that case:

$application = new Application([
    // data
]);

$application = $applicant->application()->save($application);

haakym left a reply on Correct Approach To Creating Record Then Immediately Adding One-to-one Relation • 8 months ago

@pmall

Sorry! It's a one-to-one relation. Corrected the question.

haakym started a new conversation Correct Approach To Creating Record Then Immediately Adding One-to-many Relation • 8 months ago

I've got the following eloquent models and relations set up:

Applicant

class Applicant {
    public function application()
    {
        return $this->hasOne(Application::class);
    }
}

Application

class Application {
    public function applicant()
    {
        return $this->belongsTo(Applicant::class);
    }
}

I simply want to create a new applicant then add a new application for the applicant just created:

$applicant = Applicant::create([
    // data
]);

$application = $applicant->applications()->create([
    // data
]);

However, when running this code I receive the following error:

SQLSTATE[23000]: Integrity constraint violation: 
1452 Cannot add or update a child row: a foreign key constraint fails (
    `attestation`.`applications`, CONSTRAINT `applications_applicant_id_foreign` 
    FOREIGN KEY (`applicant_id`) REFERENCES `applications` (`id`) ON DELETE CASCADE
)
(SQL: insert into `applications` (
    `status`,
    `reference_number`,
    `organisation_id`,
    `qualification_id`,
    `applicant_id`,
    `updated_at`,
    `created_at`
) values (
    pending,
    573066CE59BFE,
    24,
    1,
    12, // <------ applicant_id, i.e. the foreign key!
    2016-05-09 11:30:38,
    2016-05-09 11:30:38
))

As I understand, the error is occuring because I'm passing it a foreign key that doesn't exist in the database. So I was wondering what is happening here with the first call to the DB that I make to create the applicant:

$applicant = Applicant::create([
    // data
]);

Is it not written to the DB before the second statement that creates the application:

$application = $applicant->applications()->create([
    // data
]);

... and thus causing the integrity constraint violation? Because if I dd($applicant) immediately after creating it or dd(Applicant::find($applicant->id)) I get the record and I can also view it in the DB.

I've found that prior to re-factoring my code when I was calling create() to add a new record then building up a new application line by line and using save(), I didn't encounter this issue. So I'm curious to know how both calls differ (if they do) and what would be the correct way to approach such a scenario as I thought it was something very simple with eloquent which I've done many times before. Any advice would be most appreciated, thanks!

3rd May, 2016

haakym left a reply on Testing A Form, Dropdown Issues Where Dropdown Is Populated Via Vuejs • 8 months ago

Great, thanks for the suggestion @ohffs I will check out the video and see if its manageable.

Jeffrey Way did mention the following on an AMA on hashnode last month:

One pain point for me at the moment would relate to testing code that requires JavaScript. Right now, we have a great API in place for integration testing...but, once you introduce JavaScript, it gets a little tricky. I'm hoping that someone will PR a Selenium driver that allows us to use the exact same API we currently have, but leverage Selenium behind the scenes.

https://hashnode.com/ama/with-jeffrey-way-cimd458uo01bwtg53yed55n8n#cimdltl9l01eltg53in0oaiqk

Here's hoping it gets implemented.

haakym left a reply on Question On Book: PHP Objects, Patterns, And Practice • 8 months ago

@robgeorgeuk Looks like there is something in the works! No idea when it will be completed though?

Just signed a contract to write PHP Objects Patterns and Practice Edition 5 - should be a fun one!

https://twitter.com/getInstance_mz/status/685193564368814085

haakym left a reply on Question On Book: PHP Objects, Patterns, And Practice • 8 months ago

Thanks a lot for the suggestions guys!

25th April, 2016

haakym started a new conversation Question On Book: PHP Objects, Patterns, And Practice • 8 months ago

Hey everyone,

At the following link https://laracasts.com/recommended-reading the book "PHP Objects, Patterns, and Practice 4th ed. 2013 Edition" by Matt Zandstra is recommended.

I wanted to purchase the book on kindle, but as the book was published in 2013 I was curious to know whether a large part of the content would be outdated or not, or if there's a newer version in the works that I should wait for.

Any advice on this or recommendations for other books would be appreciated.

Thank you!

20th March, 2016

haakym started a new conversation Testing A Form, Dropdown Issues Where Dropdown Is Populated Via Vuejs • 10 months ago

I'm writing a small test for a form and I'm getting errors when running phpunit. It gets stuck because it can't see any options in one of the dropdown boxes named organisation which is populated by vuejs. The dropdown in question is populated with a list of json values loaded when the page first loads up and is further filtered by the first dropdown list in the form, namely academic_certificate.

Here's my view/blade file with the dropdown:

<select name="organisation" id="" class="form-control" {{-- required="required" --}} v-model="organisation">
    <option value="">Select</option>
    <option v-for="organisation in organisations | filterBy application_type in 'organisation_types'">
        @{{ organisation.name }}
    </option>
</select>

Here's my test:

/** @test */
public function verified_user_can_create_academic_certificate_application()
{
    $faker = Faker::create();

    $user = factory(App\Models\User::class)->create();
    $applicant = Role::where('name', 'applicant')->firstOrFail();
    $user->roles()->attach($applicant->id);
    
    $this->actingAs($user)
        ->visit('/applicant/application/create')
        ->select('academic_certificate', 'application_type')
        ->type($faker->firstNameMale, 'first_names')
        ->type($faker->lastName, 'last_name')
         // fill out rest of form ...
        /****************************************
            here's where it gets an error!
        ****************************************/
        ->select('Some organisation', 'organisation')   
        ->select('1', 'qualification')
        ->type('IT', 'speciality')
        ->type('2002', 'year_of_award')
        ->press('Submit')
        ->see('complete');
}

The test:

$ vendor/bin/phpunit
PHPUnit 4.8.23 by Sebastian Bergmann and contributors.

E.FF

Time: 7.15 seconds, Memory: 37.00Mb

There was 1 error:

1) ApplicationTest::verified_user_can_create_academic_certificate_application
InvalidArgumentException: Input "organisation" cannot take "Some organisation" as a value (possible values: ,
                                                {{ organisation.name }}
                                            ).

C:\wamp\www\attestation\vendor\symfony\dom-crawler\Field\ChoiceFormField.php:140
C:\wamp\www\attestation\vendor\symfony\dom-crawler\FormFieldRegistry.php:128
C:\wamp\www\attestation\vendor\symfony\dom-crawler\Form.php:77
C:\wamp\www\attestation\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:808
C:\wamp\www\attestation\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:788
C:\wamp\www\attestation\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\InteractsWithPages.php:775
C:\wamp\www\attestation\tests\acceptance\applicant\ApplicationTest.php:55

Any help would be really appreciated, I'm trying to get to grips with testing and would hate to get stuck here! Thank you.

14th March, 2016

haakym left a reply on Generating A Unique ID/Code For Resource • 10 months ago

@premsaurav

No, not really. The main reason I was asking the question is that firstly I was wondering if there are any issues with the methods that I have considered, i.e. uniqid() and if there's a better way that others may have used in their projects.

haakym started a new conversation Generating A Unique ID/Code For Resource • 10 months ago

Apologies if this has already been addressed on the fourms, wasn't entirely sure what to search for.

I'm developing an application where a user submits an Application. Each Application needs to have a unique ID or code which will primarily be used by the user and the system admins in order to identify an particular Application.

My question is what would be the best way to generate this ID/code in PHP? I have the following considerations for the ID/code:

  1. It has to be unique
  2. It can't be too long (if the applicant phones up we don't him reading a 30/40 digit number over the phone)

I've considered using uniqid() or uniqid(rand()) which generates a unique string of 13 and 23 characters respectively. From my searching around I've also seen UUID been mentioned but didn't feel it was right for my use case.

Any suggestions or guidance on this would be appreciated.

Thank you!

26th February, 2016

haakym left a reply on Dynamic Eloquent Relationship • 10 months ago

Many thanks @harryg and @pmall

I've implemented some of the suggestions you made and the structure although I think I could do a bit more refining the database is looking a lot better now! The adam wathan video was quite useful too.

Thanks again for your helpful advice.

haakym started a new conversation Working A Large Number Of Tasks Into A Queue • 10 months ago

I'm building a web application using Laravel 5 that creates links to the web application that when visited show a form for a student's progress report. These links are sent by the web application to an email of a contact at the institution the student attends in order for them to complete the progress report accessed by the link in the email.

The problem I face is when creating and sending links. I have some code which works fine with a couple of hundred students, however in real world use the application would be potentially creating and sending 3000 or so links at one time. The code I've written simply can't handle such a large number and the application crashes.

Although I'm more than welcome to other suggestions, I believe the answer to the problem is utilising queues. I have already used queues when sending email, but I would like to work some other parts of the code into queues but I'm a bit unsure how to do this!

Brief database schema

Student hasMany Link

Student hasMany InstitutionContact (limited to two by my application)

Link hasMany InstitutionContact (limited to two by my application)

Email manyToMany Link

What I am trying to accomplish

  • Get all the Student's that require a new Link

  • Create a Link for each Student

  • Assign the Student's current InstitutionContacts to the Link's InstitutionContact

  • Loop through all the newly created Links in order to group them together by shared InstitutionContacts - this is so an email is not sent per link (thus possibly sending too many emails to the same address), rather links should be grouped together by the same email/contact and sent together where applicable

  • Loop through all the Links grouped by email/contact and:

    1. Send an email including the Link's info to the designated InstitutionContact's email address
    2. Write a copy of the Email to the database
    3. Join the Email created in the former step to the Link(s) that were sent in it

So the main challenge I'm facing is performing the aforementioned task with a large dataset. I have already considered creating and sending a Link one by one via a queue, however this wouldn't allow me to group all the Links together by contact/email. As the task wouldn't be performed regularly I would be open to consider performing the task as it is already with an increase in memory and time for the process, however I didn't have much success when attempting this using set_time_limit(0); and ini_set('memory_limit','1056M'); before sending any links.

Any help would be really appreciated, thank you if you read this far!

Code

app\Http\Controllers\LinkController.php

public function storeAndSendMass(Request $request)
{
    $this->validate($request, [
        'student_id' => 'required|array',
        'subject'    => 'required|max:255',
        'body'       => 'required|max:5000',
    ]);

    $studentIds = $request->get('student_id');
    $subject    = $request->get('subject');
    $body       = $request->get('body');
    
    $students = $this->student
        ->with('institutionContacts')
        ->whereIn('id', $studentIds)
        ->where('is_active', 1)
        ->get();

    // create link, see Link.php below for method
    $newLinks = $this->link->createActiveLink($students);

    // send link to student's contact(s), see LinkEmailer.php below for method
    $this->linkEmailer->send($newLinks, ['subject' => $subject, 'body' => $body], 'mass');

    // return
    return response()->json([
        'message' => 'Creating and sending links'
    ]);
}

app\Models\Link.php

public function createActiveLink($students)
{
    $links = [];

    foreach ($students as $student) {
        $newLink = $this->create([
            'token'          => $student->id, // automatically hashed
            'status'         => 'active',
            'sacb_refno'     => $student->sacb_refno,
            'course_title'   => $student->course_title,
            'university_id'  => $student->university_id,
            'student_id'     => $student->id,
            'institution_id' => $student->institution_id,
            'course_id'      => $student->course_id,
        ]);

        // $newLink->save();

        $studentContacts = $student->institutionContacts;

        if ($studentContacts) {

            foreach ($studentContacts as $studentContact) {
                
                $newLink->contacts()->create([
                    'type'                   => $studentContact->pivot->type,
                    'institution_contact_id' => $studentContact->pivot->institution_contact_id/*,
                    'link_id'                => $linkId,*/
                ]);

                $newLink->save();
            }

        }

        $links[] = $newLink->load('student');
    }

    return $links;
}

app\Emails\LinkEmailer.php

namespace App\Emails;

use App\Emails\EmailComposer;

class LinkEmailer
{
    protected $emailComposer;

    public function __construct(EmailComposer $emailComposer)
    {
        $this->emailComposer = $emailComposer;
    }

    public function send($links, $emailDetails, $emailType)
    {        
        $contactsAndLinks = $this->arrangeContactsToLinks($links);

        foreach ($contactsAndLinks as $linksAndContact) {
            
            $emailData = array_merge($linksAndContact, $emailDetails);
            
            // send/queue email
            \Mail::queue('emails/queued/reports', $emailData, function ($message) use ($emailData) {
                $message
                    ->to($emailData['email'], $emailData['formal_name'])
                    ->subject($emailData['subject']);
            });
            
            // compose email message, returns text of the email
            $emailMessage = $this->emailComposer->composeMessage($emailData);

            // // create Email
            $email = \App\Models\Email::create([
                'to'      => $emailData['email'],
                'from'    => 'report@domain.org',
                'subject' => $emailData['subject'],
                'body'    => $emailMessage,
                'type'    => $emailType,
                'user'    => $_SERVER['REMOTE_USER']
            ]);

            foreach ($linksAndContact['links'] as $link) {
                $link->emails()->attach($email->id);
            }
        }
    }

    // group links by contact
    public function arrangeContactsToLinks($links)
    {
        $contactsForLinks = [];
        $assigned         = false;
        $match            = false;
        
        foreach ($links as $link) { // 1, n

            if ($link->contacts) {

                foreach ($link->contacts as $contact) { // 1, 2
                    
                    if ($contactsForLinks) {
                        
                        $assigned = false;

                        foreach ($contactsForLinks as $key => $contactLink) { // n
                            // assign links to existing email in array
                            if ($contactLink['email'] === $contact->institutionContact->email) {
                                $match = false;

                                // check link hasn't already been included
                                foreach ($contactsForLinks[$key]['links'] as $assignedLink) {
                                    if ($assignedLink === $link) {
                                        $match = true;
                                    }
                                }

                                // if there was no match add to list of links
                                if (!$match) {
                                    $contactsForLinks[$key]['links'][] = $link->load('student');
                                    $assigned = true;
                                    break;
                                }
                            }
                        }

                        if (!$assigned) {
                            $contactsForLinks[] = [
                                'email'                 => $contact->institutionContact->email,
                                'formal_name'           => $contact->institutionContact->formal_name,
                                'requires_id'           => $contact->institutionContact->institution->requires_id,
                                'requires_course_title' => $contact->institutionContact->institution->requires_course_title,
                                'links'                 => [$link->load('student')],
                            ];
                        }
                    } else {
                        $contactsForLinks[] = [
                            'email'                 => $contact->institutionContact->email,
                            'formal_name'           => $contact->institutionContact->formal_name,
                            'requires_id'           => $contact->institutionContact->institution->requires_id,
                            'requires_course_title' => $contact->institutionContact->institution->requires_course_title,
                            'links'                 => [$link->load('student')],
                        ];
                    }
                }
            }
        }

        return $contactsForLinks;
    }
}

25th February, 2016

haakym left a reply on How To Run Queue:work Correctly? • 10 months ago

Ahhhh man I feel silly! I'm pretty sure I'd checked the database before and found the jobs still in there but it seems to be firing them off just fine now I've double checked.

Also, that's really useful to know that queue:work doesn't give the same feedback as queue:listen.

Thanks so so much for your help Jeroen! Really appreciate it!

haakym started a new conversation How To Run Queue:work Correctly? • 10 months ago

I've got a system that will potentially send up to 1000s of emails at once. I decided to use Laravel's queue functionality opting for the inbuilt database queue.

I can happily fire off jobs using artisan queue:listen and was wondering if I can save resources after reading that I can run artisan queue:work --daemon. However whenever I run artisan queue:work --daemon nothing happens? queue:listen on the other hand seems to work just fine. Do I need to run queue:listen alongside queue:work?

As I understand from this post: https://laracasts.com/discuss/channels/general-discussion/queue-worker-vs-daemon-queue-worker

queue:listen fires queue:work so a bit unsure why it isn't working to begin with?

Any help would be appreciated, thanks!

22nd January, 2016

haakym left a reply on How Do I Check If The User Is Logged In From A View? • 11 months ago

Pretty sure it must be the same. From the docs:

Determining If The Current User Is Authenticated

To determine if the user is already logged into your application, you may use the check method on the Auth facade, which will return true if the user is authenticated:

if (Auth::check()) {
    // The user is logged in...
}

However, you may use middleware to verify that the user is authenticated before allowing the user access to certain routes / controllers.

https://laravel.com/docs/5.2/authentication#retrieving-the-authenticated-user

6th January, 2016

haakym left a reply on Dynamic Eloquent Relationship • 1 year ago

@harryg

Yep that's how I've got my course relationship set up. Okay that's pretty straight forward now I think about it, I think I'm still too wrapped up in my old DB structure to think outside the box! Ideally the latest report method would be return a relationship because I need to use it with the ->has and ->doesntHave eloquent methods (which I think only work with relationships?) so I'm guessing I can do something like:

public function latestReport()
{
    return $this->reports()
        ->where('course_id', $this->course_id)
        ->latest('date_to');
}

@pmall

That's quite a different approach to what I'm currently doing with the student - course relationship (which is currently one to many). I do need a way to keep track of the student's current course, so I'm assuming I'd keep the student's current course on the student model but then add an entry into the student/course table when creating a report to link it all up?

To both harryg and pmall - Thanks so much for your input on my question I really do appreciate your responses! I don't think I'll be able to implement all the suggestions you have made just yet due to time constraints on the project, but I definitely hope to come back to it very soon and implement your suggestions then hopefully post back here to benefit others and let you know what solution worked for me.

5th January, 2016

haakym left a reply on Dynamic Eloquent Relationship • 1 year ago

@harryg

Thanks again for your response, however I don't think it addresses how to retrieve the last report for the student's current course or even a specified course, unless I'm missing something.

Just to reiterate, I don't want the relationship to ONLY get the latest report, I want it to get the latest report which would be the report that is related to their course. So for example, if the student's current course is master I want to retrieve the latest MasterReport linked to that student.

Thanks

haakym left a reply on Dynamic Eloquent Relationship • 1 year ago

@pmall and @harryg - Many thanks for your replies and taking the time to explain the correct way to go about this.

I think I was failing to understand that each "subreport" (e.g. PhdReport) would have/be linked to a parent Report instance earlier.

However, I'm still failing to understand how I would implement the relation to retrieve the latest report for the student's current course using the approach you guys have suggested.

Just to make things clear, let's say we have a student whose current course is master and this student has two reports an EslReport and a MasterReport, how is the latestReportrelation implemented to retrieve the report for his current course (the master report)???

haakym left a reply on Dynamic Eloquent Relationship • 1 year ago

@pmall Thanks a lot for your reply.

By Document I assume you mean Report?

I'm not entirely sure how a polymorphic relationship solves the issue, but I'm most probably missing something!

Currently I have a abstract Report model which is the parent model of PhdReport, MasterReport, BachelorReport, FoundationReport and EslReport with each of the 5 aforementioned reports being different in their attributes. I can't see how I can work a ReportType model into this current set up as wouldn't this require me to scrap the 5 report models in favour of one Report model therefore assuming all the attributes for each report be the same?

I'll also add that a student could begin by being on the ESL course and therefore have an EslReport then later change to Master and have a MasterReport. What I was trying to do with the relation in the first post is get the latest report for the student's current course.

haakym left a reply on Dynamic Eloquent Relationship • 1 year ago

Current solution is dynamically changing the relationship called, e.g.

$courseName = 'Master';

$students = Student::with('latest' . $courseName . 'Report')->get();

This does require a method for each report type in the student model which I'm guessing isn't the best solution but for now it works!

haakym started a new conversation Dynamic Eloquent Relationship • 1 year ago

I'm using eloquent and trying to set up what I would best describe as a dynamic relationship.

I have a Student model that can be on a Course: PhD, Master, Bachelor, Foundation or ESL and a student can have a report specific to each course, therefore I have the following eloquent models: PhdReport, MasterReport, BachelorReport, FoundationReport or EslReport.

One such relationship I require is to get the latest report for the student's current course. So if the student is on the bachelor course I want to grab the BachelorReport model where the date_to attribute is the most recent. This is my implementation on the Student model for the relationship:

public function latestReport()
{
    $courseReport = ucfirst(strtolower($this->course->name)) . 'Report'; 

    return $this->hasOne('\App\Models\\' . $courseReport)->latest('date_to');
}

This relationship works fine when I run the following code in artisan tinker:

// get a student
$student = App\Models\Student::first();

// student returned
=> <App\Models\Student #000000003dd8f51b00000000d869fe64> {
       course_id: 2, /* which is the master course */
       // other attributes ...
}

// get the latest report for the student's current course
$s->latestReport

// we get a MasterReport as expected
=> <App\Models\MasterReport #000000003dd8f51900000000d869fe64> {
    // attributes
}

However, when I run the following code:

$students = $this->student
    ->where('course', 2)
    ->where('is_active', '1')
    ->with('latestReport', /* other relationships */)
    ->get();

It always appears to run the code for PhdReport, this is shown when I check the Laravel debugbar

select * from `phd_reports` 
  where `phd_reports`.`student_id` in (/* list of ids */) 
  order by `date_to` desc

My current idea for a workaround is to conditionally apply a relation specific to the course/report:


$students = $this->student
    ->where('course', 2) /* for masters */
    ->where('is_active', '1');

if ($course == 'master') {
    $students = $students->with('latestMasterReport');
}

Any advice would be much appreciated. Thank you!

7th December, 2015

haakym left a reply on Best Practice For Date Conversion For View Data/data From Ajax • 1 year ago

A question I asked on Stackoverflow got a working answer for me, code:

Route::get('/', function () {

    $student = App\Models\Student::first();

    $response = new League\Fractal\Resource\Item($student, new App\Transformers\Models\StudentTransformer);

    $manager = new \League\Fractal\Manager();
    $manager->setSerializer(new \League\Fractal\Serializer\ArraySerializer());

    return response()->json($manager->createData($response)->toArray());

});

Link: http://stackoverflow.com/questions/34017140/transform-eloquent-model-using-fractal

haakym left a reply on Assign A Query To Variable To Then Perform Two Separate Queries? • 1 year ago

@screenager Again, I don't see how your code with altered syntax addresses the question. Thanks for your comment anyway though, it's always good to get a different perspective.

Yes, you're right I think I can use filters as one approach, thanks a lot.

As for the question I asked: can I start an eloquent query, assign it to a variable then continue using the variable for two separate queries without them conflicting with one another, the feedback I got from stackoverflow was no I can't, so use query scopes or I could use filters as you suggested.

http://stackoverflow.com/questions/34131581/assign-a-query-to-variable-to-then-perform-two-separate-queries#34131892

haakym left a reply on Assign A Query To Variable To Then Perform Two Separate Queries? • 1 year ago

@screenager I don't see how that answers the question? All you've done is modify the syntax of the code snippet I put in my question. Please clarify further if you can.

haakym started a new conversation Assign An Query To Variable To Then Perform Two Separate Queries? • 1 year ago

Hey everyone, just wondering if I can start an eloquent query, assign it to a variable then continue using the variable for two separate queries without them conflicting with one another. A simple example:

$students = $this->student
    // more query stuff
    ->where('is_active', 1);

$bachelorStudents = $students
    ->where('course_id', 3)
    ->get();

$masterStudents = $students
    ->where('course_id', 4)
    ->get();

or would I need to do:

$bachelorStudents = $this->student
    ->where('course_id', 3)
    ->get();

$masterStudents = $this->student
    ->where('course_id', 4)
    ->get();

I always thought I could do the former, but some of my results appear to show I can't but I am open to believe that if you can do it then perhaps I'm doing something wrong.

4th December, 2015

haakym started a new conversation How To Access Child Component Data In V-if Outside Of Component? • 1 year ago

Hey everyone.

In short, I want to get a value from a child component and check what it is in the parent. I have a working implementation using computed properties and a reference via v-ref on the child component, but I was wondering if I am doing it the right way and if there's a better/proper way to do it.

To get to specifics, I have a component with checkboxes, the checked checkboxes' values within this component are kept in the components data in an array variable named selected. Outside of the component I want to conditionally show a <div> using v-if however I'm unsure how to correctly grab the child component's selected value.

Here's a brief overview of my code:

component mark up

<student-table
    v-ref:student-table
    :data="students"
    :course="course"
    :columns="columns"
>
</student-table>

component registration

Vue.component('student-table', {

    /* unrelated code */

    data: function () {
        return {
            selected: []
        }
    },

    /* unrelated code */
})

main vue instance

var vueApp = new Vue({
    /* unrelated code */
    
    computed: {
        selected: function () {
            return this.$refs.studentTable.selected.length
        }
    },
    
    /* unrelated code */
})

Then in my html I can reference selected and I'll get the length of StudentTable.selected and thus be able to use it in my v-if

Thanks for any guidance or help!

30th November, 2015

haakym left a reply on Best Practice For Date Conversion For View Data/data From Ajax • 1 year ago

Yes, thank you. Have updated the question to indicate it's not working.

If I return it in a response like so:

Route::get('/', function () {

    $student = App\Models\Student::first();

    $response = new League\Fractal\Resource\Item($student, new App\Transformers\Models\StudentTransformer);

    return response()->json([$response]);

});

I get:

[
    { }
]

What makes you think the transforming is fine? Where is the transformed data supposed to reside in the return transformed object? In the dd() code in my last post the original eloquent model is sitting in Item->data and the data is all the same, no transformations. I'm clearly missing something here!

Thanks for your input so far @JoeDawson most appreciated

haakym left a reply on Best Practice For Date Conversion For View Data/data From Ajax • 1 year ago

Yes sorry about that I failed to mention that properly.

If I run the following code:

Route::get('/', function () {

    $student = App\Models\Student::first();

    $response = new League\Fractal\Resource\Item($student, new App\Transformers\Models\StudentTransformer);

    dd($response);

});

I get this:

Item {#299 ▼
  #data: Student {#305 ▼
    #fillable: array:10 [▶]
    #connection: null
    #table: null
    #primaryKey: "id"
    #perPage: 15
    +incrementing: true
    +timestamps: true
    #attributes: array:13 [▶]
    #original: array:13 [▶]
    #relations: []
    #hidden: []
    #visible: []
    #appends: []
    #guarded: array:1 [▶]
    #dates: []
    #dateFormat: null
    #casts: []
    #touches: []
    #observables: []
    #with: []
    #morphClass: null
    +exists: true
  }
  #meta: []
  #resourceKey: null
  #transformer: StudentTransformer {#301 ▼
    #availableIncludes: []
    #defaultIncludes: []
    #currentScope: null
  }
}

haakym left a reply on Best Practice For Date Conversion For View Data/data From Ajax • 1 year ago

@milon my issue is not how to convert dates, I'm quite comfortable with doing that, but my issue is how to get the transformer working.

Right now it's returning an empty response! Any ideas why the fractal transformer isn't working?

haakym started a new conversation Best Practice For Date Conversion For View Data/data From Ajax • 1 year ago

I am using Laravel and Vue js and populating the data in the vue instace using ajax calls, the data is coming from my eloquent models.

I have date fields in my model that as default shows the data in the following format: Y-m-d H:i:s. I would like to convert these dates to a more human readable format e.g. d-m-Y when sending the data to my webpage via ajax calls that populate my vue instance. I'm aware of the following ways of accomplishing this:

  1. Render at the client end using something like momentjs - I don't think this option is a good idea because the view shouldn't really be doing such work (if it can be avoided) - right?

  2. Using something fractal transformer - I have tried to implement this and so far I am failing. This is my code so far that I've been using as a test:

Transformer for my Student model:

<?php

namespace App\Transformers\Models;

use App\Models\Student;
use League\Fractal;

class StudentTransformer extends Fractal\TransformerAbstract
{
    public function transform(Student $student)
    {
        return [
            'name'           => $student->name,
            'birth_date'     => date('d-m-Y', strtotime($student->birth_date)),
            'start_date'     => date('d-m-Y', strtotime($student->start_date)),
            'is_active'      => $student->is_active ? 'Yes' : 'No',
            'course_title'   => $student->course_title,
            'university_id'  => $student->university_id,
            'institution_id' => $student->institution_id,
            'course_id'      => $student->course_id,
        ];
    }
}

Routes file:

Route::get('/', function () {

    $student = App\Models\Student::first();

    $response = new League\Fractal\Resource\Item($student, new App\Transformers\Models\StudentTransformer);

    return response()->json([$response]);

});
  1. Using a view presenter - I have considered using https://github.com/laravel-auto-presenter/laravel-auto-presenter, but I believe this would only help in passing data to the view, but would not help for ajax calls - right?

Any advice would be appreciated on this! Thanks.

26th November, 2015

haakym left a reply on Updating A Pivot Table • 1 year ago

@niells Thanks again for your responses!

PS: you would still require to make 100 inserts if you have 100 students, not 50, or maybe i misunderstood your requirements.

Yes you're right, sorry I was getting my calculations wrong (long day!)

In the end I decided to go for the delete using the DB facade then attaching the new items. My code looks something like this now:

// delete
\DB::table('institution_contact_student') // the pivot table
            ->whereIn('student_id', $studentIds)
            ->where('type', $contactType)
            ->delete();

// add
foreach ($students as $student) {
    $student->institutionContacts()->attach([$contactId => ['type' => $contactType]]);
 }

19th November, 2015

haakym left a reply on Updating A Pivot Table • 1 year ago

@niells

Thanks so much for your reply, I appreciate the example of using the withPivot function.

As an alternative, do you think it's worth running a DB query to perform the delete then I can perform the records using attach. So if I had 100 students to run through it would be 1 for the delete and 50 for the insert. Cut down by half pretty much.

DB::table('institution_contact_pivot')
    ->whereIn('student_id', $studentIds)
    ->where('type', 1)
    ->delete();

haakym started a new conversation Updating A Pivot Table • 1 year ago

I've got a many to many relationship between a student and a institution_contact.

students should only ever have two institution_contacts and I have a attribute on the pivot table named type to be set as 1 or 2.

So, my pivot table looks like this: institution_contact_student: id, institution_contact_id, student_id, type

I've run into difficulty in deciding how to approach the issue of adding/updating the pivot table. Let's say I have 100 students and I want to assign them a contact with the type of 1.

My current solution is to delete the contact then add it:

$students = Student::all(); // the 100 students

foreach ($students as $student) {
            // remove existing contact
            $student->institutionContacts()->newPivotStatement()->where('type', $contactType)->delete();

            // add new contact
            $student->institutionContacts()->attach([$contactId => ['type' => $contactType]]);
        }

However, I'm thinking that this is going to hit the database twice for each student, right? So would I be better off creating a model for the pivot table and removing all entries that matched the student id and the type then simply adding the new one? Or would creating a model for the pivot table be considered bad practice and is there a better way of accomplishing this that I've missed?

22nd October, 2015

haakym left a reply on The Factory And Abstract Factory Design Patterns • 1 year ago

Yes please! Have done a "beginner" implementation on a system I'm working on. Would love to make it a proper implementation though!

Edit Your Profile
Update

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