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.
Related
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.
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' ]);
I want to create dynamic route name for my app. Here is my route file
Route::group(['prefix' => '{team}/dashboard', 'middleware' => 'isMember'], function() {
Route::get('/user', array('uses' => 'UserController#index', 'as' => 'user.index'));
Route::get('/user/edit/{id}', array('uses' => 'UserController#edit', 'as' => 'user.edit'));
Route::patch('/user/{id}', array('uses' => 'UserController#update', 'as' => 'user.update'));
Route::delete('/user/{id}', array('uses' => 'UserController#destroy', 'as' => 'user.delete'));
it's not simple if i have to define route like this
'route' => ['user.delete', $team, $user->id]
or
public function destroy($team,$id) {
// do something
return redirect()->route('user.index', $team);
}
I want to generate route name like "$myteam.user.delete" or something more simplier like when i define "user.delete" it includes my team name.
How i can do that? is it possible?
You could do that by setting as. Also using resource routes will be handy.
$routeName = 'team.';
Route::group(['as' => $routeName], function(){
Route::resource('user', 'UserController');
});
Now you can call like
route('team.user.index');
More on resource routes here https://laravel.com/docs/5.3/controllers#resource-controllers
try this:
Route::delete('/user/{team}/{id}', array('uses' => 'UserController#deleteTeamMember', 'as' => 'myteam.user.delete'));
Now call the route as:
route('myteam.user.delete', [$team, $id]);
Have a list of providers with a three filters (location, category, event type), i need to concatenate filters in routes for make the url more friendly, it would be like ej.: mysite.com/filter/location/cordoba/eventtype/weeding/category/travels
these filters are not required, so i can filter for only location, or location and category, event type and category, etc.. now my question is, ¿could i do that in laravel 5.2?
i before do this like this:
Route::get('/filtro/localidad/{localidad?}', [
'uses' => 'ProviderController#filterLocation',
'as' => 'site_filter_location_path'
]);
Route::get('/filtro/tipo_evento/{event_type?}', [
'uses' => 'ProviderController#filterEventType',
'as' => 'site_filter_event_type_path'
]);
Route::get('/filtro/rubro/{caption?}', [
'uses' => 'ProviderController#filterCaption',
'as' => 'site_filter_caption_path'
]);
i need something like this (it send me a error where i put only one or two parameters)
Route::get('/filtro/location/{localidad?}/event_type/{event_type?}/caption/{caption?}', [
'uses' => 'ProviderController#filter',
'as' => 'site_filter_path'
]);
Let's say that I search by location and category, then the url would be like this: mysite.com/location/cordoba/event_type//caption/travel This generates a bad url which gives me error, then how can I do this?
Laravel will not know how to find your route every time you have a missing parameter, so you cannot really rely on
Route::get('/filtro/location/{localidad?}/event_type/{event_type?}/caption/{caption?}', [
'uses' => 'ProviderController#filter',
'as' => 'site_filter_path'
]);
You should have something like
Route::get('/filtro/location/{localidad}/event_type/{event_type}/caption/{caption}', [
'uses' => 'ProviderController#filter',
'as' => 'site_filter_path'
]);
So in this case you can call
/filtro/location/Santito/event_type/Fiesta/caption/false
To be sure there would not have missing parameters.
What I prefer to do
Filtering is something hard to put in URLs. Every time you have a new filter, you'll get a new url and that's not good at all, because it gets messy really fast. What people do, usually is to have an endpoint only for the filter, and use url parameters to configure the filter. Laravel pagination is a kind of filter that does not uses route, but url parameter. So I would do
Route::get('/filtro', [
'uses' => 'ProviderController#filter',
'as' => 'site_filter'
]);
And a controller like this
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProviderController extends Controller
{
private function buildFilter($request, $query)
{
if ($localidad = $request->get('localidad')) {
$query->where('localidad', $localidad);
}
if ($tipoEvento = $request->get('tipo_evento')) {
$query->where('tipo_evento', $tipoEvento);
}
...
return $query;
}
public function filter(Request $request)
{
$query = Localidad::newQuery();
$query = $this->buildFilter($request, $query);
$query->where('filter something else');
return view('home', ['data' => $query->get()]);
}
}
Then you only have to make a form using method="GET" to filter
<form action="/filtro" method="GET">
...
</form>
And you app should receive requests like
/filtro?localidad=Santa Maria&tipo_evento=Fiesta
And if you need to hit your own routes, by doing internal filter, you can do things like:
Route::get('/filter', function () {
$items = [
'localidad' => 'Buenos Aires',
'tipo_evento' => 'Santa Maria',
'rubro' => 'Todos',
];
$items = collect($items)->map(function($item, $key) {
return "$key=$item";
});
$filter = implode('&', $items->toArray());
return redirect()->to('/filter?'$filter);
});
In the example $filter is localidad=Buenos Aires&tipo_evento=Santa Maria&rubro=Todos, so it would call
/filter?localidad=Buenos Aires&tipo_evento=Santa Maria&rubro=Todos
In my routes file, I have;
Route::get('/{token}/student', [
'uses' => 'SurveyController#resumeSurvey',
'as' => 'student',
]);
Route::get('/{token}/city', [
'uses' => 'SurveyController#resumeSurvey',
'as' => 'city',
]);
So the route is either "student" or "city". How do I determine which one in my controller method? Should I even be structuring my routes like this? Should I just point them to two different methods?
I can easily pass in {token} for example with just;
public function resumeSurvey($token)
{
Inside a controller you can get a current route name by getting a route object Illuminate\Routing\Route, at first place, then calling its method getName.
The next two ways are the same.
public function resumeSurvey($token)
{
$routeName = Route::getCurrentRoute()->getName();
$routeName = $this->getRouter()-> getCurrentRoute()->getName());
}
You should use different methods if you would like different things to do.
Example:
Route::get('/{token}/student', [
'uses' => 'SurveyController#resumeStudent',
'as' => 'student',
]);
Route::get('/{token}/city', [
'uses' => 'SurveyController#resumeCity',
'as' => 'city',
]);
And in your controller you should have two methods:
public function resumeStudent($token) {
}
public function resumeCity($token) {
}
Then your first route goes to resumeStudent and the ohter route to resumeCity.