I want to
SELECT events.id = scan.event_id IF Scan.student_id MATCH user.student_id SHOW events.id and events.name
But my code dint work, it blank screen nothing happen to show
so blind :|
Controller
public function pdftranscript($id)
{
$users = DB::table('users')
->join('scan', 'users.student_id', '=', 'scan.student_id')
->join('events', 'scan.event_id', '=', 'events.id')
->select('users.*', 'events.m_event_name', 'scan.event_id')
->get();
This blade
#foreach($user as $user)
<tr>
<td>{{$user->student_id}}</td>
<td>{{$user->name}}</td>
#foreach($event as $event)
<td>{{$event->m_event_name}}</td>
#endforeach
<tr>
#endforeach
First you could dd($users) in your controller to check whether you have got a successful query. Then in blade, it should be:
#foreach($users as $user)
<tr>
<td>{{$user->student_id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->m_event_name}}</td>
<tr>
#endforeach
You already took the values in $users variable..No need for the second foreach loop.
#foreach($users as $user)
<tr>
<td>{{$user->student_id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->event_id}}</td>
<td>{{$user->m_event_name}}</td>
<tr>
#endforeach
Related
I am using an optional parameter and am encountering the following error:
Undefined variable $id
even if it is passed in the route correctly.
Why am I getting this error? Can you help me please?
Route
Route::get('/project/index/{id?}', [ProjectController::class, 'index'])->name('project.index');
ProjectController
public function index($id=null)
{
if($id){
$projects = Project::withCount(['tasks'=> function (Builder $query){
$query->where('project_id', $id);
}])->get();
} else {
$projects = Project::withCount('tasks')->get();
}
return view('project.index', compact('projects'));
}
View
<tr>
<th>ID</th>
<th>Name</th>
<th>N. Task</th>
<th>Azione</th>
</tr>
#foreach ($projects as $project)
<tr>
<td>{{ $project->id }}</td>
<td>{{ $project->name }}</td>
<td>{{ $project->tasks_count }}</td>
#endif
</tr>
#endforeach
You need to pass the $id to the withCount method with use()
$projects = Project::withCount(['tasks'=> function (Builder $query) use ($id){
$query->where('project_id', $id);
}])->get();
First learn about route: Routing
Remove ? from route
Route::get('/project/index/{id}', [ProjectController::class, 'index'])->name('project.index');
change ProjectController function parmeter from
public function index($id=null)
to
public function index($id)
Done!!
I've just followed this tutorial about implementing a data table with Livewire in Laravel.
So far so good. Now I want to order a column that comes from a one-to-one relationship, but I just can't get the sorting work well when selecting this phones relationship column.
In this case, phones is the relationship method and number is the column I display in the table, which I want to allow the sorting, as well.
How do I implement the sorting by column relationship?
Here is part of my code of the livewire blade (original repo of the tutorial):
<select wire:model="orderBy" class="...">
<option value="firstname">Name</option>
<option value="lastname">Lastname</option>
<option value="email">E-mail</option>
<option value="phones">Phone number</option>
</select>
...
<table class="table-auto w-full mb-6">
<thead>
<tr>
<th class="px-4 py-2">Name</th>
<th class="px-4 py-2">Lastname</th>
<th class="px-4 py-2">E-mail</th>
<th class="px-4 py-2">Phone number</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td class="border px-4 py-2">{{ $user->firstname }}</td>
<td class="border px-4 py-2">{{ $user->lastname }}</td>
<td class="border px-4 py-2">{{ $user->email }}</td>
<td class="border px-4 py-2">{{ $user->phones->number }}</td>
</tr>
#endforeach
</tbody>
</table>
{!! $users->links() !!}
And here is the Livewire controller (original repo of the tutorial):
class UsersTable extends Component
{
use WithPagination;
public $perPage=5;
public $search = '';//Search string
public $orderBy='firstname';
public $orderAsc=true;
public $relationship_columns=['phones'];
public function render()
{
if(!in_array($this->orderBy,$this->relationship_columns)){
$users = User::search($this->search)
->orderBy($this->orderBy,$this->orderAsc ? 'asc' : 'desc')
->simplePaginate($this->perPage);
}else if($this->orderBy=='phones'){
$order = $this->orderAsc ? 'asc': 'desc';
$users = User::search($this->search)
->with(['phones' => function ($q) use($order){
$q->orderBy('number',$order);
}])
->simplePaginate($this->perPage);
}
return view('livewire.users-table',compact('users'));
}
}
Reference.
The sorting of (order by) phones is not working well. It seems it sorts some parts well, but in general, sorting is flawed. It can be sorted neither ascendant nor descendant.
Looks like the sorting type (asc,desc) is not taking effect in
$q->orderBy('number',$order);
Moreover, if I use the whereHas() method:
$order = $this->orderAsc ? 'asc': 'desc';
$users = User::search($this->search)
->whereHas('phones',function ($q) use($order){
$q->orderBy('number',$order);
})
->simplePaginate($this->perPage);
I get the following error:
SQLSTATE[HY000]: General error: 20018 The ORDER BY clause is invalid
in views, inline functions, derived tables, subqueries, and common
table expressions, unless TOP, OFFSET or FOR XML is also specified.
[20018] (severity 15)
What am I missing? Any ideas on how to fix this?
How do I implement the order by functionality on the relationship column in Livewire?
Based on these posts Ordering database queries by relationship columns in Laravel and Laravel Eloquent whereColumn Query Example, I have solved the ordering by relationship this way:
I set any name to the relationship, in the parameter search function, let's say phone.number.
Then, with an if statement I detect if the ordering is that relationship:
if($this->field == 'phone.number'){
$users = $users->orderBy(\App\Models\Phone::select('number')->whereColumn('users.phone_id','phones.id'));
}else{
$users = $users->orderBy($this->field,$this->order);
}
Moreover, in this answer it is explained how to search inside a column relationship.
You can use
$user = User::search($this->search)
->whereHas('phones', function ($q){
$q->orderBy('number', $order);
})
->simplePaginate($this->perPage);
instead of
$users = User::search($this->search)
->with(['phones' => function ($q) use($order){
$q->orderBy('number',$order);
}])
->simplePaginate($this->perPage);
I want to display two variable data on same view in a foreach loop
public function getWalletList()
{
$data = Data::orderBy('id','desc')->get()->toArray();
$user_ids = array_column($data, 'id');
$user_data = User::whereIn('id',$user_ids)->get()->toArray();
$d= array('data'=>$data,'user'=>$user_data);
return view('wallet_list', compact('d'));
}
So how to diplay data on laravel blade template
#foreach($d as $value)
<tr>
<td>{{$value['wallet']['name']}}</td>
<td>{{$value->user_id}}</td>
<td>{{$value->price}}</td>
<td>{{$value->price}}</td>
<td>{{$value->updated_at}}</td>
</tr>
#endforeach
why dont you use two foreach ?
#foreach($d as $value)
<tr>
<td>{{$value['wallet']['name']}}</td>
<td>{{$value->user_id}}</td>
<td>{{$value->price}}</td>
<td>{{$value->price}}</td>
<td>{{$value->updated_at}}</td>
</tr>
#endforeach
#foreach($b as $value)
<tr>
<td>{{$value['wallet']['name']}}</td>
<td>{{$value->user_id}}</td>
<td>{{$value->price}}</td>
<td>{{$value->price}}</td>
<td>{{$value->updated_at}}</td>
<tr>
#endforeach
I have a for each loop to iterate over the collection of records. It has the user_id, but not the other user credentials. I created another collection of user. How do I include this in my iteration?
Controller
public function index(Request $request, $id)
{
$thoughts = Thought::where('id', $id)->orderBy('created_at', 'desc')->paginate(100)->get();
$user = User::where('id', $thoughts->user_id);
return view('backend.pages.thought_list', compact('thoughts, user'));
}
View
#foreach ($thoughts as $thought)
<tr>
<td class="col-md-1">{{ $user->username}} <span class="user_id_thought">ID - {{ $user->user_id}}</span></td>
<td class="col-md-1">{{ $thought->response }}</td>
<td>{{ $thought->thought_type }}</td>
<td>{{ $thought->id }}</td>
</tr>
#endforeach
How do I display my users variable in the loop. Or is there a better way of doing this.
Eager Loading is the perfect use case for your requirement in my opinion.
e.g.
$thoughts = Thought::with('user')->get();
foreach ($thoughts as $thought) {
echo $thought->user->name;
}
For more details see: https://laravel.com/docs/5.6/eloquent-relationships#eager-loading
I am trying to pass 2 different columns from my sql table to my view ,so that i can show them.
My controller is:
$interface = DB::table("users")->distinct()->get(['name']);
$interfac = DB::table("users")->distinct()->get(['email']);
view()->share('users',compact('interface','interfac'));
My view is :
#foreach ($users as $key => $value)
<tr>
<td>{{ $value->name }}</td>
<td>{{ $value->email }}</td>
</tr>
But this is not working while passing the two variables.If i pass one it is working but passing two is not working.thanks.
Controller
$users = DB::table('users')->get();
return view('users', compact('users'));