Saturday, April 15, 2017

Missing argument 1 for App\Http\Controllers\

im new in laravel. I have a problem on my app version 5.2.45

I having Missing argument 1 for App\Http\Controllers\PhotoController::create() when i try to pass variable to view with information from database:

Route:

Route::get('photo/create/{id}', 'PhotoController@create');

Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use DB;

use Session;

class PhotoController extends Controller { private $table = 'photos';

// Show Create Form
public function create($gallery_id) {
    // render view
    return view('photo.create', compact('gallery_id'));
}

View:

@section('content')

<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <h1>Upload photo</h1>

        {!! Form::open(array('action' => 'PhotoController@store', 'enctype' => 'multipart/form-data')) !!}
        
        

        
        

        
        

        <input type="hidden" name="gallery_id" value="">

        
        {!! Form::close() !!}

    </div>
</div>
@endsection



via Dmitry Malys

Route Model Binding multiple parameters for tenant prefix Route::resource()

I'm developing an API using Laravel's Resource Controllers features and using Orchestral Multi-tenant Database Schema Manager as a Single Database.

In my control methods I am using the Route Model Binding to injecting a model ID to a route or controller action, often query to retrieve the model that corresponds to that ID.

Some relevant routes from my API:

| POST      | api/v1/{tenant}/fornecedores                     | store  | App\Http\Controllers\Api\Fornecedor\FornecedorController@store  | api,tenant,jwt.auth |
| GET|HEAD  | api/v1/{tenant}/fornecedores                     | index  | App\Http\Controllers\Api\Fornecedor\FornecedorController@index  | api,tenant,jwt.auth |
| GET|HEAD  | api/v1/{tenant}/fornecedores/count               |        | App\Http\Controllers\Api\Fornecedor\FornecedorController@count  | api,tenant,jwt.auth |
| PUT|PATCH | api/v1/{tenant}/fornecedores/{fornecedor}        | update | App\Http\Controllers\Api\Fornecedor\FornecedorController@update | api,tenant,jwt.auth |
| GET|HEAD  | api/v1/{tenant}/fornecedores/{fornecedor}        | show   | App\Http\Controllers\Api\Fornecedor\FornecedorController@show   | api,tenant,jwt.auth |
| GET|HEAD  | api/v1/{tenant}/fornecedores/{fornecedor}/audits |        | App\Http\Controllers\Api\Fornecedor\FornecedorController@audits | api,tenant,jwt.auth |
...

An example of my API route file:

// API V1
Route::group(['prefix' => 'v1/{tenant}', 'middleware' => 'tenant'], function () {

    // Fornecedores
    Route::group(['prefix' => 'fornecedores'], function () {
        Route::get('count', 'Api\Fornecedor\FornecedorController@count');
        Route::get('{fornecedor}/audits', 'Api\Fornecedor\FornecedorController@audits');

        Route::resource('/', 'Api\Fornecedor\FornecedorController', [
            'parameters' => ['' => 'fornecedor'],
            'except' => ['create', 'edit', 'destroy']
        ]);
    });
});

These are some methods of my FornecedorController.php:

public function index()
{
    return $this->fornecedorService->getFornecedoresPaginate();
}

// This is line 48 (throw below):
public function show(Fornecedor $fornecedor)
{
    return $fornecedor;
}

However when trying to access the URL that uses the Route Model Binding I get the following error:

URL: /api/v1/1/fornecedores/1

FatalThrowableError in FornecedorController.php line 48:
Type error: Argument 1 passed to App\Http\Controllers\Api\Fornecedor\FornecedorController::show() must be an instance of App\Models\Fornecedor, string given

And this is the all trace:

in FornecedorController.php line 48
at FornecedorController->show('1', object(Fornecedor))
at call_user_func_array(array(object(FornecedorController), 'show'), array('tenant' => '1', 'fornecedor' => object(Fornecedor))) in  Controller.php line 55
at Controller->callAction('show', array('tenant' => '1', 'fornecedor' => object(Fornecedor))) in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(FornecedorController), 'show') in Route.php line 203
at Route->runController() in Route.php line 160
at Route->run() in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in GetUserFromToken.php line 46
at GetUserFromToken->handle(object(Request), object(Closure)) in
...

I could verify that there is apparently a confusion when retrieving the parameters. When inspecting route parameters, this is the result:

public function show($fornecedor)
{
    dd(\Route::current()->parameters());
}

URL: /api/v1/1/fornecedores/18

Result:

array:2 [
  "tenant" => "1"
  "fornecedor" => "18"
]

And when trying to retrieve the provider's route:

public function show($fornecedor)
{
    dd($fornecedor);
}

URL: /api/v1/1/fornecedores/18

Result:

"1"

Even using bind the parameter I get is not correct. And the other routes that do not use a second parameter are ok.

Can someone tell me a better approach?

Thanks!



via Thiago Pereira

how to create admin login in laravel 5.3

I have created user login and registration form with the PHP artisan command php artisan make:auth. But now I have to create an admin panel and a seprate admin login page. How can I do this? Please instruct me on how can I do this.

Also, how can I restrict access to admin pages by standard users and redirect them to home page?



via M. Ibrar

I have already installed composer but while running command in command prompt It's showing an error.?

enter image description here This was error which i got while installing laravel.. I have reinstalled xampp then also there is a same error..!



via Hitesh

Simulate second login and query through relations off that

What ways are there to simulate another login without password (1 user, some playable characters).

It worked when a User created the Team. But it does make more sense that a Character creates/joins a Team. Since a User hasMany Characters, I'm completly screwed. How do I know which of all the Characters creates/joins a Team? It was easy with only 1 User doing this.

Probably missing some general programming skills, but I'm learning!

Relations
User hasMany Characters <> Character belongsTo User
Character belongsTo Team <> Team hasMany Characters

URL
In team.index I really don't get how to collect all Characters of a Team. The url is currently just /team. Do I have to use something like /{character}/{team}? But I like the simplicity of just /team.

Code
I came up with variants of the following, but if feels wrong. And is wrong :-D

// how would the app know which of the Characters.Team we need. App and me don't know...
$team = auth()->user()->characters()->team()->first();
$characters = \App\Character::where('team_id', $team->id)->get();

Clarify this for me, please!

Are the relations wrong?
Is my approach wrong?
Am I missing easy Laravel magic?

Thank you.



via Bensen

How to secure communication between Laravel and Lumen?

Laravel backend will be accessing to Lumen via API using Guzzle.

I don't need a database in Lumen, so how can I add security between Laravel and Lumen? As far I am aware if I need pass token I would database access in Lumen.

Lumen is for internal use, which is not for the public to access.



via I'll-Be-Back

Laravel: getting stored filename

I am storing an uploaded file using the following code on my controller:

$stored_file = $request->file('image')->store(Config::get('filesystems.uploads.gallery_dir'));
Log::info($stored_file);

The $stored_file variable contains the full path of the file once saved, is there a function within Laravel to return just the generated name? (without the path)

Currently $stored_file returns uploads/gallery/wg7Aa4frj8PQkH89oIseMtlpFtruiJNsHTOoretn.jpeg
I would like to to return wg7Aa4frj8PQkH89oIseMtlpFtruiJNsHTOoretn.jpeg



via Imran

Advertisement