Send parameter to controller via Laravel Route - laravel

Given the following code, I simple want the second route to send an arbitrary value for id or any other variable I can access from within show();
Route::get('foo/{id}', 'FoobarController#show')->where('id', '[0-9]+');
Route::get('bar', 'FoobarController#show')->with('id', -1); // This pseudo-code doesn't work. I want to send parameter id with an arbitrary value

Why not like this?
Routes:
Route::get('bar/{id?}', 'FoobarController#show')->where('id', '[0-9]+');
Controller:
class FoobarController extends Controller{
public function show($id){
$id = $id ? $id : "default value";
}
}
Or:
public function show($id="default value"){..}

Related

How to get result in Laravel with where clause while passing model in controller method

I have a route to get a single post item by slug.
Route
Route::get('post/{post}', 'PostController#details')->name('post.details');
While I want to pass the model in the controller method for the route.
Controller
public function details(Post $post)
{
// how to get the post by slug
}
My question is how can I get the post by slug passing in the route
instead of post ID?
I am aware that I can pass the slug and get the post using where clause.
//Route
Route::get('post/{slug}', 'PostController#details')->name('post.details');
//Controller method
public function details($slug)
{
$post = Post::with('slug', $slug)->first();
}
But I want to learn to do the same by passing the Model in the method.
set route key name to your model class
//Post.php
public function getRouteKeyName()
{
return 'slug';
}
This will inform Laravel injector/resolver to look the variable passed in slug column while fetching the object instance.
What you want to do is implicit route model binding
What you can do is in your Post model define getRouteKeyName like below
<?php
class Post extends Model
{
/**
* Get the route key for the model.
*
* #return string
*/
public function getRouteKeyName()
{
return 'slug';
}
}
and define your route like this:
Route::get('post/{post:slug}', 'PostController#details')->name('post.details');
and then in your controller
public function details(Post $post)
{
// it will return post with slug name
return $post;
}
Hope it helps.
Thanks

How to call a controller function as a callback for the validation rules?

Deal All,
I have a controller which is extending the CI_Controller like this;
class MY_Controller extends CI_Controller
{
function check_unique($table, $field, $message_label, $value, $except_id){
$query = "SELECT * FROM $table WHERE $field = $value AND id != $except_id LIMIT 1";
if(count($this->db->query($query)->row_array()) == 0 ){
return TRUE;
}else{
$this->form_validation->set_message($field, "The field '" . $message_label . "' is not available. Please try a different value.");
return FALSE;
}
}
}
In my Client class -> edit function, that is...,
class Client extends MY_Controller
{
function edit($id){
$this->form_validation->set_rules("name", "Name", "required|max_length[50]");
}
}
...I want to check if name is being duplicated while update. Is there a way I can call the "check_unique" function from the controller in the validation rules?
You can use Form validation libraries callback feature to call your custom validation functions.
As your custom validation method is in MY_Controllerand you are inheriting it in your Clientclass it will work fine as following.
function edit($id){
$this->form_validation->set_rules("name", "Name", "required|max_length[50]|callback_check_unique");
}
Remember you need call your function using callback_ prefix.
In the Callback function, you can pass arguments as well. By default, CI will pass string parameter as an argument to your callback function. So you may need to modify your custom function little bit

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"));
}

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...

Laravel 5 : get route parameter in Controller 's constructor

I defined routes for a controller this way :
/model/{id}/view
/model/something/{id}/edit
I need to get the id parameter in the contructor of this controller. For example :
class ArtController extends Controller {
public function __construct(Request $request){
//dd($this->route('id')); //Doesn't work
//dd($request->segments()[1]); //this works for the first route but not the second
}
}
How can you get the parameter id in the constructor of a Controller in Laravel?
You should be able to do something like this
$id = Route::current()->getParameter('id');
Update:
Starting in laravel 5.4 getParameter was renamed to parameter
$id = Route::current()->parameter('id');
public function __construct(Request $request)
{
$id = $request->route('id');
dump($id);
}
You can call anywhere:
request()->route('id');
No need for injection
not use segments
use segment
public function __construct(Request $request){
dd($request->segment(1));}

Resources