Laravel 5 - Nest Resource index method resource ID - php

I try to make a default controller base on the Laravel Restful Controller, and I'm block with the index method with Nested Resources.
I have a route Route::resource('photos.comments', 'DefaultController'); and I need to get the photo_id in my index method. But so fare, I only get {photos}.
public function index(Request $request)
{
// $request->route('photos) => {photos}
}
or
public function index(Request $request, $photosId)
{
// photosId => {photos}
}
What am I missing ?
Thanks

Apparently you're doing it right. What do you mean when you say you get {photos}? Is the photo_id in the URL? Like photos/1/comments?
Here's how I do it and it works:
route.php
Route::resource('users.stuff' ,'StuffController');
StuffController.php
public function index($uid, Request $request)
{
//$uid contains the user id that is in the URL
User::find($uid)->doSomeStuff();
dd($uid);

Related

Get request from controller in model - Laravel

In my API I used "with" method to get parent's model relation and everything works fine.
I want to add an attribute in my relation and return it in my API but I should use request in my model.
Something like this :
Book.php
protected $appends = ['userState'];
public function getUserStateAttribute () {
return User::find($request->id); //request not exists here currently
}
I have $request in my controller (api controller)
Controller.php
public function get(Request $request) {
Post::with('books')->all();
}
I believe using static content to append in array of model is so easy but how about using request's based content ?
I guess you can use request() helper :
public function getUserStateAttribute () {
return User::find(request()->get('id'));
}
Sure this is not really MVC pattern, but it can work
You want to take request as a parameter here:
public function getUserStateAttribute (Request $request) {
return User::find($request->id);
}
That way you can access it in the function. You will just need to always pass the Request object whenever you call that function.
e.g. $book->getUserStateAttribute($request);
Alternatively, you could just pass the ID, that way you need not always pass a request, like so:
public function getUserStateAttribute ($id) {
return User::find($id);
}
Which you would call like:
e.g. $book->getUserStateAttribute($request->id);

How create one route with multi method in there?

I would like create one route with index method and all method name if exist can be call. Is any way to do this ?
Route::get('/{params}/table', TableController#index)->name(table.index)
Now in controller i would like create index method and all request should go there. I want get params as method and try call if exist.
public function index(Request $request) {
dd($request);
}
How can i do this ?
UPDATE
I think i need smth like:
Route::get('/{params}/abc/{any?}, function(any) {
get params $any
call function from TableController#any
});
and if method exist in TableController
You can simple receive $params or $any variable in the controller
public function index($params, $any) {
dd($any);
}
and than check if needed method exists.

How call specific function dynamically that we get in url/path in laravel

My Code in route.php:-
Route::get('/register/{id}',array('uses'=>'UserRegistration#id));
I want to call function id (that can be any function of controller) in UserRegistration controller.
Url is like this:- http://localhost:8000/register/test,
http://localhost:8000/register/login
here test and login are function in controller.
{id} is the parameter you're passing to route. So, for your routes go with something like this:
Route::get('/register/id/{id}',array('uses'=>'UserRegistration#id));
//this route requires an id parameter
Route::get('/register/test',['uses'=>'UserRegistration#test]);
Doing this you can call your functions but it is not the recomended way of doing it. Having separete routes foreach function allows for a more in depth control.
public function id(Request $request)
{
return $this->{$request->id}($request);
}
public function test(Request $request)
{
return $request->all();
}

Laravel 5.4 Route with Request and Params

I found myself stuck or lost in docs!
I'm using Laravel 5.4 and I'm trying to create validation in a controller which requires a request object.
My problem is my route passes parameters, I can't find in the docs an example how you include the Request $request argument and the $id parameter in a Controller method.
Here is my example:
1: SomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
...
public function edit($id) {
$request = Request; // <-- Do I use that instead of passing an arg?
}
2: Routes with a Paramter in -- routes/web.php
Route::match(['GET', 'POST'], '/some/edit/{id}', 'SomeController#edit');
In their example # https://laravel.com/docs/5.4/requests#accessing-the-request they have Request $request this in their controller, but what happens to Route parameters?:
public function store(Request $request) {}
Question: Do I need to edit the route or is the first parameter ignored if its a request?
A: Or would I do this in **SomeController.php?**
public function edit(Request $request, $id)
{
// I would get the $request, but what happens to $id?
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
]);
}
B: Perhaps, something like this?:
public function edit($id)
{
// Or Request::getInstance()?
$this->validate(Request, [
'title' => 'required|unique:posts|max:255',
]);
}
Any advice would help, or even an example!
Doing
public function edit(Request $request, $id)
should match the route you already have. So it's ok to do that. Request $request here is not a required parameter that you pass. Laravel will take care of it.
It works this way because everytime you send a request, well Laravel 'sends' a request for you (GET, POST). So, it always exist. But you can use use it within your controller if you dont pass it to the controller function as a parameter.
However, you can access it through the global helper function request() if you dont want to pass it as a function parameter.
$request = request();
$name = $request->name; // or
$name = request('name');

Accessing Lumen/Laravel URL params

I guess I'm missing something really obvious here but how do you access URL params in Lumen? I have the following route:
$app->get('user/{id}', ['uses' => 'userController#testId']);
Then in my user controller I have:
public function testId(Request $request) {
return $request->input('id');
}
But the ID is always null what have I missed even in this very basic example?
Okay, doesn't matter, as I thought I'm the tool here.
For anyone else (and myself in the future when I land on this page after a Google with the exact same problem next year) the documentation states:
If your controller method is also expecting input from a route parameter, simply list your route arguments after your other dependencies. For example, if your route is defined like so:
$app->put('user/{id}', 'UserController#update');
public function update(Request $request, $id)
{
//
}
So you need to pass any URL params into the function, they don't appear to be accessible via Lumens $request object.
public function testId(Request $request, $id) {
return $id;
}
According to https://laravel.com/docs/master/requests you can do the following:
$app->get('user/{id}', ['uses' => 'userController#testId']);
public function testId(Request $request, $id) {
//$id is from the path
}
The reasoning is that id is part of the request path and not the request input
Try with this
public function testId(Request $request) {
return $request->id;
}

Categories