jeudi 24 novembre 2016

How to add new row of data when id is auto increment

I'm using laravel 5 framework. I don't have enough knowledge. I have a User model and user table in migrations.

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

     protected $fillable = [
       'name',
       'cnic',
       'age',
       ];
   }


     //migration up function
     public function up()
     {
        Schema::create('users', function (Blueprint $table) {
        $table->increments('id');           
        $table->string('name');
        $table->string('cnic');
        $table->string('age')->nullable();
        $table->timestamps();
     });

I have written an action in controller for adding a new row in the table by accessing id, name and cnic which is passed through Request parameter. Here I haven't given age because I don't need it in every record and it is nullable. I have updated this record using id.

   public function update(Request $request)
    {
      $user = User::findorFail($request->id);
      $user->update(array('name' => $request->name));
      $user->update(array('cnic' => $request->cnic));
      $user->save();
    }

But I don't want to do this. I want to add a new row only with given name and cnic because id is auto increment. Thank You!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire