Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

biniyam20's avatar

Create vs Make for models?

In php artisan tinker when i use the create vs make methods for a model, an attribute defauls to 0 for create but is created correctly when I use the make method. I took at the source code and create essentially calls make and then saves the data to the database.

Any guidance would be appreciated?

https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Eloquent/FactoryBuilder.php#L82

>>> factory('App\Renter')->make();
=> App\Renter {#3037
     student_id: 70606910,  <----------
     fname: "Wanda",
     lname: "Glover",
     email: "[email protected]",
   }
>>> factory('App\Renter')->create();
=> App\Renter {#3033
     student_id: 0, <----------
     fname: "Dorothea",
     lname: "Lueilwitz",
     email: "[email protected]",
   }
>>> 
0 likes
10 replies
realrandyallen's avatar

You answered it yourself :) The only difference is create persists to the database whereas make just returns a new instance of the model

3 likes
biniyam20's avatar

@realrandyallen Sorry I should have clarified my question. I was wondering why in tinker mode create does not create the entire model but create does? I was alluding to the student_id column that has a correct value under make() but defaults to 0 under create(). Does my question make sense?

This my factory for the Renter class for reference. It seems it does work when using the make method so the issue may not be in the factory. I think I may just be missing some knowledge about the tinker environment because the id does not get populated in the database either.

<?php

use Faker\Generator as Faker;

$factory->define(App\Renter::class, function (Faker $faker) {

  $class_year = $faker->numberBetween(18,22);

  $first_name = $faker->firstName;
  $last_name = $faker->lastName;
  return [
    'student_id' => $faker->randomNumber(8, true),
    'fname' => $first_name,
    'lname' => $last_name,
    'email' => substr($first_name,0,1) . $last_name . $class_year . '@cmc.edu'
  ];
});
realrandyallen's avatar
Level 44

@BINIYAM20 - Is student_id the primary key? I've seen this happen when you have a primary key that isn't auto incrementing. To fix it you just have to put this in your Renter model:

...

public $incrementing = false;

...
shez1983's avatar

the other problem could be the int type.. if you have small int and you try to assign a bigger number..

biniyam20's avatar

That was it @realrandyallen . I didn't realize I should be specifying if my tables do not increment. Thank you for the help!

I dug into the documentation and as you alluded to Laravel inserts records differently depending on the $incrementing variable, which makes sense. If the table is incrementing, then Laravel does more work behind the scenes to ensure the next entry's PK increments as well however it performs a simple insert query if the $incrementing is false because the developer provides all of the data. https://github.com/illuminate/database/blob/master/Eloquent/Model.php#L784

I was wondering considering I've used code like the following in my codebase to create a renter, without the $incrementing variable set to false, why does the student_id PK get fully set in my server environment as opposed to the tinker environment I posted above in which if I use the Renter::create() method then the student id is not set. You've already helped me lots, I was just curious if you knew the explanation for that behavior of the top of your head?

              Renter::create(request(['student_id', 'fname', 'lname', 'email']));

Thank you :)

1 like
realrandyallen's avatar

@BINIYAM20 - Glad it worked out! To be honest I'm really not sure, it's unexpected behavior to me because in your factory you're generating a student_id with faker so to me it should be present regardless of make or create in tinker.

To further the mystery I did this test locally and in tinker I got a student_id with both make and create! So the only question then is are we on the same version of Laravel? If you're on an older version maybe something got fixed.

I'm on 5.7.16

biniyam20's avatar

@realrandyallen Sorry for the late response. I am also on version 5.7.16. I think my question was misguiding because once I added $incrementing = false, I do get a student_id with both make and create. The important thing is that I get a valid student_id, which I do. Thanks for the help.

1 like

Please or to participate in this conversation.