0

This is a continuation of my last question.

I like to create a relationship between a user (with an account type that’s equal to a “profile”) and my job posts. What I did was create a relationship like this in my models (not sure if correct tho)

User.php

public function jobposts()
{
    $this->hasMany(JobPost::class)->where('account_type', 'profile');
}

JobPost.php

public function userprofile()
{
    $this->belongsTo(User::class)->where('account_type', 'profile');
}

JobPostController.php

public function store(Request $request)
{
    $this->validate($request, [
        'job_name' => 'required|max:100',
        'describe_work' => 'required|max:800',
        'job_category' => 'required|not_in:0',
        'city' => 'required|not_in:0',
        'state' => 'required|not_in:0',
        'zip' => 'required|regex:/\b\d{5}\b/',
    ]);

    dd(auth()->user()->jobpost()->job_name);
}

2021_11_20_211922_create_job_posts_table.php

Schema::create('job_posts', function (Blueprint $table) {
        $table->id();
        $table->foreignId('user_id')->contrained()->onDelete('cascade');
        $table->string('job_name');
        $table->text('describe_work');
        $table->string('job_category');
        $table->timestamps();
    });

Got 2 questions about what I can do in the JobPostController.php.

  1. How do I dd() to test the output?

This seems wrong

dd(auth()->user()->jobpost()->job_name);
  1. How do I add it correctly into the DB like this?
public function store(Request $request)
{
    $request->user()
        ->jobpost()
        ->create([
            'job_name' => $request->job_name
        ]);
}
Ginz77
  • 63
  • 3
  • 10
  • `jobpost` and `jobposts` check again – DevinE Nov 21 '21 at 11:36
  • Regarding your dd question. Log::info() will help you on that (default logfile is storage/logs/laravel.log). Remember to print_r($your_var, true) for complex data structures. – jvndev Nov 22 '21 at 15:29

0 Answers0