我正在尝试从主题表中获取主题ID,以便将相关的关键字保存到添加了关键字的主题中。我通过给<select>
标记分配主题ID的值以不同的方式添加了它,但这不是一个好习惯,因为现在我无法将主题添加到相关ID。我需要获取主题的ID,以便可以将该主题添加到相关用户,并将关键字添加到相关主题。有帮助吗?
这是我的观点
@section('content1')
<p>Mentor Subject:
@foreach($subjects as $s)
<li> {{ $s['subject_name'] }} <a href="">Edit</a><a href="">Delete</a>
</li>
@endforeach
</p>
<p> Specified Keywords:
@foreach($subjectKeywords as $sk)
<li> {{ $sk ['keyword_title1'] }} </li>
<li> {{ $sk ['keyword_title2'] }} </li>
<li> {{ $sk ['keyword_title3'] }} </li>
<li> {{ $sk ['keyword_title4'] }} </li>
<li> {{ $sk ['keyword_title5x'] }} </li>
@endforeach
</p>
<form class="form-horizontal" method="POST" role="form" action="/add-new-subject" >
{{ csrf_field() }}
<div class="form-group" data-rule="required">
<label>Subject Titles</label> <br/>
<select id="ddselect" name='subject_name' class="signup" required >
<option value=""> Select Subject to Monitor </option>
@foreach($subjectDetails as $s)
<option id={{ $s['id'] }} name={{ $s['id'] }} value={{
$s['id'] }}>{{ $s['subject_name'] }}</option>
@endforeach
</select>
<div class="validation"></div>
</div>
<div class="form-group">
<input type="text" name="k1" class="signup-control form" id="k1" placeholder="Keyword 1"/>
<div class="validation"></div>
<input type="text" name="k2" class="signup-control form" id="k2" placeholder="Keyword 2"/>
<div class="validation"></div>
<input type="text" name="k3" class="signup-control form" id="k3" placeholder="Keyword 3"/>
<div class="validation"></div>
</div>
<div class="form-group">
<input type="text" name="k4" class="signup-control form" id="k4" placeholder="Keyword 4"/>
<div class="validation"></div>
<input type="text" name="k5" class="signup-control form" id="k5" placeholder="Keyword 5"/>
<div class="validation"></div>
</div>
</div>
<br/>
<div class="col-xs-12">
<!-- Button -->
<button type="submit" id="submit" name="submit" class="form contact-form-button light-form-button oswald light">Select Subject</button>
</div>
</form>
@endsection
@endsection
这是我的控制器
public function showSubject() {
if(Auth::user()->id) {
$subjects = Subject::where('user_id', Auth::user()->id)->get();
$subjectDetails = Subject::all();
$subjectKeywords = SubjectKeyword::all();
return view('mentor\showSubject', compact('subjects', 'subjectDetails', 'subjectKeywords'));
}else {
Session::flash("message", "Please sign in to access this page");
return redirect('/signup');
}
// echo Auth::user()->id;
// die();
return view('mentor\showSubject');
}
public function addNewSubject() {
if(Auth::user()->id) {
$s1 = Input::get('subject_name');
$subjects = new Subject();
$subjects->user_id = Auth::user()->id;
$subjects->subject_name = $s1;
// echo $s1;
$k1 = Input::get('k1');
$k2 = Input::get('k2');
$k3 = Input::get('k3');
$k4 = Input::get('k4');
$k5 = Input::get('k5');
$sk = new SubjectKeyword();
$sk->subject_id = $s1;
$sk->user_id = Auth::user()->id;
$sk->keyword_title1 = $k1;
$sk->keyword_title2 = $k2;
$sk->keyword_title3 = $k3;
$sk->keyword_title4 = $k4;
$sk->keyword_title5 = $k5;
$sk->save();
Session::flash("message", "Your keywords are saved");
return redirect('/subjects');
}else {
Session::flash("message", "Please sign in to access this page");
return redirect('/signup');
}
}
这是我的路线
Route::get('/subjects', 'MentorController@showSubject');
Route::post('/add-new-subject', 'MentorController@addNewSubject');
我想获取主题的ID,以便可以向主题添加相关的关键字。例如如果用户将CS作为主题添加,那么我将获取其ID并将所有关键字保存到其中。
感谢前进
答案 0 :(得分:0)
<select id="ddselect" name='subject_name' class="signup" required >
<option value=""> Select Subject to Monitor </option>
@foreach($subjectDetails as $s)
<option value={{ $s['id'] }}>{{ $s['subject_name'] }}</option>
@endforeach
</select>
///在您的控制器中,您可以通过subject_name获取您的ID
$s1 = Input::get('subject_name');
//after that get subject name based on that id
$sample=DB::table('tablename')->where('id',$s1)->first();
//then you can store both id and name separately
$subjects = new Subject();
$subjects->user_id = Auth::user()->id;
$subjects->subject_name = $sample->subjectname;