简体   繁体   中英

I want to get a value from 2 tables and save it to a new one in Laravel 5

I have a laravel blade that when the interested button is clicked, it will store information in the querys table. 幼虫叶片 as you can notice in the url, 1 is the 'id' of the job that is displayed in the blade that is fetched from the jobs table.

I have a new table called 'querys' which has columns: id,jobid,name,age,sex,degree,contactmail, and bio. In which, the 'id' is Auto Incremented, 'jobid' should be the same as the 'id' from the jobs table, and the name, age,sex, degree, contactmail, and bio should be from the user_profiles table.

this is my jobcontroller@interested

public function interested(request $request)
{
    $job = job::find($id);
    $userinfo = userprofile::find($id);
    $query = new query;
    $query->jobid = $job->id;
    $query->name = $userinfo->name;
    $query->age = $userinfo->age;
    $query->sex = $userinfo->sex;
    $query->degree = $userinfo->degree;
    $query->contactmail = $userinfo->contactmail;
    $query->bio = $userinfo->bio;
    $query->save();

    //return
}

my web.php

Route::post('/submitresume', 'jobcontroller@interested');

this is the user_profiles table 在此处输入图片说明

when I clicked Interested, I get the error Undefined variable: id in jobcontroller.php (line 101)

I know I am doing something wrong but I don't know what since I am new to this.

this is the laravel blade in which the Interested Button is located

<form action= "/submitresume" method="post">
{{csrf_field()}}
<div class="col-md-8 col-md-offset-2">
  <h2 class="display-2">{{$item->jobposition}}</h2>
  <p class="lead">{{$item->schoolname}}</p>
  <p class="text-info"> {{$item->jobdesc}} </p>
  <p class="text-info"> Salary: {{$item->prevemp}} </p>
  <p class="text-info"> Contact Information: {{$item->contact}} </p>

  <p class="lead">
    <button type="submit" class="btn btn-primary">Interested</button>
  </p>

</div>
</form> 

hello getting that error because the $id variable not present in your function interested(), laravel uses oops concept you have use below mention code you will not get any error.

public function interested(request $request)
{
    $user = Auth::user();
    $id=$user->id;
    $job = job::find($id);
    $userinfo = userprofile::find($id);
    $query = new query;
    $query->jobid = $job->id;
    $query->name = $user->name();
    $query->age = $userinfo->age;
    $query->sex = $userinfo->sex;
    $query->degree = $userinfo->degree;
    $query->contactmail = $userinfo->contactmail;
    $query->bio = $userinfo->bio;
    $query->save();
    //return
}

Use route method to send id like this.

<form action= "{{ route('submitresume',$id) }}" method="post">

Your route:

Route::post('/submitresume/{$id}', 'jobcontroller@interested')->name('submitresume');

Your controller:

public function interested(Request $request,$id)
{
    $job = job::find($id);
    $userinfo = userprofile::find($id);
    $query = new query;
    $query->jobid = $job->id;
    $query->name = $userinfo->name;
    $query->age = $userinfo->age;
    $query->sex = $userinfo->sex;
    $query->degree = $userinfo->degree;
    $query->contactmail = $userinfo->contactmail;
    $query->bio = $userinfo->bio;
    $query->save();

    //return
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM