so I would like to get each data from two different table and display in on my view according to the ID of each row on the view table. However, I am stuck as I was not able to display the data according to their ID and now it is showing all my rows from the other two tables as follow:
Here is my table section within the view:
<table class="table table-hover table-borderless">
<tr>
<th class="text-center" width="10px">No</th>
<th width="250px">Event Name & Venue</th>
<th width="120px">Event Start</th>
<th width="120px">Event End</th>
<th width="80px" class="text-center">Category</th>
<th width="80px">Participant</th>
</tr>
@foreach ($events as $event)
<tr>
<td class="text-center"></td>
<td> <br/>
Venue:
@foreach ($event_details as $event_detail)
@endforeach
</td>
<td> </br> </td>
<td> </br> </td>
<td class="text-center">
@switch($event->event_category)
@case($event->event_category==1)
HQ-Based
@break
@case($event->event_category==2)
Stakeholder
@break
@case($event->event_category==3)
Webinar
@break
@case($event->event_category==4)
Online Training
@break
@default
@endswitch
</td>
<td class="text-center">
<ol>
@foreach ($event_participants as $parti)
<li>
@foreach ($users->where('id', $parti->user_id) as $user)
@endforeach
</li>
@endforeach
</ol>
</td>
</tr>
@endforeach
</table>
Here is my controller:
public function filter(Request $request)
{
$events = Event::where([
['id', '!=', Null],
[function ($query) use ($request) {
// if (($search = $request->search)){
// $query->orWhere('event_parts' , 'LIKE', '%' . $search . '%') ->get();
// }
if (($search = $request->search AND $searchdate1 = $request->DateFilter1 AND $searchdate2 = $request->DateFilter2)) {
$query->orWhere('event_parts' , 'LIKE', '%' . $search . '%');
$query->whereBetween('event_start',[$searchdate1,$searchdate2]) ->get();
}
if (($searchdate1 = $request->DateFilter1 AND $searchdate2 = $request->DateFilter2)) {
$query->whereBetween('event_start',[$searchdate1,$searchdate2]) ->get();
}
if (($searchdate1 = $request->DateFilter1)) {
$query->whereBetween('event_start',[$searchdate1,now()]) ->get();
}
}]
])
->paginate(1000);
$events = Event::paginate(5);
$officers = User::all();
// return view('events.filter', compact('events' , 'officers', 'officers2'))
// ->with('i', (request()->input('page', 1) - 1) * 10);
return view('events.filter', compact('officers'));
}
public function print(Request $request)
{
$events = Event::where([
['id', '!=', Null],
[function ($query) use ($request) {
if (($search = $request->search AND $searchdate1 = $request->DateFilter1 AND $searchdate2 = $request->DateFilter2)) {
$query->orWhere('event_parts' , 'LIKE', '%' . $search . '%');
$query->whereBetween('event_start',[$searchdate1,$searchdate2]) ->get();
}
if (($searchdate1 = $request->DateFilter1 AND $searchdate2 = $request->DateFilter2)) {
$query->whereBetween('event_start',[$searchdate1,$searchdate2]) ->get();
}
if (($searchdate1 = $request->DateFilter1)) {
$query->whereBetween('event_start',[$searchdate1,now()]) ->get();
}
}]
])
->paginate(1000);
$events = Event::all();
$users = User::all();
$event_id = DB::table('events')->pluck('id');
$event_details = Event_Details::all();
// $event_details = Event::with('event_venue')->where('id', $event_id)->get();
$event_participants = DB::table('event_participants')
->join('events' ,'event_participants.user_id', '=','events.id')
->get();
return view('events.print', compact('events', 'users', 'event_participants', 'event_details'))
->with('i', (request()->input('page', 1) - 1) * 10);
}
}
Model Events:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
protected $fillable = [
'event_name',
'event_category',
'event_start_date',
'event_end_date',
];
public function users()
{
return $this->belongsTo('App\User');
}
public function participants()
{
return $this->hasMany('App\Event_Participants', 'event_id');
}
public function details()
{
return $this->hasOne('App\Event_Details', 'event_id');
}
}
Model Event_Participants:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Event_Participants extends Model
{
protected $table = 'event_participants';
protected $fillable =
[
'event_id',
'user_id',
];
public function events()
{
return $this->belongsTo('App\Event');
}
}
Model Event_Details:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Event_Details extends Model
{
protected $table = 'event_details';
protected $fillable = [
'event_id',
'event_venue',
'event_desc',
'event_rem',
'event_start_time',
'event_end_time',
'event_adder',
];
public function events()
{
return $this->belongsTo('App\Event' ,'event_id');
}
}
via
Chebli Mohamed