How do I call model function on create event? Laravel-5

jackjoesmith

I'm trying to create a referral url when a user is first created. My function inside my User model looks like this:

private function make_url()
{
    $url = str_random(40);
    $this->referral_url->url = $url;
    if ($this->save()){
        return true;
    }
    else{
        return false;
    }
}

Within the model, I've tried doing this but didn't work

USER::creating(function ($this){
$this->make_url();
})

I also tried calling it in my User Controller within the create user action

public function create(UserRequest $request)
{
$data = $request->all()
$data['password']= bcrypt($request->input('password'));

if($user=User::create($data))
{
  $user->make_url();
}
}

I get this error in return

Indirect modification of overloaded property App\User::$referral_url has no effect

Thanks in advance for your help guys =]

p.s: If there's a better way to go about creating referral urls please tell me.

update

My entire user model

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    protected $table = 'users';

    protected $fillable = [
                            'first_name',
                            'last_name',
                            'url',
                            'email',
                            'password',
                            'answer_1',
                            'answer_2',
                            'answer_3'
                            ];                           


    protected $hidden = ['password', 'remember_token'];


    public function make_url()
    {
        $url = str_random(40);
        $this->referral_url->url = $url;
        if ($this->save()){
            return true;
        }
        else{
            return false;
        }
    }

    public function user_info()
    {
        return $this->hasOne('App\UserInfo');
    }

    public function sec_questions()
    {
        return $this->hasOne('App\SecurityQuestions');
    }

    public function referral_url()
    {
        return $this->hasOne('App\ReferralUrl');
    }
}

update I modified the function in the model to look like this now.

public function make_url()
{
    $url = str_random(40);
    $referral_url = $this->referral_url;
    $referral_url = new ReferralUrl();
    $referral_url->user_id = $this->id;
    $referral_url->url = $url;
    if ($referral_url->save()){
        return true;
    }
    else{
        return false;
    }
}

When I call

$user->make_url() 

I'm able to create it and it shows up in my db, but I also get the error-

Trying to get property of non-object
samlev

Normally the creating method should be called within boot():

public static function boot() {
    parent::boot();

    static::creating(function ($model) {
        $model->foo = 'bar';
    });
}

This would then be called automatically before the model is saved for the first time.

The problem that I see with your code is that you're attempting to modify a relation which doesn't exist yet.

So to explain, the hasOne method will attempt to join the current model to the remote model (in your case a ReferralUrl model) in SQL, but it can't do that before you save your model because your model doesn't exist in the database.

With your second attempt, the ReferralUrl object is the one that is changing, so that is the one that you need to save:

public function make_url() {
    $url = str_random(40);
    $referral_url = $this->referral_url
    $referral_url->url = $url;
    if ($referral_url->save()){
        return true;
    } else {
        return false;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I call a model in Laravel 5?

From Dev

How do I create a model of a function?

From Dev

How to call a model function inside the controller in Laravel 5

From Dev

Laravel 5 call a model function in a blade view

From Dev

How can I mock the create method on a model in laravel 5?

From Dev

How can I mock the create method on a model in laravel 5?

From Dev

Laravel: How do I create a custom pivot model?

From Dev

How do I call a batch of Laravel5 commands?

From Dev

How do I call the right controller function in Laravel?

From Dev

How do I call the right controller function in Laravel?

From Dev

Laravel 5: Model object could call Builder method. How does Laravel do that?

From Dev

How to Call function on laravel pivot model

From Dev

How to call a function on model Collection laravel

From Dev

how do I create custom error page in laravel 5

From Dev

How to create a model for this relationship in Laravel 5?

From Dev

How do I call other event methods?

From Dev

How do I call other event methods?

From Dev

Binding event listeners function, do I need to call the function later?

From Dev

Laravel model inheritance - how to call a function from parent model?

From Java

How do I call the `function` function?

From Dev

How do I call a function inside a function

From Dev

How do I create a click event in Verold?

From Dev

how do i create Tstrings with onchange event?

From Dev

Call a method in model in Laravel 5

From Dev

How can I create function for database activities and call them in controller in Laravel 5.1

From Dev

How do I pass an event as a function parameter?

From Dev

How do I create a VS C# project for a lamda function to handle a Amazon IoT developer button event?

From Dev

How to call Model via artisan command in Laravel 5?

From Dev

How do I create a function with a function in Python?

Related Related

  1. 1

    How do I call a model in Laravel 5?

  2. 2

    How do I create a model of a function?

  3. 3

    How to call a model function inside the controller in Laravel 5

  4. 4

    Laravel 5 call a model function in a blade view

  5. 5

    How can I mock the create method on a model in laravel 5?

  6. 6

    How can I mock the create method on a model in laravel 5?

  7. 7

    Laravel: How do I create a custom pivot model?

  8. 8

    How do I call a batch of Laravel5 commands?

  9. 9

    How do I call the right controller function in Laravel?

  10. 10

    How do I call the right controller function in Laravel?

  11. 11

    Laravel 5: Model object could call Builder method. How does Laravel do that?

  12. 12

    How to Call function on laravel pivot model

  13. 13

    How to call a function on model Collection laravel

  14. 14

    how do I create custom error page in laravel 5

  15. 15

    How to create a model for this relationship in Laravel 5?

  16. 16

    How do I call other event methods?

  17. 17

    How do I call other event methods?

  18. 18

    Binding event listeners function, do I need to call the function later?

  19. 19

    Laravel model inheritance - how to call a function from parent model?

  20. 20

    How do I call the `function` function?

  21. 21

    How do I call a function inside a function

  22. 22

    How do I create a click event in Verold?

  23. 23

    how do i create Tstrings with onchange event?

  24. 24

    Call a method in model in Laravel 5

  25. 25

    How can I create function for database activities and call them in controller in Laravel 5.1

  26. 26

    How do I pass an event as a function parameter?

  27. 27

    How do I create a VS C# project for a lamda function to handle a Amazon IoT developer button event?

  28. 28

    How to call Model via artisan command in Laravel 5?

  29. 29

    How do I create a function with a function in Python?

HotTag

Archive