I am having trouble in finding and understanding my mistake. I have table payments and i want to show this data on my students page, (by getting student_id). It should get the data by student_id.
payments{id, student_id, course_id, transfer_amount, payment_type_id, is_paid, date}
But this mistake keeps showing. Where is my mistake, guys please help.Any help is appreciated.
Here is StudentsController.php
public function payments($id, Request $request) {
$student = Student::findOrFail($id);
$payment = Payment::where('student_id', $id)-> get();
$student->has('payments')->where('student_id', $request->id)->get();
return view('students.groups', compact('student', 'payment'));
}
Here is my model Student.php
public function payments()
{
return $this->belongsToMany('App\Payment','payments', 'student_id');
}
here is view students> payments.blade.php
#forelse ($student->payments as $key => $payment)
<tr class="center aligned">
<td>{{++$key}}</td>
<td>{{$payment->course_id}}</td>
<td>{{$payment->payment_type_id</td>
<td>{{$payment->transfer_amount}}</td>
<td>
#if($payment->is_paid)
<p style="color: green;">Paid</p>
#else <p style="color: red">Not Paid</p>
#endif
</td>
#empty
<tr class="center aligned">
<td colspan="12" class="ui red header">No data</td>
</tr>
#endforelse
error Not unique table/alias showing up.
Do you really need to have the variable $payment? Did you use this somewhere or anywhere in the view? If not, then this will probably the right eloquent syntax:
public function payments($id, Request $request) {
$student = Student::with('payments')->where('student_id', $id)->firstOrFail();
return view('students.groups', compact('student'));
}
In the above example, it is searching for a user with id of $id and include its payments details using relationships that's defined in the model.
If you need to validate the student MUST always has at least 1 payment, then you could add has('payments') to the syntax like so:
public function payments($id, Request $request) {
$student = Student::has('payments')->with('payments')->where('student_id', $id)->firstOrFail();
return view('students.groups', compact('student'));
}
This will return ONLY if the student has at least 1 payment stored in the database.
Hope it helps!
I think you can update your query like:
public function payments($id, Request $request) {
$student = Student::findOrFail($id);
$payment = Student::with('payments')->where('id', $id)->get();
return view('students.groups', compact('student', 'payment'));
}
Hope this helps you.
UPDATE 5/5/2017
I think you Should Update payments.blade.php
#forelse ($student->payments as $key => $payment)
<tr class="center aligned">
<td>{{++$key}}</td>
<td>{{$payment->course_id}}</td>
<td>{{$payment->payment_type_id}}</td>
<td>{{$payment->transfer_amount}}</td>
<td>
#if($payment->is_paid)
<p style="color: green;">Paid</p>
#else <p style="color: red">Not Paid</p>
#endif
</td>
#empty
<tr class="center aligned">
<td colspan="12" class="ui red header">No data</td>
</tr>
#endforelse
Related
I'm working with Laravel 8 and I have made a table like this at Blade:
<div class="card-body table-responsive p-0">
<table class="table table-hover">
<tr>
<th>Username</th>
<th>Email</th>
<th>Role</th>
<th>Actions</th>
</tr>
#foreach($roles as $role)
#if(count($role->users))
#foreach($role->users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $role->name }} | {{ $role->label }}</td>
<td>
<form action="{{ route('levels.destroy' ,$user->id) }}" method="post">
#method('DELETE')
#csrf
<div class="btn-group btn-group-xs">
Edit
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</form>
</td>
</tr>
#endforeach
#endif
#endforeach
</table>
</div>
And the result perfectly showing up:
But now I got problem with Edit & Delete buttons that I have specified $user->id as parameter for both of them.
And when I hover over the buttons I can see the user id properly defined:
But when it comes to edit method which is using Route Model Binding, it does not find the user:
public function edit(User $user)
{
dd($user->id); // return null
}
However if I do not use Route Model Binding and say this instead:
public function edit($id)
{
dd($id); // return 1
}
It properly shows the user id!
I don't know why the Route Model Binding not working here, so if you know what's going wrong or how to fix this issue, please let me know...
You are trying to access the User Model which in this case doesn't know what id is, so you should be passing the id of the user to the edit route using either Get by passing it to the url endpoint , so now you can get it like
public function edit($id)
{
dd($id); // return null
}
or by sending it as a POST form and get it like
public function edit(Request $request)
{
dd($request->id); // return null
}
I saw comments, your resource controller name is not matching with your variable name "$user".
You can look here on official laravel docs.
In your situation, this might help;
Route::resource('levels', LevelController::class)->parameters([
'levels' => 'user'
]);
I am new to laravel, I am creating candidates approval/rejected. Now I am working for the approval. I have "general.blade.php" where this is showing all the pending list. Then when the admin approve selected candidates, it will pass the data to the new blade I called "Approved.blade.php". However, in my case. when I click one candidate, It pass all the candidate list to the new blade, either pending or approve.
CandidateController.php
public function postApprove($id)
{
$candidates = GeneralForm::where('id', '=', e($id))->first();
if($candidates)
{
$candidates->status = 1;
$candidates->save();
$candidates=GeneralForm::where('status' , '1')->get();
$menu = Menu::get();
$current_menu = 2;
$candidates = GeneralForm::latest()->paginate(5);
return view('approve', compact('candidates', 'menu', 'current_menu'));}
general.blade.php
*This is my button where admin can approve candidate *
<a class="flex items-center " href="{{route('application',['id'=>$candidate->id])}}"> <i data-feather="user-check" class="w-4 h-4 mr-1"></i> Accept </a>
I am supposed to show only approve candidates here
approve.blade.php
<tbody>#foreach ($candidates as $candidate)
<tr class="intro-x">
<td class="w-40">
<div class="font-medium whitespace-no-wrap">{{ $candidate->fullname }}</div>
</td>
<td class="w-40">
<div class="font-medium whitespace-no-wrap">{{ $candidate->email }}</div>
</td> </tbody>#endforeach
route
Route::get('application/approve/{id}', 'CandidateController#postApprove')->name('application');
What I am getting is, whenever I click one candidate, it will redirect me into approval.blade.php. however it is showing me whole list including the pending. I want it to show only the approve candidate. My database data working fine, only my view is not. Can someone help me which part is wrong?
You're getting 5 candidates using $candidates = GeneralForm::latest()->paginate(5); and passing that data to your view. Since you only need one candidate, you should update your code as follows:
public function postApprove($id)
{
$candidate = GeneralForm::find($id);
$candidate->status = 1;
$candidate->save();
$menu = Menu::get();
$current_menu = 2;
return view('approve', compact('candidate', 'menu', 'current_menu'));
}
GeneralForm::where('id', '=', e($id))->first(); can be shortened to GeneralForm::find($id)
Removed the line $candidates=GeneralForm::where('status' , '1')->get(); since that would get you a list of candidates, and it would be overwritten by $candidates = GeneralForm::latest()->paginate(5); anyway.
Renamed $candidates to $candidate to reflect that there's only one item
Then in your view you can get rid of the #foreach... since we only have one item to display:
<tbody>
<tr class="intro-x">
<td class="w-40">
<div class="font-medium whitespace-no-wrap">{{ $candidate->fullname }}</div>
</td>
<td class="w-40">
<div class="font-medium whitespace-no-wrap">{{ $candidate->email }}</div>
</td>
</tr>
</tbody>
Removed the #foreach since we only have one item to display
I have a problem, I made a support system, and when I want to enter the page where I watch the ticket, I will get 404 not found. Basically the route is that of the id in the database.
Routes:
Route::get('/viewTickets', 'TicketController#view_MyTickets')->name('viewTickets');
Route::get('/viewTickets/{ticket}', 'TicketController#view_MyTicketUpdate')->name('updateTicket');
Route::post('/viewTickets/{ticket}', 'TicketController#update_MyTicket');
Controller:
public function view_MyTickets() {
$tickets = Ticket::latest()->get();
return view('viewTickets', compact('tickets'));
}
public function view_MyTicketUpdate() {
$tickets = Ticket::latest()->get();
return view('updateTicket', compact('tickets'));
}
View:
<tbody>
#foreach($tickets as $ticket)
<tr>
<td>{{$ticket->id}}</td>
<td>{{$ticket->user_id}}</td>
<td>{{$ticket->title}}</td>
<td>{{$ticket->category}}</td>
<td>{{$ticket->status}}</td>
<td>
<form>
<i class="material-icons"></i>
<i class="material-icons"></i>
</td>
</form>
</tr>
#endforeach
</tbody>
I really don't understand the problem, a way to solve and did not receive error 404? Btw, I read the other topics and I couldn't find a solution.
The link you are setting in the href attribute is only the ticket id, but it should be, for example, "/viewTickets/1"
Try to put this:
href="{{route('updateTicket', ['ticket' => $ticket->id])}}"
In your route :
Route::get('/viewTickets', 'TicketController#view_MyTickets')->name('viewTickets');
Route::get('/viewTickets/{ticket}', 'TicketController#view_MyTicketUpdate')->name('updateTicket');
Route::post('/viewTickets/{ticket}', 'TicketController#update_MyTicket');
In your Controller :
public function view_MyTickets() {
$tickets = Ticket::latest()->get();
return view('viewTickets', compact('tickets'));
}
public function view_MyTicketUpdate(Request $request, $ticket) {
$tickets = Ticket::find($ticket);
return view('updateTicket', compact('tickets'));
}
In your Blade :
<tbody>
#foreach($tickets as $ticket)
<tr>
<td>{{$ticket->id}}</td>
<td>{{$ticket->user_id}}</td>
<td>{{$ticket->title}}</td>
<td>{{$ticket->category}}</td>
<td>{{$ticket->status}}</td>
<td>
<i class="material-icons"></i>
<i class="material-icons"></i>
</td>
</tr>
#endforeach
</tbody>
Hi i want do somethink like that: Page where user can look popularity rented cars.
I almost do this but i get some problems and i dont know how figure it.
I made relationship many-to-many and its working great.
View:
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Name Car</th>
<th>Popularity</th>
</tr>
</thead>
<tbody>
<?php $i = 1;?>
#foreach($cars as $car)
#foreach($car->rentcar as $rentcar)
<tr>
<td>{{ $i++ }}</td>
<td>{{$rentcar->name}}</td>
#foreach($car_pops as $car_pop)
<td>
#if($car_pop->total == '1')
<span class="glyphicon glyphicon-star"></span>
#elseif($car_pop->total == '2')
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
#endif
</td>
#endforeach
</tr>
#endforeach
#endforeach
</tbody>
</table>
Controller:
public function popularityCar()
{
$cars = User::with('rentcar')->get();
$car_pops = DB::table('rent_car')
->select('car_id', DB::raw('count(*) as total'))
->groupBy('car_id')
->get();
return view('user.popularityCar', ['cars' => $cars, 'car_pops' => $car_pops]);
}
in pivot table user can rent more then one time car with thats same name and i want display only one name car i mind i got audi and user rent it and second user rent it too but on other date so i want display only one time car audi and display popularity using bootstrap icon. and its almost working but showing more cars with thats same name so i trying groupBy like with checking popularity car but i got problem and i dont know how use it.
Model User:
public function rentcar()
{
return $this->belongsToMany(Cars::class, 'rent_car', 'user_id', 'car_id')->withTimestamps()->withPivot('start', 'end');
}
Model Cars:
public function caruser()
{
return $this->belongsToMany(User::class, 'rent_car', 'car_id', 'user_id');
}
So maybe is easy way to make query mysql with i can get car name and go to pivot table to get total car_id.
I have a post table and a comment table and I want to show a list of my comments. But for showing the title of post for each comment I get thrown this error:
Trying to get property of non-object (View:\myBlog\app\views\comments\list.blade.php)
I am really confused about it.
This is my controller:
public function listComment()
{
$comments = Comment::orderBy('id', 'desc')->paginate(20);
$this->layout->title = 'Comment Listings';
$this->layout->main = View::make('admin.index')->nest('content', 'comments.list', compact('comments'));
}
This is my comments/list.blade.php
<tbody>
#foreach($comments as $comment)
<tr>
<td>{{{$comment->commenter}}}</td>
<td>{{{$comment->email}}}</td>
<!--<td>{{$comment->post->title}}</td>-->
<td>
{{Form::open(['route'=>['comment.update',$comment->id]])}}
{{Form::select('status',['yes'=>'Yes','no'=>'No'],$comment->approved,['onchange'=>'submit()'])}}
{{Form::close()}}
</td>
<td>{{HTML::linkRoute('comment.delete','Delete',$comment->id)}}</td>
<td>{{HTML::linkRoute('comment.show','Quick View',$comment->id,['data-reveal-id'=>'comment-show','data-reveal-ajax'=>'true'])}}</td>
</tr>
#endforeach
</tbody>
</table>
{{$comments->links()}}
What is my wrong with passing data to comments/list.blade.php?