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

Nababur's avatar
Level 43

How can i get company id and user_id from company table? to create job

Admin route will create this job using a single company id and user_id.

public function jobStore(JobPostRequest $request) {

    Job::create([
        'user_id'=> $user_id,
        'company_id'=> $company_id,
        'title' => request('title'),
      
    ]);


    return redirect()->back()->with('success', 'Job posted Successfully.');
}
0 likes
4 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To get the company id and user_id from the company table, you can use the Eloquent ORM provided by Laravel. Assuming you have a Company model and a User model, you can retrieve the required data using the following steps:

  1. Import the necessary models at the top of your file:
use App\Models\Company;
use App\Models\User;
  1. Inside the jobStore method, you can retrieve the company id and user_id using the find method on the respective models:
public function jobStore(JobPostRequest $request)
{
    $company = Company::find($company_id);
    $user = User::find($user_id);

    // Rest of your code...
}
  1. Once you have retrieved the company and user, you can access their respective ids using the id property:
$company_id = $company->id;
$user_id = $user->id;
  1. Now you can use these values to create the job:
Job::create([
    'user_id' => $user_id,
    'company_id' => $company_id,
    'title' => request('title'),
]);

return redirect()->back()->with('success', 'Job posted Successfully.');

Make sure to replace App\Models\Company and App\Models\User with the actual namespaces of your models. Also, ensure that the $company_id and $user_id variables are properly assigned before using them in the create method.

Remember to validate and sanitize the input data before using it in your code to prevent any security vulnerabilities.

Nababur's avatar
Level 43

@LaryAI How can I get two id values from this query?

                <select name="user_id" id="user_id" class="form-control">
                    @foreach (App\Models\Company::all() as $company)
                  
                    <option value="{{ $company->user_id }}">{{ $company->cname }}</option>
                    <option value="{{ $company->id}}">{{ $company->id}}</option>
                        
                    @endforeach





                </select>

I need company id in hidden value input type and user_id in select value.

        <div class="form-group row">
            <div class="col-md-2">Employer name</div>
            <div class="col-md-4">
                <select name="user_id" id="user_id" class="form-control">
                    @foreach (App\Models\Company::all() as $company)
                  
                    <option value="{{ $company->user_id }}">{{ $company->cname }}</option>
                 
                        
                    @endforeach

                    <input type="hidden" name="company_id" value="{{ $company->id }}">
                </select>
          


             </div>
        </div>
Snapey's avatar

how do company and user relate to each other?

1 like

Please or to participate in this conversation.