I am fairly new to laravel and I have implemented a system in my project so that user can send "dm's" to eachother. But now in the create view there are checkboxes where you can check the users you want to send a dm to. I get the users like this in my create function in my controller:
$users = User::where('id', '!=', Auth::id())->get();
My goal is that users can click on other users names in other views and select an option with "send dm" or something. But I have no Idea how I can pass that id to my create function in my controller. Should I make the checkboxes hidden and select the id that is passed? Or what should my gameplan be here?
This is my create function in my controller
public function create($userid)
{
$users = User::where('id', '=', $userid);
return view('messenger.create', compact('users'));
}
These are my routes:
Route::group(['prefix' => 'messages'], function () {
Route::get('/', ['as' => 'messages', 'uses' => 'MessagesController#index']);
Route::get('create/{userid}', ['as' => 'messages.create', 'uses' => 'MessagesController#create']);
Route::post('/', ['as' => 'messages.store', 'uses' => 'MessagesController#store']);
Route::get('{id}', ['as' => 'messages.show', 'uses' => 'MessagesController#show']);
Route::put('{id}', ['as' => 'messages.update', 'uses' => 'MessagesController#update']);
});
The problem is I don't know how to fill up that $userid in my create function.
But I am not sure this is the correct way to do it, I think I might be on a completely wrong path here
Could realy use all the help
Thanks in advance!
If you want to use links and send a single user a dm here is an approach. You need a view for the dm and view with the user.
Here is the user:
<a href"{{ route('createMessage', ['user_id' => $user->id]) }}">Send message to: {{ $user->name }}</a>
You send user_id as route parameter that holds the id of the user that you want to send a message to ($user->id).
Here is the route:
Route::get('/message/create/{user_id}', [
'uses' => 'UsersController#create',
'as' => 'createMessage'
]);
You get the route parameter user_id in your create methond in your controller like so:
public function create($user_id)
{
$user = User::find($user_id);
return view('messenger.create', ['user' -> $user]);
}
Try this :
Your routes :
Route::get('create/{userid}', ['as' => 'messages.create', 'uses' => 'MessagesController#createForm']); // to load the form
Route::post('create/{userid}', ['as' => 'messages.create', 'uses' => 'MessagesController#create']); // to create the DM
To load the form with the user information :
public function createForm($userid)
{
$user = User::find($userid);
return view('messenger.create', array("user" => $user));
}
You can passe the ID on the URL in the action attribute for example :
<form action="create/{{ $user->id }}" method="POST">
The method to create the DM:
public function create($userid)
{
$user = User::find($userid);
//create the DM
}
There is another solution, you can pass on with a hidden input
Related
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.
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.
In Laravel, I know I can get all routes using `Route::getRoutes() but I can't find if it is possible to get a list of all routes contained in a specified group.
For example, i have this route file:
Route::group(array('group_name' => 'pages'), function() {
Route::any('/authentication', array('as' => 'authentication', 'uses' => 'LogController#authForm' ));
Route::group(array('before' => 'auth_administration'), function() {
Route::any('/tags_category/index', array('as' => 'index-tags-categories', 'uses' => 'TagsCategoryController#index'));
Route::any('/tags_category/update', array('as' => 'update-tags-category', 'uses' => 'TagsCategoryController#update'));
});
});
Route::group(array('before' => 'auth_administration'), function() {
Route::any('/tags_category/store', array('as' => 'store-tags-category', 'uses' => 'TagsCategoryController#store'));
Route::any('/tags_category/update/{id}', array('as' => 'update-form-tags-category', 'uses' => 'TagsCategoryController#updateForm'));
Route::any('/tags_category/delete/{id}', array('as' => 'delete-tags-category', 'uses' => 'TagsCategoryController#delete'));
}); // operazioni protette
and in my controller i want obtain only routes contained in the first group (the one with the variable 'group_name').
Is it possible? If yes how I can do it? Thanks
The attributes passed to the group in the first parameter are stored on the route in the action array. This array can be accessed via the getAction() method on the route. So, once you get access to the route objects, you can filter based on this information.
$name = 'pages';
$routeCollection = Route::getRoutes(); // RouteCollection object
$routes = $routeCollection->getRoutes(); // array of route objects
$grouped_routes = array_filter($routes, function($route) use ($name) {
$action = $route->getAction();
if (isset($action['group_name'])) {
// for the first level groups, $action['group_name'] will be a string
// for nested groups, $action['group_name'] will be an array
if (is_array($action['group_name'])) {
return in_array($name, $action['group_name']);
} else {
return $action['group_name'] == $name;
}
}
return false;
});
// array containing the route objects in the 'pages' group
dd($grouped_routes);
User artisan to list all routes for the application
php artisan routes --name=admin
in Laravel 5
php artisan route:list --name=admin
Suppose I have multiple routes in my Laravel routes.php Like-
Route::post( 'createClan',array('uses'=>'MyClan#createClan'));
Route::post( 'joinClan',array('uses'=>'MyClan#joinClan'));
Route::get( 'myclan', array( 'as' => 'myclan' , 'uses' => 'MyClan#myclan' ));
Route::get( 'clanDetails', array( 'as' => 'clanDetails' , 'uses' => 'MyClan#clanDetails' ));
Route::get( 'myclanDefault', array('as' => 'myclanDefault' , 'uses' => 'MyClan#myclanDefault' ));
And I have Controller named 'Common' and I have a 'ValidateRoute' function inside that common like this-
public function ValidateRoute($UserId, $form_name){
switch ($form_name)
{
case 'createJoin'://Create/Join Clan Menu Option.
$createJoin=TRUE;
$clans= Userclanmapping::where('user_id','=',$UserId)->where('active','=',1)->get(array('clan_id'));
foreach($clans as $clan)
{
$createJoin=FALSE;
}
return $createJoin;
}
}
My problem is that I want to call that function before calling that route. And If the function returns true only then I can open that route but if function returns false I should be moved to another route or I should get a message like route not found.
In short, I am trying to apply a custom check on every route. A route is only accessible only if it fulfills that conditions.
I tried this code-
Route::get( 'myclanDefault', array( 'before'=>'common:ValidateMenuOptions(Session::get(\'userid\'),\'createJoin\'))','as' => 'myclanDefault' , 'uses' => 'MyClan#myclanDefault' ));
But this didn't work.
You can use route filters per route basis using something like this:
// In filters.php
Route::filter('myclanDefaultFilter', function($route, $request){
// ...
if(someCondition) {
return Redirect::route('routeName');
}
});
Then declare the route like this:
Route::get(
'myclanDefault',
array(
'before' => 'myclanDefaultFilter',
'as' => 'myclanDefault',
'uses' => 'MyClan#myclanDefault'
)
);
Now before myclanDefault is dispatched to the action the myclanDefaultFilter filter will run first and you may redirect from that filter depending a certain condition.
Also, you may declare a global before event for all the routes using something like this:
App::before(function($request){
// This event will fire for every route
// before they take any further action
});