dimanche 13 mars 2022

Laravel relationships and storing data

I am new to laravel i have to store cv_id and job_id and user_id in a table in the DB. So This is my Schema cv

 Schema::create('cv', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string ('FName') ;
            $table->date('PrefJobEnd');
            $table->integer('WorkExp');
            $table->string('fileCV');
            $table->timestamps();
        });
    }

User Schema

 Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->boolean('status')->default(1);
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();

Jobs Schema:

  Schema::create('jobs', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('logo');
            $table->integer('WorkExp');
            $table->string('Company');
            $table->string('salary');
            $table->timestamps();

I create a new table to collect the ids of each of them called Job application table:

 Schema::create('job_applications', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('user_id');        
        $table->unsignedBigInteger('cv_id');
        $table->unsignedBigInteger('job_id');
        $table->timestamps();
    });

Then I specified some relationships in models

User model:

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

public function JobApplication()
{
    return $this->hasMany('App\Models\JobApplication');
}

JobApplication Model;

     public function cv()
        {
             return $this->belongsTo(cv::class, 'user_id');
    
        }
        public function Job()
        {
             return $this->belongsTo(Job::class, 'Job_id');
    
        }

cv model:

  public function Job()
    {
         return $this->hasMany(Job::class, 'user_id');

    }


    public function User()
    {
         return $this->hasMany(USer::class, 'user_id');

    }

}

job model:

    protected $primarykey ='id';
    protected $table ='jobs';

     //user post piviot for savedJobs
     public function User()
     {
         return $this->hasMany('App\Models\User');
     }
     
     public function deadlineTimestamp()
     {
        //  return Carbon::parse($this->deadline)->timestamp;
     }
  
     public function cv()
    {
         return $this->belongsTo(cv::class, 'user_id');

    }

I am trying to store ids of user, job and cv with following code in the controller; The error

Creating default object from empty value

    public function applyJob(Request $request,$id)
    {
        $application= new JobApplication;
        $jobs = Job::find($request->job_id);
        $jobs->job_id = $id;

        $users = User::find($request->user);
        $users->user_id = $id;

        $cv=cv::all( );
        $cv =cv::where ($users.'user_id','=',$cv.'user_id');
        $cv->cv_id = $id;

 //    dd($user);
        $ratings->save();
    //     return redirect('/')->with('success','well vvvvdscs');

also i tried this one the error was "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'cv_id' cannot be null (SQL: insert into job_applications (user_id, job_id, cv_id, updated_at, created_at) values (4, 1, ?, 2022-03-13 10:46:44, 2022-03-13 10:46:44))"

//     // $jobs = Job::find($request->job_id);
//     // $cv = cv::find($request->cv_id);
//     // $user = User::find(auth()->user()->id);
//     // $application = new JobApplication;
//     // $application->user_id = auth()->user()->id;
//     // $application->job_id = $request->job_id;
//     // $application->cv_id = $request->cv_id;
//     // $application->save();
//     // dd($application);
}

So any one can tell me a way or what is wrong in my code?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/IjM9YB4
via IFTTT

Aucun commentaire:

Enregistrer un commentaire