如何添加到下拉选择选项的链接?

时间:2019-01-22 22:42:07

标签: laravel

This is an image of my drop down select showing islands e.g Maiana as parent and culprits as child e.g Batiboa Kumeto etc我只是想知道如何在select选项中添加链接并将id传递给另一种形式?

我试图创建一个如上图所示的下拉列表

Maiana     -Koriaue Miruango     -Batiboa Kumeto

阿拜昂     -Erenawa Aeete

但是我无法实现的是添加链接 到Koriaue Miruango,Maiana岛下的Batiboa Kumeto和其他地方。一旦点击了名称(例如Batiboa Kumeto),就会传递一个ID来加载另一张表格。

感谢您的帮助

我的岛屿模特

<?php

namespace App\Model\Enforcement;
use Illuminate\Database\Eloquent\Model;

class Island extends Model
{
    protected $table = 'islands';

    protected $fillable = ['island_name'];

    public function culprits(){
        return $this->hasMany(Culprit::class, 'island_id');
    }
 }

我的罪魁祸首模型

<?php

namespace App\Model\Enforcement;
use Illuminate\Database\Eloquent\Model;

class Culprit extends Model
{
    protected $table = 'culprits';
    protected $fillable =  ['island_id','first_name','last_name','age','home_island'];

   public function island(){
       return $this->belongsTo(Island::class, 'island_id');
   }

   public function crimes(){
       return $this->hasMany(Crime::class);
   }
}

我的控制器

<?php

namespace App\Http\Controllers\Frontend\Enforcement;
use App\Model\Enforcement\Culprit;
use App\Model\Enforcement\Island;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $islands = Island::with(['culprits'])->get();
       // $culprits = Culprit::get();

        // dd($islands, $culprits);


       return  view('backend.enforcement.admin.tests.show',compact('islands','culprits'));
}

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($id)
    {
        $culprit = Culprit::findOrFail($id);
        $offences = Offence::pluck('name','id')->all();
        $fishedareas = Fishedarea::pluck('fishedarea_name','id')->all();

     return view('backend.enforcement.admin.tests.create',compact('culprit','offences','fishedareas'));
}

我的路线

   Route::resource('/enforcements/tests', 'Frontend\Enforcement   \TestController');
  Route::get('/enforcements/tests/{id?}', 'Frontend\Enforcement\TestController@create')->name('tests.create');

我的索引视图

<select data-placeholder="Select island" style="width:350px;"   class="chosen-select" tabindex="5">
<option value=""></option>
@foreach ($islands as $island)
        <optgroup value="{{$island->id}}" label="    {{$island->island_name}}">
        @foreach($island->culprits as $culprit)
        <option value = "{{$culprit->id}}"> {{$culprit->first_name}}<a  href="{{route('tests.create', $culprit->id)}}"> {{$culprit->last_name}}    </a></option>
        </optgroup>
        @endforeach
@endforeach
</select>

我的创建表单

                       <div>

                        {!! Form::open(array('url' => route('tests.store'))) !!}
                        {!! Form::hidden('id', $culprit->id, old('id'), ['class' => 'form-control']) !!} 
                        {!! Form::label('offence_id','Offence Name')!!}
                        {!! Form::select('offence_id',['' => 'Choose Offence Type']+ $offences, null, ['class'=>'form-control'])!!}
                        {!! Form::label('fishedarea_id','Fished Area')!!}
                        {!! Form::select('fishedarea_id',['' => 'Choose Fished Areas']+ $fishedareas, null, ['class'=>'form-control'])!!}
                        {!! Form::label('crime_date','Date')!!}
                        {!! Form::date('crime_date',null,['class'=>'form-control'])!!}
                        {!! Form::label('remarks','Remarks')!!}
                        {!! Form::textarea('remarks',null,['class'=>'form-control'])!!}

                       </div>   


                       <div class="form-group">

                       <button class="btn btn-primary">Add New</button>

                       </div>

                        <div>

            {!! Form::close() !!}
                    </div>

1 个答案:

答案 0 :(得分:0)

据我所知,我做了一些反复试验,并提出了类似的解决方案来解决我的问题。确保从头到尾都有更好的方法

<select name="forma"  data-placeholder="Select island" style="width:350px;"   class="chosen-select" tabindex="5" onchange="location =    this.options[this.selectedIndex].value;">
<option value=""></option>
@foreach ($islands as $island)
        <optgroup value="{{$island->id}}" label="{{$island->island_name}}">
        @foreach($island->culprits as $culprit)
        {{--<option value="{{route('tests.create', $culprit->id)}}">    {{$culprit->first_name}}  {{$culprit->last_name}}</option>--}}
        <option value="{{route('tests.create', $culprit->id)}}">
        <a href="{{route('tests.create', $culprit->id)}}">{{$culprit->first_name}}      {{$culprit->last_name}}</a>
       </option>
       </optgroup>
        @endforeach
@endforeach
</select>
相关问题