I have a route like this:
Route::get('page/{id},{time}', 'OpController#op');
Now my method is simply:
public function op(Request $request, $id, $time)
{
dump($request->all());
dump($id);
}
If I call that with /op/hello,123 I get this dump:
$request->all() -> []
$id -> "hello"
Is there any reasons that $request doesn't have the parameters?
$request->input('id') returns null
Because Request shouldn't have URL vars in it. Request used for getting data from forms etc. You should use $id and $time variables if you want to get data from URL in this case.
You can contents of Request object by using dd($request);
Got it:
$request->input('param');
Works when url query param, example: url?param=text and all the params of POST.
That doesn't work with route param 'myurl/{param}'
Just change your route with below code....
Route::get('page/{id}/{time}', 'OpController#op');
And write url like /page/hello/123
Related
I need a little help and I can’t find an answer. I would like to replicate a row from one data table to another. My code is:
public function getClone($id) {
$item = Post::find($id);
$clone = $item->replicate();
unset($clone['name'],$clone['price']);
$data = json_decode($clone, true);
Order::create($data);
$orders = Order::orderBy('price', 'asc')->paginate(5);
return redirect ('/orders')->with('success', 'Success');
}
and i got an error :
"Missing argument 1 for
App\Http\Controllers\OrdersController::getClone()"
.
I have two models: Post and Order. After trying to walk around and write something like this:
public function getClone(Post $id) {
...
}
I got another error
Method replicate does not exist.
Where‘s my mistake? What wrong have i done? Maybe i should use another function? Do i need any additional file or code snippet used for json_decode ?
First of all, make sure your controller gets the $id parameter - you can read more about how routing works in Laravel here: https://laravel.com/docs/5.4/routing
Route::get('getClone/{id}','YourController#getClone');
Then, call the URL that contains the ID, e.g.:
localhost:8000/getClone/5
If you want to create an Order object based on a Post object, the following code will do the trick:
public function getClone($id) {
// find post with given ID
$post = Post::findOrFail($id);
// get all Post attributes
$data = $post->attributesToArray();
// remove name and price attributes
$data = array_except($data, ['name', 'price']);
// create new Order based on Post's data
$order = Order::create($data);
return redirect ('/orders')->with('success', 'Success');
}
By writing
public function getClone(Post $id)
you are telling the script that this function needs a variable $id from class Post, so you can rewrite this code like this :
public function getClone(){
$id = new Post;
}
However, in your case this does not make any sence, because you need and integer, from which you can find the required model.
To make things correct, you should look at your routes, because the url that executes this function is not correct, for example, if you have defined a route like this :
Route::get('getClone/{id}','YourController#getClone');
then the Url you are looking for is something like this :
localhost:8000/getClone/5
So that "5" is the actual ID of the post, and if its correct, then Post::find($id) will return the post and you will be able to replicate it, if not, it will return null and you will not be able to do so.
$item = Post::find($id);
if(!$item){
abort(404)
}
Using this will make a 404 page not found error, meaning that the ID is incorrect.
I have the following route in my routes.web.php. Looks like this...
Route::get('/spielerAuswahl/{somevar}', 'SpielplanController#getHeimGast');
In the variable {somevar} I have for example Nr1=111&Nr2=222. The route works fine in a get to SpielplanController...
public function getHeimGast(){
$var = $somevar;
return view('test')->with('variableControllerSomevar', $var);
}
In this function I want to put the Nr1=111&Nr2=222 in the $var and make then an easy output of this variable in the view. How to get that?
Though the answer come late, but you can put Nr1=111&Nr2=222 as uri with proper encoding. For javascript, encodeURIComponent can be used to escape certain characters to form a valid URI:
encodeURIComponent('Nr1=111&Nr2=222');
// output: Nr1%3D111%26Nr2%3D222
To request server, using this url:
http://example.com/spielerAuswahl/Nr1%3D111%26Nr2%3D222
The controller function then receives correct form of variable as follows:
public function getHeimGast ($somevar) {
$somevar // = Nr1=111&Nr2=222
}
Only URI segments can be map as route variables in Laravel, no query string variables.
You can simply fetch those like these:
public function getHeimGast(){
$vars = request()->all();
return view('test', $vars);
}
In your test blade, you can use those query vars, eg( Nr1=111&Nr2=222 ) as:
$Nr1, $Nr2 ... and so on...
NOTE: given a query like this: /spielerAuswahl?Nr1=111&Nr2=222
public function login($companyId="",$invoiceId="")
{
}
$companyId I want to fetch from URL get its working fine for me but here I want to get $companyid as input::get
public function login()
{
$companyid = Input::get();
}
I am getting null becourse i cannot pass name in get() becouse i m fetching it from url so how can i achive it ........
value i have passed from route
First you need to look at your route definition. If your route is as
Route::get('/login/{companyId?}/{invoiceId?}', array('as'=> 'login', function($companyId="",$invoiceId="") {}));
then remove those parameters and change it to
Route::get('/login', array('as'=>'login',function(){....}));
After that you can change your route call to
route('login', array('companyId'=> 1231323, 'invoiceId'=> 123123));
this arguments you passed in array will be query string. It is important to remove those dynamic segments from your Route definition.
I am following tutorial http://www.tutorials.kode-blog.com/laravel-5-angularjs-tutorial and I have managed to write the similar method for my controller:
public function update(Request $request, $id) {
$employee = Employee::find($id);
$employee->name = $request->input('name');
//...
$employee->save();
return "Sucess updating user #" . $employee->id;
}
It is thought in tutorial that this code works but in reality var_dump($request->input) gives NULL. So - what $request variable should I use for getting the body of the request? I made var_dump($request) but the structure is unmanageably large. Actually I am suscpicous about this tutorial - do we really need to list all the fields in the standard update procedure?
You can access the input data with:
$input = $request->all();
https://laravel.com/docs/5.2/requests#retrieving-input
However, I've also had to get the input in this manner when using AngularJS $http module:
$input = file_get_contents('php://input');
for get all input
try it
$request = \Input::all();
If you want to fetch individual parameters from request object the you can do that with input Method of Request Class.
$request->input("parameter_name");
But if you want to fetch all request parameters then you can use all method which will return you an array of all the request key-value pairs
$request->all()
The thing you are missed is, you are calling $request->input which is null because input is method of Request class and not a property
I'm trying to call a function inside one of my controller from the action() helper function. I need to pass a paramter to my function.
Here is the funciton I'm trying to call :
public function my_function($clef = 'title')
{
$songs = Song::orderBy($clef)->get();
return View::make('my_view', compact('songs'));
}
Here is the way I call it :
Author
The function is always running with the default value, even if I put anything else in my call. From what I can see in my address bar, the paramter seems to be sent along with the call :
http://www.example.com/my_view?clef=author
From the little I know, it seems correct to me, but since it doesn't work, I must come to the evidence that it isn't. What would be the cleanest way to call my function with the right parameter?
The reason why it's not working is because query strings aren't passed as arguments to your controller method. Instead, you need to grab them from the request like this:
public function my_function(Request $request)
{
$songs = Song::orderBy($request->query('clef'))->get();
return View::make('my_view', compact('songs'));
}
Extra tidbit: Because Laravel uses magic methods, you can actually grab the query parameter by just doing $request->clef.
Laravel URL Parameters
I think assigning parameters need not be in key value pair. I got it to work without names.
If your route is like /post/{param} you can pass parameter as shown below. Your URL will replace as /post/100
URL::action('PostsController#show', ['100'])
For multiple parameters say /post/{param1}/attachment/{param2} parameter can be passed as shown below. Similarly your url will be replaced as /post/100/attachment/10
URL::action('PostsController#show', ['100', '10'])
Here show is a method in PostsController
In PostsController
public function show($param1 = false, $param2 = false)
{
$returnData = Post::where(['column1' => $param1, 'column2' => $param2 ])->get();
return View::make('posts.show', compact('returnData'));
}
In View
Read More
In Routes
Route::get('/post/{param1}/attachment/{param2}', [ 'as' => 'show', 'uses' => 'PostsController#show' ] );
URL Should be: http://www.example.com/post/100/attachment/10
Hope this is helpful.