How to get the second argument from route in laravel - laravel

I am trying to get the second argument from the route in laravel from the following code.
web.php
Route::get('users/{name}/forksnippets/{name2}','Forksnippet#viewforksnippet');
controller
public function viewforksnippet($slug){
dd($slug);
}
getting name here as result

Just add the second argument to the method of the controller.
public function viewforksnippet($slug, $name2)
{
dd($slug, $name2);
}

public function viewforksnippet($name, $name2)
{
dd($name2);
}
Please try this ex

Related

Request get method route handling

I am using laravel 5 . I want to set route like
http://localhost:8000/website/product-search-data?term=k
I also tried with
Route::get('/website/product-search-data/*', 'websiteController#searchProductList');
public function searchProductList($term)
{
dd($term);
}
But its providing me Sorry, the page you are looking for could not be found. How can I handle and get parameer value ?
Maybe can try this instead:
Route::get('/website/product-search-data/{term}', 'websiteController#searchProductList');
public function searchProductList($term) {
dd($term);
}

Route not defined when passing parameter to a route

I have a named route with a parameter which looks like this...
Route::get('/profile/{user_id}', [ProfileController::class, 'profile'])->name('profile');
Now in one of my controller,
I have a function that calls this route like this
public function _myFunction($some_data) {
return redirect()->route('profile', [$some_data->user_id]);
}
and in my ProfileController's profile() function, I have this.
public function profile() {
return view('modules.profile.profile');
}
I've followed the documentation and some guides I found in SO, but I got the same error,
"Route [profile] not defined."
can somebody enlighten me on where I went wrong?
Here's what my routes/web.php looks like...
Route::middleware(['auth:web'])->group(function ($router) {
Route::get('/profile/{user_id}', [ProfileController::class, 'profile'])->name('profile');
});
When calling the route, you should pass the name of the attribute along with the value (as key vaue pairs). In this case, your route is expecting user_id so your route generation should look like this:
return redirect()->route('profile', ['user_id' => $some_data->user_id]);
Read more on Generating URLs To Named Routes in Laravel.
I solved the issue, and its really my bad for not providing a more specific case information and made you guys confused.
I was using socialite and called _myFunction() inside the third party's callback..
After all, the problem was the socialite's google callback, instead of placing the return redirect()->route('profile', [$user->id]) inside _myFunction(), what I did was transfer it to the callback function.
So it looked like this now...
private $to_pass_user_id;
public function handleGoogleCallback()
{
try {
$user = Socialite::driver('google')->user();
$this->_myFunction($user);
return redirect()->route('profile', [$this->to_pass_user_id]);
} catch (Exception $e) {
dd($e->getMessage());
}
}
public function _myFunction($some_data) {
... my logic here
$this->to_pass_user_id = $some_value_from_the_logic
}

BadMethodCallException Method App\Http\Controllers\CvController::create does not exist

Having the issue when loading the cvs/create being return an error of;
My route file;
My CvController;
Application frames (1)
in your route
CvController#create
but in your CvController
public function cerate()
spellings mistake.
create != cerate
There is a spell mistake in your function
public function create() {
return view('cv.create');
}

Multiple implicit model binding with one controller in Laravel

I am trying to attach multiple models with one controller using implicit model binding but I am getting the following error if I try to attach more than one model with methods.
index() must be an instance of App\\Http\\Models\\Modelname, string given
Here is my code:
public function index(Model1 $model1,Model2 $model2,Model3 $model3)
{
print_r($application_endpoint);
}
Route:
Route::resource("model1.model2.model3","MyController",["except"=>["create","edit"]]);
Your route should looks like that:
Route::resource("your_route/{model1}/{model2}/{model3}","MyController",[
"except"=>["create","edit"]
]);
Yess you can register routes like these
Route::resource("model1.model2.model3","MyController",["except"=>["create","edit"]]);
but in your controller, you have to
public function index($id,$id2,$id3)
{
print_r($application_endpoint);
}
OR
you can do like this
Route::model('key/key/key', 'MyController')
and in your controller
public function index(Model1 $model1,Model2 $model2,Model3 $model3)
{
print_r($application_endpoint);
}

Laravel 4: get url parameter from controller

I'm new to laravel 4 and I'm currently having the following problem:
In my routes.php I have the line:
Route::get('maps/{map_type}', 'MapsController#loadMaps');
And in my MapsController I'd like to get the {map_type} to use it.
So my question: How can I retrieve map_type in my Controller?
Your first argument in your loadMaps method is your map type.
public function loadMaps($map_type) {
return $map_type;
}
Take a look at the first two code snippets on http://laravel.com/docs/controllers.
Controllers receive parameters as parameters in the called function:
class MapsController extends BaseController {
public function loadMaps($map_type) {}
}
Check http://laravel.com/docs/controllers#basic-controllers for more info

Resources