Laravel 5.8 Routed page not found 404 error - php

I am new to Laravel. I am learning Laravel from tutorial and I drive into one problem which I can't solve.
I think I have problem somewhere into Routing, but I can't find it
Funny thing is that if the href is {{route('tag.create'}}, then it goes to creating page, but when I need to use ID it's not working...
I had same functionality for posts and categories, but everything worked fine for those two. So I really need your help to see what I can't see. I have these files:
index.blade.php:
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-body">
<table class="table table-hover">
<thead>
<th>
Tag name
</th>
<th>
Delete
</th>
</thead>
<tbody>
#if($tags->count()>0)
#foreach($tags as $tag)
<tr>
<td>
{{$tag->tag}}
</td>
<td>
<i class="fa fa-trash" aria-hidden="true"></i>
</td>
</tr>
#endforeach
#else
<tr>
<th colspan="5" class="text-center">
No tags yet
</th>
</tr>
#endif
</tbody>
</table>
</div>
</div>
#stop
web.php - this is the place where I define routes for tags for TagsController.php:
//Tags
Route::get('/tags',[
'uses'=>'TagsController#index',
'as'=> 'tags'
]);
Route::post('/tag/update/{$id}',[
'uses'=>'TagsController#update',
'as'=> 'tag.update'
]);
Route::get('/tag/create',[
'uses'=>'TagsController#create',
'as'=> 'tag.create'
]);
Route::post('/tag/store',[
'uses'=>'TagsController#store',
'as'=> 'tag.store'
]);
Route::get('/tag/delete/{$id}',[
'uses'=>'TagsController#destroy',
'as'=> 'tag.delete'
]);
TagsController.php - at first I tried to destroy the element, then I tried to return create view(because when I go through /tag/create rout everything works), but neither worked here
public function destroy($id)
{
return view ('admin.tags.create');
/*
Tag::destroy($id);
Session::flash('success', 'Tag deleted succesfully');
return redirect()->back();*/
}

I believe that you should set the route to Route::get('/tag/delete/{id}',[ 'uses'=>'TagsController#destroy', 'as'=> 'tag.delete' ]); because in your case you are telling the route to expect a variable called $id

Please change the parameters in the route setup in web.php from $id to id. I should solve your issue.
Eg: Route::get('/tag/delete/{id}',[
'uses'=>'TagsController#destroy',
'as'=> 'tag.delete'
]);
Thanks !!.

Related

display the authenticated user in laravel

Hello i'm trying to display the authenticated user but i'm getting this error :
Undefined variable: users (View:
C:\wamp64\www\Blan\resources\views\users\index.blade.php)
this is my code :
usercontroller :
public function index()
{
return view('users.index')->with('user', Auth::user());
}
the view :
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-header">
Update Your Profile
</div>
<div class="card-body">
<table class="table table-hover">
<thead>
<th>Image</th>
<th>Name</th>
<th>Update</th>
</thead>
<tbody>
#if( $users -> count() > 0 )
#foreach ($users as $user)
<tr>
<td>
#if(isset($user->profile->avatar))
<img src="{{ asset($user->profile->avatar)}}" width="60px" height="60px" style="border-radius: 50%" alt="">
#else
No image
#endif
</td>
<td>
{{$user->name}}
</td>
<td>
Update
</td>
</tr>
#endforeach
#else
<tr>
<th colspan="5" class="text-center">No Users </th>
</tr>
#endif
</tbody>
</table>
</div>
</div>
#stop
But if i return all the users using this code in the controller :
return view('users.index')->with('users', User::all());
its work fine.
so how i can return only the current authenticated user ?
You don't need to send the auth user via your controller.Auth is the global facade in laravel.Also you don't need foreach because there is only 1 authenticated user. Just type Auth::user()->name in the blade
where you want to display the user
Yea and btw the error ur getting is because you are doing foreach for $users but sending $user to blade but im pretty sure it wont work even if you correct that typo
In your example
public function index()
{
return view('users.index')->with('user', Auth::user());
}
You have used "user"
But tried to access variable as "users"
Unless you just made a typo in your example?

Can't delete from SQLite table in Laravel

I'm Doing a "take home assignment" for job interview. While I have some experience in web development, its not my forte. I am trying to delete a row in a SQLite table using a HTML DELETE button. I am using Laravel-php framework.
I've tried different solutions on google and stack Overflow, but none seem to solve the problem. I modeled my approach after this Laracasts video
Link to my code
The blade seems to be passing the correct info ($id from $contact->id) & the controller seems to be receiving. But the given contact associated with the id isn't being deleted.
FROM BLADE:
<div class="col-md-6">
<table class="table table-striped table-hover">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
</tr>
#foreach($contacts as $contact)
<tr>
<td> {{$contact->f_name}} </td>
<td> {{$contact->l_name}} </td>
<td> {{$contact->address}} </td>
<td>
<form method="POST" action="/delete/{{ $contact->id }}">
#method('DELETE')
#csrf
<div class="field">
<div class="control">
<button type="submit" class="button">Delete Contact</button>
</div>
</div>
</form>
</td>
</tr>
#endforeach
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</div>
FROM CONTROLLER:
public function delete($id) {
Contact::find($id)->delete();
return view('index');
}
FROM ROUTE:
Route::delete('/delete', [
'uses'=>'ContactController#delete',
'as'=>'contacts.delete'
]);
you're not getting the id on your delete method, you need to include it on the url like
Route::delete('/delete/{id}', [
'uses'=>'ContactController#delete',
'as'=>'contacts.delete'
]);
in this case, you don't need to change the delete method.
you can also retrieve the id from the request object without changing the delete route, int this case you need to include the id as a hidden input on your view and your delete method will look like this
public function delete(Request $request) {
Contact::find($request->id)->delete();
return view('index');
}

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

"Type error: Too few arguments to function App\Http\Controllers\UserController::attendance(), 0 passed and exactly 1 expected"

I have two table in my database which are user and the attendance table. What I want to do now is I want to show the attendance data from database according to the user in their attendance view which is linked with their profile. This is my attendance function in the userController.
public function attendance($id)
{
$user = UserProfile::findOrFail($id);
$this->authorize('modifyUser', $user);
return view ('user.attendance', ['user'=>$user]);
}
This is my route to attendance view.
Route::get('/attendance/', ['as' => 'user.attendance', 'uses' => 'UserController#attendance']);
This is my attendance view.
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<h1><i class="fa fa-university"></i> Attendance</h1>
</div>
<div class="row">
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Date</th>
<th>Time</th>
<th>Present</th>
</tr>
<?php $no=1; ?>
<tr>
<td>{{ $no++ }}</td>
<td>{{$user->attendance->date}}</td>
<td>{{$user->attendance->time}}</td>
<td>{{$user->attendance->present}}</td>
</tr>
</table>
</div>
</div>
</div>
#stop
This is the error that i got.
Type error: Too few arguments to function App\Http\Controllers\UserController::attendance(), 0 passed and exactly 1 expected".
I am new to laravel.
You're getting this error because attendance() method expects an ID and you don't pass it. Change the route to:
Route::get('attendance/{id}', ['as' => 'user.attendance', 'uses' => 'UserController#attendance']);
And pass an ID when creating a link to the attendance() method:
{{ route('user.attendance', ['id' => 1]) }}
Or:
{{ url('attendance/1') }}
If you want to get ID of currently logged in user, do not pass ID. Use auth()-user() instead:
public function attendance()
{
$this->authorize('modifyUser', auth()->user());
return view ('user.attendance');
}

Server side pagination of laravel 5

I am having some trouble implementing server side pagination in Laravel 5.
Do you have any idea please tell
try with this one
//in controller
$users = \App\User::paginate(15)
return view('your desired view file name', compact('users'));
// in view
<div class="text-center text-muted" role="status" aria-live="polite">Showing {{$users->firstItem()}} to {{$users->lastItem()}} of {{$users->total()}} entries</div>
<div style="display:flex;justify-content:center;align-items:center;" >
<p></p>
{!! with(new Illuminate\Pagination\BootstrapThreePresenter($users))->render()!!}
</div>
In controller you do somrthing like following
$users = \App\User::paginate(15)
return view('template.file', compact('users'));
and in View file you put following
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
#foreach ($users as $user)
<tr>
<td> {{ $user->name }} </td>
</tr>
#endforeach
</tbody>
</table>
{!! $users->appends(\Input::except('page'))->render() !!}
Last line renders pagination links.

Categories