猿问

如果未在视图中传递,如何找到 id?

我正在尝试从主题表中获取主题 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>


这是我的路线


        Route::get('/subjects', 'MentorController@showSubject');

        Route::post('/add-new-subject', 'MentorController@addNewSubject');

我想获得一个主题的 id,以便我可以向主题添加相关的关键字。例如,如果用户添加 CS 作为主题,那么我将获取其 ID 并将所有关键字保存到其中。


月关宝盒
浏览 119回答 1
1回答

慕慕森

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <select id="ddselect" name='subject_name' class="signup" required >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value=""> Select Subject to Monitor </option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@foreach($subjectDetails as $s)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value={{ $s['id'] }}>{{ $s['subject_name'] }}</option>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@endforeach&nbsp; &nbsp; &nbsp; &nbsp; </select>//在你的控制器中,你可以通过subject_name获取你的id&nbsp; &nbsp; &nbsp; &nbsp; $s1 = Input::get('subject_name');&nbsp; &nbsp; //after that get subject name based on that id&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $sample=DB::table('tablename')->where('id',$s1)->first();&nbsp; &nbsp;//then you can store both id and name separately&nbsp; &nbsp; &nbsp; &nbsp; $subjects = new Subject();&nbsp; &nbsp; &nbsp; &nbsp; $subjects->user_id = Auth::user()->id;&nbsp; &nbsp; &nbsp; &nbsp; $subjects->subject_name = $sample->subjectname;
随时随地看视频慕课网APP
我要回答