Laravel how pass and read param from one route to another - laravel

how pass and get params from one route to another
return redirect('/registration')->with('some_params', $input);
and here is registrationController index method:
public function index(){
$some_params = Request::get('some_params'); //no result
}

with method flashes data to the session, you have to retrieve the data using the Session::get method.
public function index(){
$some_params = Session::get('some_params');
}

Related

Laravel authorization policy not working on Show page

I have a laravel app using Policies to assign roles and permissions, i cant seem to access the show page and im not sure what im doing wrong?
If i set return true it still shows a 403 error as well, so im unsure where im going wrong here. The index page is accessable but the show page is not?
UserPolicy
public function viewAny(User $user)
{
if ($user->isSuperAdmin() || $user->hasPermissionTo(44, 'web')) {
return true;
}
return false;
}
public function view(User $user, User $model)
{
if ($user->isSuperAdmin() || $user->hasPermissionTo(44, 'web')) {
return true;
}
return false;
}
UserController
public function __construct()
{
$this->authorizeResource(User::class, 'user');
}
public function index()
{
$page_title = 'Users';
$page_description = 'User Profiles';
$users = User::all();
return view('pages.users.users.index', compact('page_title', 'page_description', 'users'));
}
public function create()
{
//
}
public function store(Request $request)
{
//
}
public function show($id)
{
$user = User::findOrFail($id);
$user_roles = $user->getRoleNames()->toArray();
return view('pages.users.users.show', compact('user', 'user_roles'));
}
Base on Authorize Resource and Resource Controller documentation.
You should run php artisan make:policy UserPolicy --model=User. This allows the policy to navigate within the model.
When you use the authorizeResource() function you should implement your condition in the middleware like:
// For Index
Route::get('/users', [UserController::class, 'index'])->middleware('can:viewAny,user');
// For View
Route::get('/users/{user}', [UserController::class, 'view'])->middleware('can:view,user');
or you can also use one policy for both view and index on your controller.
I had an issue with authorizeResource function.
I stuck on failed auth policy error:
This action is unauthorized.
The problem was that I named controller resource/request param with different name than its model class name.
F. ex. my model class name is Acknowledge , but I named param as timelineAcknowledge
Laravel writes in its documentation that
The authorizeResource method accepts the model's class name as its first argument, and the name of the route / request parameter that will contain the model's ID as its second argument
So the second argument had to be request parameter name.
// Here request param name is timelineAcknowledge
public function show(Acknowledge $timelineAcknowledge)
{
return $timelineAcknowledge->toArray();
}
// So I used this naming here also
public function __construct()
{
$this->authorizeResource(Acknowledge::class, 'timelineAcknowledge');
}
Solution was to name request param to the same name as its model class name.
Fixed code example
// I changed param name to the same as its model name
public function show(Acknowledge $acknowledge)
{
return $acknowledge->toArray();
}
// Changed here also
public function __construct()
{
$this->authorizeResource(Acknowledge::class, 'acknowledge');
}
I looked over Laravel policy auth code and I saw that the code actually expects the name to be as the model class name, but I couldn't find it anywhere mentioned in Laravel docs.
Of course in most of the cases request param name is the same as model class name, but I had a different case.
Hope it might help for someone.

Laravel using where clause on Views

I'm newbie on Laravel.
I can send data like that:
public function index()
{
$InboxNew = Models\Inbox::where('read', false)->get();
$InboxMarkedAsRead = Models\Inbox::where('read', true)->get();
return view('dashboard.inbox', compact('InboxNew', 'InboxMarkedAsRead'));
}
I want to get data in view like that but gives some errors:
public function index()
{
$Inbox = Models\Inbox::all();
return view('dashboard.inbox', compact('Inbox'));
}
In view:
#if($Inbox->where('read', false)->get())
...
#endif
Your controller :
public function index()
{
$Inbox = Models\Inbox::all();
return view('dashboard.inbox', compact('Inbox'));
}
In blade you can achieve your data like this way :
#foreach($inbox as $query)
#if($query->read == false)
//
#endif
#endforeach
Problem with your code is that you have already called all() method on the Models\Inbox. What all() do is to simply call the get() method on model without applying any conditions.
You either need to define only a model (via query() method:
// Controller
public function index()
{
$Inbox = Models\Inbox::query();
return view('dashboard.inbox', compact('Inbox'));
}
and fetch it later with where clauses In view:
#if($Inbox->where('read', false)->get())
...
#endif
OR
You can do it in a cleaner way, which is to fetch the data in controller and use the view only to show the data
You either need to define only a model (via query() method:
// Controller
public function index()
{
$Inbox = Models\Inbox::where('read', false)->get();
return view('dashboard.inbox', compact('Inbox'));
}
and fetch it later with where clauses In view:
#if($Inbox)
...
#endif
PS: To check if a record exists, use exists() method instead of get() e.g $Inbox->exists()

Pass variable from one method to another in same controller Laravel

I need to pass select option value from store method to show
i need to pass the $typeg value to show method
public function store(Request $request ) {
$typeg = $request->input('type');
}
public function show($id) {
dd($this->store($typeg));
}
i get
Undefined variable:
or
Too few arguments to function app\Http\Controllers\appController::show(), 1 passed and exactly 2 expected
Try this
on the first function you have some variable witch you want to pass it to another function\method
Than you need to use $this and the name of the other method you'd like to pass the var too something like this.
public function oneFunction(){
$variable = "this is pretty basic stuff in any language";
$this->anotherFunction($variable)
}
public function anotherFunction($variable){
dd($variable);
}
Store your data on session (or somewhere else like cookie, cache, database). So you can reach the data later.
class SomeController extends Controller {
public function store(Request $request ) {
session(["typeg"=>$request->input('type')])
}
public function show($id) {
dd(session("typeg"));
}

How to I specify this route in Laravel?

I want to have a URL like this:
/v1/vacations?country=US&year=2017&month=08
How do I set the route in Laravel 5.3 and where can I put the controller and logic to accept the query string?
your route should look like;
Route::get('v1/vacations', 'VacationsController#index');
then on VacationsController
public function index()
{
dd(request()->query());
$query=request()->query();
//search database using query
//return view with results
}
Query strings can not be defined in your route since the query string is not part of the URI.
To access the query string you should use the request object. $request->query() will return an array of all query parameters. You can also use it as such to return a single query param $request->query('key')
You just would check the Request object for the url parameters like so:
// The route declaration
Route::get('/v1/vacations', 'YourController#method');
// The controller
class YourController extends BaseController {
public function method(Illuminate\Http\Request $request)
{
$country = $request->country;
// do things with them...
}
}
Hope this helps you.

Passing page URL parameter to controller in Laravel 5.2

In my application I have a page it called index.blade, with route /index. In its URL, it has some get parameter like ?order and ?type.
I want to pass these $_get parameter to my route controller action, query from DB and pass its result data to the index page. What should I do?
If you want to access the data sent from get or post request use
public function store(Request $request)
{
$order = $request->input('order');
$type = $request->input('type');
return view('whatever')->with('order', $order)->with('type', $type);
}
you can also use wildcards.
Exemple link
website.dev/user/potato
Route
Route::put('user/{name}', 'UserController#show');
Controller
public function update($name)
{
User::where('name', $name)->first();
return view('test')->with('user', $user);
}
Check the Laravel Docs Requests.
For those who need to pass part of a url as a parameter (tested in laravel 6.x, maybe it works on laravel 5.x):
Route
Route::get('foo/{bar}', 'FooController#getFoo')->where('bar', '(.*)');
Controller:
class FooController extends Controller
{
public function getFoo($url){
return $url;
}
}
Test 1:
localhost/api/foo/path1/path2/file.gif will send to controller and return:
path1/path2/file.gif
Test 2:
localhost/api/foo/path1/path2/path3/file.doc will send to controller and return:
path1/path2/path3/file.doc
and so on...

Resources