我有一个名为COIN的表,其中包含字段id和name,我还有一个名为EVENT的表,其中包含字段id,title,description。在我的项目中,一枚硬币可以有很多事件,一个事件可以有许多相关的硬币,所以我制作了一个名为coin_event的中间表。问题是,我如何制作一个搜索栏,当我从Select2中选择我想要的硬币时,我会返回与所选硬币相关的所有事件?
我只有PageController驱动程序,它有方法可以做我需要的东西,但到目前为止我都是尿布,我不知道从哪里开始。
我占据了laravel 5.6
活动模型
public function coins()
{
return $this->belongsToMany(Coin::class);
}
Coin_event迁移
public function up()
{
Schema::create('coin_event', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('coin_id');
$table->unsignedInteger('event_id');
$table->timestamps();
});
}
硬币模型
public function events()
{
return $this->belongsToMany(Event::class);
}
PageController
public function search(Request $request){
$coins = Coin::findMany($request->coinSearch)->load('events')
->orderBy('event_at', 'DESC')
->where('status', 'APROBADO')
->paginate(8);
$events = $coins->pluck('events')->collapse();
return view('welcome', compact('events', 'coins'));
}
欢迎观点
<form name="form" method="get" class="form"
role="form" action="{{ route('search') }}">
{{csrf_field()}}
<div class="form-group">
<select class="select2 select-coin col-12 form-control"
id="coinSearch"
name="coinSearch[]"
multiple="multiple"
style="width: 260px;"
required>
@foreach ($coins as $coin)
<option value="{{ $coin->id }}">{{ $coin->coin_name }}
</option>
@endforeach
</select>
<button class="btn btn-rounded btn-info">Buscar</button>
</div>
</form>
<div class="row">
@foreach ($events as $event )
<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12">
<div class="card text-center">
<div class="card-header">
<h5><strong>{{ \Carbon\Carbon::parse($event->event_at)->toFormattedDateString() }}</strong>
<a class="reminder" href="#alertReminder" data-toggle="modal" data-target="#alertReminder" data-idevent="13002" data-date="23 May 2018" data-coin="SunContract (SNC)" data-title="Strategic Partnership"><i class="far fa-clock"></i></a>
</h5>
@foreach ($event->coins as $coin)
<h5 style="display: inline;"><strong><span>{{$coin->coin_name}} ({{$coin->coin_symbol}})</span></strong></h5>
@endforeach
<h6>{{$event->title}}</h6>
</div>
<div class="card-body">
<h5 class="card-title"><small>(Publicado: {{ \Carbon\Carbon::parse($event->created_at)->toFormattedDateString() }})</small></h5>
<p class="card-text" >{{$event->description}}</p>
<a class="btn btn-xs btn-outline-primary waves-effect waves-light proof_modal"
href="{{asset($event->proof)}}">
<span class="btn-label"><i class="fa fa-file-photo-o"></i></span> Prueba
</a>
<a class="btn btn-xs btn-outline-primary waves-effect waves-light" target="_blank"
href="{{$event->source}}">
<span class="btn-label"><i class="fa fa-external-link-square"></i></span> Fuente
</a>
</div>
<div class="card-footer">
<h6 class="text-footer-card"><small>Publicado por: {{$event->twitter_account}}</small></h6>
@if(empty($event->wallet_address))
<h6 class="text-footer-card"><small>ETH: 0x6e4Cf4b17086cD4Aae40Dbe9cB998d8460F0FbC6</small></h6>
@else
<h6 class="text-footer-card"><small>{{$event->wallet_type}}: {{$event->wallet_address}}</small></h6>
@endif
</div>
</div>
<br>
</div>
@endforeach
</div>
<div class="text-center">
{{ $events->links() }}
</div>
欢迎观看
答案 0 :(得分:0)
试试这个:
public function search(Request $request){
$coins = Coin::whereKey($request->coinSearch)
->orderBy('event_at', 'DESC')
->where('status', 'APROBADO')
->paginate(8);
$events = $coins->load('events')->pluck('events')->collapse();
return view('welcome', compact('events', 'coins'));
}