0

I have some problem with laravel app. I'm trying to insert some info in database, and it works fine, untill I have addded a new row 'ip_address'. Ip address is defined as varchar(255) and everything is storing except address. What could be the problem? This is the code

public static function log(string $message, string $action, string $request)
    {
        return Log::create(['user_id' => Auth::id() , 'log_message' => $message, 'action' => $action, 'ip_address' => '212.91.176.38', 'log_type' => 'log', 'request' => $request]);
    }
1
  • 2
    did you add that field to the $fillable array on the model? since you are using mass assignment Commented Sep 15, 2020 at 11:24

2 Answers 2

2

To create an entry using laravel model, you have two choices:

Mass assignement using create(), in wich case you need to define wich fields are safe to mass assign.

public static function log(string $message, string $action, string $request)
{
    return Log::create(['user_id' => Auth::id() , 'log_message' => $message, 'action' => $action, 'ip_address' => '212.91.176.38', 'log_type' => 'log', 'request' => $request]);
}

// file Log.php
//...
Class Log extends Model
{
//...
    protected $fillable = ['user_id','log_message','action','ip_address','log_type', 'request'];
//...
}

OR

Create a new model instance

public static function log(string $message, string $action, string $request)
{
    $log = new Log();
    $log->user_id = Auth::id();
    $log->log_message = $message;
    $log->action = $action;
    $log->ip_address = '212.91.176.38';
    $log->log_type = 'log';
    $log->request = $request;
    $log->save();

    return $log;
}
Sign up to request clarification or add additional context in comments.

2 Comments

insert doesn't involve fillable since it is a query builder method
when called on a model instance it involves fillable because update is fill(...)->save , if called on a builder, it is a direct update though
1

you should define fillable property on your model with all your fields :

 protected $fillable = ['user_id','log_message','action','ip_address','log_type'];

1 Comment

I think you added an extra = :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.