if there an function to handle - laravel

All of my routes have a lang parameter and I need to unset it in the controller. How can I achieve this?
routes.php
Route::prefix('{lang?}/admin')->attribute('namespace','Admin')->middleware('auth:web')->group(function () {
Route::get('/branch/{branch}/products/create', ['uses' => 'BranchesController#createBranchProduct', 'as' => 'admin.branch.products.create']);
});
Controller:
public function createBranchProduct(Branch $branch)
{
$categories = Category::all();
return View::make('admin.branches.products.new',['branch' => $branch,'categories'=>$categories]);
}
I'm getting the following error:

Hello turky eltahawy and welcome to StackOverflow!
Let's take a look: you have grouped routes which have an optional parameter. Therefore, when you are calling createBranchProduct method, it expects two parameters: lang and instance/id of the Branch model.
What you can do is to accept 2 parameters in the createBranchProduct like this:
public function createBranchProduct($lang = null, Branch $branch)
{
$categories = Category::all();
return View::make('admin.branches.products.new',['branch' => $branch,'categories'=>$categories]);
}

I found an answer that I can make:
class baseController extends Controller {
public function callAction($method, $parameters){
unset($parameters['lang']);
return parent::callAction($method, $parameters); //TODO: Change the autogenerated stub
}
}

Related

Laravel Getting Data From Redirect

I have this route web.php
Route::get('/two-steps', [UserController::class, 'getTwoSteps']);
And I have these two functions :
public function getTwoSteps($email) {
return view('auth.two-steps', ['email' => $email]);
}
public function someFunction() {
return redirect('/two-steps')->with('email',$email_1);
}
my question is, how to pass $email_1 into getTwoSteps($email) function?
thank you

Too few arguments to function App\Http\Controllers\CategoryController::destroy(), 0 passed and exactly 1 expected

i can not delete it by id
and i have some code here for Model too
class Category extends Model
{
// Table Name
protected $table = 'categories';
// Primary Key
public $primaryKey = 'id';
// TimeStamps
public $timestamps = true;
protected $fillable= ['name','icon'];
public function getAllCategory(){
return DB::table(categories)->get();
}
public function createCategory($name,$icon){
$category= $this->create([
'name' => $name,
'icon' => $icon,
]);
return $category;
}
}
//here is a function in controller:
public function destroy($id)
{
$category = Category::findOrFail($id);
$category->delele();
return redirect('/admin.category');
}
I think the problem is in your routes/web.php file
You have to pass one argument to destroy method
eg:
Route::get('delete_category/{id}', 'CategoryController#destroy');
And your invoking URL will look like http://127.0.0.1:8000/delete_category/1
Here 1 will take as the value of id variable
The problem is rather somewhere in your view. You have route for example category.destroy but you need to pass id of model you want to destroy when creating link and you miss it and that's why you are getting this error.
You have one typo mistake and need some changes
public function destroy($id)
{
$category = Category::findOrFail($id);
$category->delete(); //type mistake
return redirect('/admin.category'); // it seems like route name
}
Better code
public function destroy($id)
{
$category = Category::findOrFail($id);
if($category) {
$category->delete();
return redirect()->route('admin.category' ,['error' => $error]);
}
return redirect()->route('admin.category');
}
In my case I forgot the name of the "Request" class.
Correct Parameter definition.
public function displayId(Request $Visitor){
return $Visitor->id;
}
Wrong Parameter definition.
public function displayId($Visitor){
return $Visitor->id;
}
SOLUTION : php artisan route:cache work on terminal.
I use that line and it worked. Firstly you check php artisan route:list and if you do not see destroy/{id} in list, you should work that command. To make that clear your route does not see {id} but when you work command, route paths in web.php update itself

Laravel domain group without having to pass domain parameter to controllers

I have all my routes in a domain group but I would like to avoid having the domain as a parameter in each controller method.
So I would like to avoid having this everywhere:
public function show($domain, $id) {}
and would like to just keep it as
public function show($id) {}
I was able to partially make it work with $request->route()->forgetParameter('subdomain') placed in a middleware but it doesn't work in the case of calling redirect()->action('SomeController#show') from a controller method.
Here are some more details:
First, all routes are in a domain group.
Route::middleware(['some_middleware'])->domain('{subdomain}' .website.com)->group(function () {
// .. All routes
} );
Then, in some_middleware I have
public function handle($request, Closure $next) {
// ..
$request->route()->forgetParameter('subdomain');
return $next($request);
}
Then where it doesn't work:
class SomeController {
public function process()
{
// ...
redirect()->action('SimpleController#show', ['simple' => $id]);
}
}
The error I'm getting is:
Missing required parameters for [Route: ] [URI: simples/{simple}].
This only works if I explicitly pass in the subdomain variable.
class SomeController {
public function process()
{
// ...
redirect()->action('SimpleController#show', ['subdomain'=>'some_subdomain', 'simple' => $id]);
}
}
Can anyone suggest a "fix" for this? Thanks in advance :)
With Laravel 5.5+, you can use URL::defaults to set request-wide values for things like the route helper.
https://laravel.com/docs/5.6/urls#default-values
public function handle($request, Closure $next) {
// ..
$subdomain = $request->route('subdomain');
URL::defaults(['subdomain' => $subdomain]);
$request->route()->forgetParameter('subdomain');
return $next($request);
}
You could create a wrapper helper for the action() helper:
if (! function_exists('actionSub')) {
function actionSub($name, $parameters)
{
return action($name, $parameters + ['subdomain' => request()->route('subdomain')]);
}
}
Then use it:
redirect(actionSub('SimpleController#show', ['simple' => $id]));
If someone has a more elegant solution for this, it will be great to see it.

show error while fetch username

show error : Missing argument 1 for App\Http\Controllers\AdminLoginController::name()
public function name($username) {
$user = AdminLogin::find($username);
return response()->json($user);
}
AdminLoginController: Its a adminlogin controller code
class AdminLoginController extends Controller{
public function show(){
$res ="Hello world!";
return response()->json($res);
}
public function log() {
$users = AdminLogin::all();
return response()->json($users);
}
public function name($username) {
$user = AdminLogin::where('username',$username)->first();
return response()->json($user);
}
RouteLoginController: Its a adminlogin controller code :
<?php
$app->get('/', function () use ($app) {
return $app->version();
});
$app->group(['prefix' => 'api/v1'], function ($app)
{
$app->get('adminlogin', 'AdminLoginController#show'); //get single route
$app->get('user', 'AdminLoginController#log'); //get single route
$app->get('username', 'AdminLoginController#name'); //get single route
$app->post('adminlogin', 'AdminLoginController#login'); //get single route
});
Error :
(1/1) ErrorException
Missing argument 1 for App\Http\Controllers\AdminLoginController::name()
Your controller method is taking the username param but the route binding is not passing one. Change your route
$app->get('username', 'AdminLoginController#name');
to
$app->get('user/{username}', 'AdminLoginController#name');
If you don't want to change your route, change your controller function signature to the below (as shown in the other answers), and make sure you are passing the 'username' as request param while invoking the url.
public function name(\Illuminate\Http\Request $request) {
$user = AdminLogin::where('username',$request->username)->first();
return response()->json(['user' => $user]);
}
You are probably calling this function using an ajax request and putting the name in the query string. In this case, the name parameter will not be sent as an attribute of the function but will be part of the request object.
You can solve this like so:
public function name(\Illuminate\Http\Request $request) {
$user = AdminLogin::find($request->username);
return response()->json($user);
}
You should try this :
public function name($username) {
$user = AdminLogin::where('username',$username)->first();
return response()->json(['user' => $user]);
}
OR
public function name(\Illuminate\Http\Request $request) {
$user = AdminLogin::where('username',$request->username)->first();
return response()->json(['user' => $user]);
}

Laravel 5 - How to passing request data from route to controller

I am new in laravel,
How to passed request data into controller ? Just like happen on view ?
Route::get('/kelihatan', function (Request $request) {
return view( 'pages' , [ 'page' => 'index' , '_request' => $request->all() ] );
});
How to passing request data to controller before passed into view or model ? Just like this ??
Route::get( '/{page}', 'UserController#show' );
Mention the path in route first depending upon the type of request i.e. Get or Post
Route::get('/invite/{code}', 'MyController#get_my_action');
Route::post('/invite/{code}', 'MyController#post_my_action');
Then in controller named as MyController create a function like
public function get_my_action($code){
//your code goes here
}
or
public function post_my_action(Request $request, $code)
{
//your code goes here
}
With that route:
Route::get('/{page}', 'UserController#show');
In the controller you can do that:
public function show()
{
$page = request()->route('page');
or that:
public function show($page)
or that:
public function show(Request $request, $page)
Try this:
Routes.php
Route::get( '/{page}', 'UserController#show' );
UserController.php
public function show($page) {
dd($page);
}
Inside your controller define your show function as below:
function show(Request $request)
{
echo $request['page'];//or whatever data you want to pass
}
///add follwoing line before you define controller
use Illuminate\Http\Request;

Resources