This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

neilcarpenter
neilcarpenter

I think what you need to do is before creating a new Actor, search the database for if one exists with the same first and last name already.

There are probably a few ways you could do this, it's worth looking at the laravel or october docs about how to use Eloquent.

One way would be like this...

use your Actor model at the top of your Actorform.php

use use WatchLearn\Movies\Models\Actor;

Then your try and select an actor with the same names from your database. If one doesn't exist - create a new one.

public function onStart()
{
    // If an actor with the same first or last name exists,
    // then $existingActor will be an istance of your Actor model
    // If not, it will have a null value
    $existingActor = Actor::where('first_name', input('first_name'))
                          ->where('last_name', input('last_name'))
                          ->first();

    if ($existingActor) {
        // An actor exist with these names...
        return 'An actor already exists with this name!';
    }

    // Start building up a new instance of your Actor model
    // with the input given via your form 
    $newActor = new Actor();
    $newActor->first_name = input('first_name');
    $newActor->last_name = input('last_name');

    // Save it!
    $newActor->save();

    return 'New actor added successfully';
}

Like I said before, there are lots of ways you could achieve what you're wanting to do. The example i've given is pretty simple.

neilcarpenter
neilcarpenter

Me putting onStart() was actually a typo so you were right to keep it as onSave()

I'm not sure why you're using

<input type="hidden" name="_handler" value="onSave">

Rather than the data attributes or javascript api.

Probably worth your while reading the octobercms ajax framework docs

https://octobercms.com/docs/ajax/introduction

What you're describing with the Al Pacino and Al Bundy scenario sounds very funky...

If you share what you have inside you onSave() handler method, i'll have a pick through it and see if I can figure out what you've got going on.

You can concatanate variables in PHP using ., like this...

// I've added a space inside single quotes to make sure
// the entire name has a space between the first and last name
// be quite odd having a name of Albundy

$fullName = $firstName.' '.$lastName;
neilcarpenter
neilcarpenter

Ok fair enough, well I've not followed those tutorials so not aware of what he's doing.

Anyway, that's not really related to your new issue. The problem you're having now is that your setting the first_name with the inputs first name. But then you're overwriting that value by setting first_name again but with the inputs last_name.

Try

$newActor->first_name = input('first_name');
$newActor->last_name = input('last_name');

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.