Trigger controller function when link is pressed laravel - php

I am trying to trigger a controller function when a link is pressed.
So.., this is the HTML
<a class="hollow button primary" href="{{ route('hire.worker', ['id' => $id]) }}"><i class="fa fa-user-plus"></i> Hire as Worker</a>
This is the route:
Route::post('/profile/{id}', [
'as' => 'hire.worker',
'uses' => 'ProfileController#hire'
]);
And this is the controller:
public function hire($id)
{
$worker = New Worker;
$worker->workerID = $id;
$worker->save();
}
When I press the button, the page only refreshes, nothing is stored in the database, I also have few other routes so that may be the issue.
// Profile
Route::get('/profile/{id}', [
'as' => 'display.profile',
'uses' => 'ProfileController#index'
]);
Route::post('/profile/{id}', [
'as' => 'display.profile',
'uses' => 'ProfileController#store'
]);
Route::post('/profile/{id}', [
'as' => 'hire.worker',
'uses' => 'ProfileController#hire'
]);

Links use GET as the request method so Route::post is not correct.
Create a new route like the one below and at the end of the controller method redirect to whatever page you originally wanted to go.
Route::get('/profile/link/{id}', [ 'as' => 'hire.worker', 'uses' => 'ProfileController#hire' ]);

Related

Showing data in Laravel Chatter Package is showing Undefined Variable Error

I am working on adding a comment like stackoverflow using chatter discussion package by devdojo so here is the problem I am writing code to show comments but an undefined variable error is coming up.
Error Page ScreenShot
public function show(Chatterreply $chatterreply ,$id)
{
$chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
return view('chatter::discussion', compact('chatterreplies'));
echo "<pre>"; print_r('$chatterreplies'); die;
}
In Web.php route is
/*
* Post routes.
*/
Route::group([
'as' => 'posts.',
'prefix' => $route('post', 'posts'),
], function () use ($middleware, $authMiddleware) {
// All posts view.
Route::get('/', [
'as' => 'index',
'uses' => 'ChatterPostController#index',
'middleware' => $middleware('post.index'),
]);
// Create post view.
Route::get('create', [
'as' => 'create',
'uses' => 'ChatterPostController#create',
'middleware' => $authMiddleware('post.create'),
]);
// Store post action.
Route::post('/', [
'as' => 'store',
'uses' => 'ChatterPostController#store',
'middleware' => $authMiddleware('post.store'),
]);
//Adding Comments
Route::post('/reply/{id}', [
'as' => 'store',
'uses' => 'ChatterreplyController#store',
'middleware' => $authMiddleware('post.reply.store'),
]);
//showing Comment
Route::get('/reply/{id}', [
'as' => 'show',
'uses' => 'ChatterreplyController#show',
'middleware' => $middleware('post.show'),
]);
First, I would suggest putting your debugging statements (...print_r...) before the return statement in your controller action, this way :
public function show(Chatterreply $chatterreply ,$id)
{
$chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
echo "<pre>"; print_r('$chatterreplies'); die();
// or use the laravel helper
dd($chatterreplies)
return view('chatter::discussion', compact('chatterreplies'));
}
You should see the content of the $chatterreplies variable.
If this is ok, check your controller name in web.php because it seems that it should be ChatterReplyController#show instead of ChatterreplyController#show (is the R letter in ChatterreplyController#show capital or not ? ) if you're following the camelCase convention as in ChatterPostController#store for instance.

Laravel doesn't return edit view

im using laravel 5.5 and i created a view for edit categories inside views/back/categories/edit
here my edit function in CategoriesController
public function edit($id)
{
$category = Categories::getall();
$categories = Categories::find($id);
return view('back.categories.edit', ['categories' => $categories, 'category' => $category]);
}
and here is my route
Route::group(['middleware'=>'admin'],function(){
Route::get('/dashboard','BackendController#index')->name('backend');
Route::group(['prefix' => 'categories'], function () {
Route::any('/show/{id}', ['as' => 'backend.categories.show', 'uses' => 'backend\CategoriesController#show']);
Route::get('/index', ['as' => 'back.categories.index', 'uses' => 'backend\CategoriesController#index']);
Route::any('/store', ['as' => 'back.categories.store', 'uses' => 'backend\CategoriesController#store']);
Route::any('/create', ['as' => 'back.categories.create', 'uses' => 'backend\CategoriesController#create']);
Route::any('/edit/{id}', ['as' => 'back.categories.edit', 'uses' => 'backend\CategoriesController#edit']);
Route::any('/update', ['as' => 'back.categories.update', 'uses' => 'backend\CategoriesController#update']);
Route::any('/destroy/{id}', ['as' => 'back.categories.destroy', 'uses' => 'backend\CategoriesController#destroy']);
});
});
my edit button
<a href="{{ url('back/categories/edit/'.$category->cat_id) }}" class="btn btn-success btn-sm">
<span class="fa fa-edit"></span> edit</a>
when i click on the edit button it return page not found with "Sorry, the page you are looking for could not be found." text and the URL :"back/categories/edit/4"
Since your route are named, you can use the route() helper to build working URL:
{{ route('back.categories.edit', ['id' => $category->cat_id]) }}
From your routes definition it looks like your url is /categories/edit/{id}
For links in templates I suggest to use the route helper function which takes the route name or the action method which takes controller#method
Also have a look at resource controllers
Route::resource('categories', 'CategoryController'); could replace your entire routes definition
https://laravel.com/docs/5.5/controllers#resource-controllers
You're missing the back portion of the prefix on your categories group.
Route::group(['prefix' => 'back/categories'], function () {
That would match your routes.

Extracting values from route in Laravel

I want to retrieve contact_id and hash values from my route. In my PictureController I have pictures function where I want to use them.
My route:
Route::get('/static/contacts/{contact_id}/picture/{hash}', [
'as' => 'platform.api.contacts.picture.hash',
'uses' => 'PictureController#pictures'
]);
I suppose this is not enough?
public function picture ($contactId, $hash)
Your Route contains little wrong.
Route::get('/static/contacts/{contact_id}/picture/{hash}', [ 'as' => 'platform.api.contacts.picture.hash', 'uses' => 'PictureController#pictures' ]);
Insted of use
Route::get('/static/contacts/{contact_id}/picture/{hash}', [ 'as' => 'platform.api.contacts.picture.hash', 'uses' => 'PictureController#picture' ]);
Your function name was wrong as you used in controller. Look at the function name in route pictures and controller you used picture.

Laravel Routes returning a 404 with route parameters

This is not the first time that I have used route parameters in laravel, however I cannot seem to get this to work.
Route:
Route::group(['prefix' => 'admin', 'before' => 'auth|beta|admin'], function()
{
Route::post('remove/{$id}', ['uses' => 'AdminController#postRemoveID', 'as' => 'admin.postremoveid']);
});
Controller:
public function postRemoveID($id)
{
$remove = ServiceProvider::where('id','=',$id)->first();
$remove->delete();
return Redirect::route('admin.manage'); //This just redirects to the page the user is currently on
}
Blade:
<a href="{{ route('admin.postremoveid', $id) }}">
<i class="fa fa-times"></i>
</a>
What would be causing my site to be redirecting to a 404?
Thanks for all your help!!
-Patrick
Use Route:get();
Route::get('remove/{id}', ['uses' => 'AdminController#getRemoveID', 'as' => 'admin.postremoveid']);
Controller:
public function getRemoveID($id)
{
$remove = ServiceProvider::where('id','=',$id)->first();
$remove->delete();
return Redirect::route('admin.manage'); //This just redirects to the page the user is currently on
}
You don't need that $ on the wild card
Route::group(['prefix' => 'admin', 'before' => 'auth|beta|admin'], function()
{
Route::post('remove/{id}', ['uses' => 'AdminController#postRemoveID', 'as' => 'admin.postremoveid']);
});

Laravel 'before' route not working

I have these three Routes
Route::get('login', [
'uses' => 'loginController#showLogin',
'as' => 'login.show',
'before' => 'guest'
]);
Route::get('dashboard', [
'uses' => 'DashboardController#index',
'as' => 'dashboard.show'
]);
Route::filter('guest', function()
{
if (Auth::check())
return Redirect::route('dashboard.show');
});
When I log-in the Auth:check() recognize it, but instead of redirecting me to localhost:8000/dashboard it redirects me only to localhost:8000
Am'I doing something wrong?
Thank you very much for any suggestions.
Redirect::route() will only redirect to saved routes. You have named your first route 'login.show'. Your route 'dashboard' is actually named 'dashboard.show'. You will need to do Redirect::route('dashboard.show')
If you are just wanting to redirect to a URL then do Redirect::to('dashboard')

Categories