Laravel route problem redirects 404 error - php

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>

Related

Route Model Binding Problem Does Not Seem To Be Working

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'
]);

Laravel : Passing value from view to controller

My View:
<table border="1" style="text-align: left;">
<td>
<b>Firstname</b>
</td>
<td>
<b>Actions</b>
</td>
#foreach($Newslist as $News)
<tr>
<td>
{{ $News->lastname }}
</td>
<td>
<button type="button" class="btn btn-primary">Edit</button>
</td>
</tr>
#endforeach
</table>
My routes/web.php:
Route::Post('NewsEdit/{{$News->id}}/EditForm','NewsControllere#EditForm');
My Controller.php:
<?php
namespace App\Http\Controllers;
use App\Newsmodel;
use Illuminate\Http\Request;
class NewsControllere extends Controller
{
public function EditForm($NewsId)
{ dd(request()->all());
echo "deepakkeynes";exit();
//$Newsmodel = Newsmodel::find($NewsId);
//return view('/News')->with('News',$Newsmodel);
}
}
While clicking the edit button in the view, the following result is obtained:
Result: 404 page
Expected Result: The Id of the edit button along with the echo value!
Can anyone help me out? I am a newbie in laravel..!
Have a look at this LaraCast tutorial on Route Model Binding. This explains the underlying documentation well.
Essentially:
routes/web.php:
Route::get('NewsEdit/{news}/EditForm','NewsControllere#EditForm');
newsController.php:
public function EditForm(Newsmodel $news)
{
return view('/News')->with('News',$news);
}

Add attributes to Laravel pagination links

Please look into my code,
this is my web.php
Route::get('test', function (Request $request) {
$data = MyTableResource::collection(MyTable::paginate(10));
$headers = ['col1', 'col2', 'col3'];
return view('test.index', compact('data', 'headers'));
});
Here is my blade file,
<table md-table>
<thead>
<tr>
#foreach($headers as $header)
<th><span>{{$header}}</span></th>
#endforeach
</tr>
</thead>
<tbody>
#foreach($data as $d)
<tr>
<td>{{$d->id}}</td>
<td>{{$d->primary_identifier}}</td>
<td>{{$d->order_date}}</td>
</tr>
#endforeach
</tbody>
</table>
<div class = "table-paginate">
{{ $orders->links() }}
</div>
My problem is that,
when I refresh my page, the URL is
http://localhost/test?page=1
And when I click on any link, the URL is just replacing with its number (http://localhost/test?page=2), not redirecting.
I inspect the pagination link, it seems like
<li class="page-item" aria-current="page">
<a class="page-link" href="http://amzmarketplace.localhost.tom/test?page=2">2</a>
</li>
It is working when I added and attribute to <a> tag, that target="_self".
But how can I add this attribute in Laravel pagination URL or is there any other way to solve this issue?
In Laravel 7.x you can customize URL in route file or controller like:
Route::get('test', function (Request $request) {
$data = MyTableResource::collection(MyTable::paginate(10));
$data->withPath('custom/url');
...
});
OR
Also append sort=votes to each pagination link, you should make the following call to appends in view, like:
{{ $data->appends(['sort' => 'votes'])->links() }}
Check this out the laravel.com/docs/7.x/pagination#displaying-pagination-results documentation link

Syntax Error access violation in query Laravel 5.1

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

LaraveL : How to get data from many to many relationship

I have 3 tables which are user->id, name,password, roles->id,role, role_user->role_id, user_id
Now if I want to show the associated data in view page means role related to user ..How i do it in view?
means I want to show if the user has role it will be checked otherwise it will be unchecked
Here is my controller :
$users = User::all();
$roles = Role::all();
return view("Permission::assign_role",compact('users','roles'));
View page
<table class="table">
<thead>
<tr>
<th>SL No</th>
<th>Name</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
#foreach($users->roles() as $row)
<tr>
<td>{{$i}}</td>
<td>{{$row->name}}</td>
<td><input type="checkbox" name="role_id" checked> $role->role</td>
</tr>
<?php $i++; ?>
#endforeach
</tbody>
</table>
Eager load the data:
$users = User::with('roles')->get();
return view("Permission::assign_role", compact('users'));
And in the view, you can do something like this if you want to check if user has specific role:
#if ($user->roles->contains($roleId))
If you want to just iterate over user's roles:
#foreach($users->roles as $role)
{{ $role->name }}
This how I am using for my applications :
User Model :
public function roles() {
return $this->belongsToMany(Role::class);
}
Role Model :
public function user() {
return $this->belongsToMany(User::class);
}
In My Controller :
$users = User::all();
In My View
#foreach($users as $user)
#foreach($user->roles() as $role)
{{$role->name}}
#endforeach
#endforeach

Categories