Responsive Advertisement

laravel inserting into multiple tables

Controller

<?php
 
namespace App\Http\Controllers;
 
use App\User;
use App\DB;
use App\Father;
use App\Mother;



use Illuminate\Http\Request;
 
class PassportController extends Controller
{

public function register(Request $request)
    {
        $this->validate($request, [
            'username' => 'required|min:3',
            'firstname' => 'required|min:3',
            'lastname' => 'required|min:3',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6',
        ]);
 
        $user = User::create([
            'username' => $request->username,
            'firstname' => $request->firstname,
            'lastname' => $request->lastname,
            'email' => $request->email,
            'password' => bcrypt($request->password)
        ]);

      

        $data = Father::create([
            'user_id' => $user->id,
            'fathername' => $request->fathername,
        ]);

        $datad = Mother::create([
            'user_id' => $user->id,
            'mothername' => $request->mothername,
        ]);

        //dd($data);
 
        $token = $user->createToken('TutsForWeb')->accessToken;
 
        return response()->json(['token' => $token], 200);
    }
Model  user

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
  
    use HasApiTokensNotifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username','firstname','lastname''email''password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password''remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];



    public function insertdata(){
        

    }
}
Model Father
<?php

namespace App;
use app\User;

use Illuminate\Database\Eloquent\Model;

class Father extends Model
{
    protected $fillable = [
        'fathername','user_id',
    ];
}


Model Mother


<?php

namespace App;
use app\User;

use Illuminate\Database\Eloquent\Model;

class Mother extends Model
{
    protected $fillable = [
        'mothername','user_id',
    ];
}




Post a Comment

0 Comments