Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method save return 404 when there is a parameter id in the url #2216

Open
dam62500 opened this issue Apr 29, 2022 · 9 comments · May be fixed by #2299
Open

Method save return 404 when there is a parameter id in the url #2216

dam62500 opened this issue Apr 29, 2022 · 9 comments · May be fixed by #2299
Assignees
Labels

Comments

@dam62500
Copy link

Describe the bug
When we have a parameter in the url like an idenifiant for example a route like this
https://www.laravel.local/admin/accounts/1/company/add

i got error 404 when i tried to save

To Reproduce
Steps to reproduce the behavior:

  1. Create a route with id parent ( ex : https://www.laravel.local/admin/accounts/1/company/add )

Expected behavior
I have two tables, account and companies, in the account area, we can modify the account information and add companies, I have a 404 error just after I try to add a company

Screenshots
https://ibb.co/5Y8Gfdb
https://ibb.co/1XnQzfK

@dam62500
Copy link
Author

My route list :
GET|HEAD|POST admin/accounts/create/{method?} .................................................................. platform.gestion.accounts.create › App\Orchid\Screens\Accounts\AccountEditScreen@handle
GET|HEAD|POST admin/accounts/{accounts}/edit/{method?} ........................................................... platform.gestion.accounts.edit › App\Orchid\Screens\Accounts\AccountEditScreen@handle
GET|HEAD|POST admin/accounts/{account}/company/add/{method?} ...................................... platform.gestion.accounts.company.add › App\Orchid\Screens\Accounts\Company\CompanyEditScreen@handle
GET|HEAD|POST admin/accounts/{method?} ........................................................................... platform.gestion.accounts.list › App\Orchid\Screens\Accounts\AccountListScreen@handle

@dam62500
Copy link
Author

<?php

declare(strict_types=1);

namespace App\Orchid\Screens\Accounts\Company;

use App\Models\Account;
use App\Models\Company;
use App\Orchid\Layouts\Account\AccountEditLayout;
use App\Orchid\Layouts\Account\Company\CompanyEditLayout;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Orchid\Screen\Action;
use Orchid\Screen\Actions\Button;
use Orchid\Screen\Screen;
use Orchid\Support\Facades\Layout;
use Orchid\Support\Facades\Toast;

class CompanyEditScreen extends Screen
{
    /**
     * @var Account
     */
    public $account;

    /**
     * @var Company
     */
    public $company;

    /**
     * Query data.

     * @param Company $company
     *
     * @return array
     */
    public function query(Account $account, Company $company): iterable
    {
        return [
            'account'       => $account,
            'company'       => $company,
        ];
    }

    /**
     * Display header name.
     *
     * @return string|null
     */
    public function name(): ?string
    {
        return $this->company->exists ? 'Modifier une entreprise' : 'Créer une entreprise';
    }

    /**
     * Display header description.
     *
     * @return string|null
     */
    public function description(): ?string
    {
        return $this->company->exists ? "Modifier l'entreprise de ".$this->company->account->name : 'Créer une entreprise';
    }

    /**
     * @return iterable|null
     */
    public function permission(): ?iterable
    {
        return [
            'platform.gestion.accounts',
        ];
    }

    /**
     * Button commands.
     *
     * @return Action[]
     */
    public function commandBar(): iterable
    {
        return [
            Button::make(__('Supprimer'))
                ->icon('trash')
                ->confirm(__('Êtes-vous sur de vouloir supprimer ce compte ? Toutes les données liées seront également supprimées.'))
                ->method('remove')
                ->canSee($this->company->exists),

            Button::make(__('Sauvegarder'))
                ->icon('check')
                ->method('save'),
        ];
    }

    /**
     * @return \Orchid\Screen\Layout[]
     */
    public function layout(): iterable
    {
        $layout_top = [];
        $layout_bottom = [];
        if ($this->company->id !== null)
        {
            $layout_top = [

            ];
            $layout_bottom = [

            ];
        }
        return [
            ...$layout_top,

            Layout::block(CompanyEditLayout::class)
                ->title(__('Entreprise'))
                ->description(__('Informations de l\'entreprise')),

            ...$layout_bottom,
        ];
    }

    public function save(Request $request)
    {
        $request->validate([
            'company.name' => [
                'required',
            ],
            /*'company.logo' => [
                'required',
            ],*/
        ]);


        $companyData = $request->get('company');

        $companyData['account_id'] = $this->account->id;
        $companyData['logo'] = 'ok';
        $company = new Company();
        $company
            ->fill($companyData)
            ->save();

        Toast::info(__('Entreprise mis à jour avec succès.'));

        return redirect()->route('platform.gestion.accounts.edit', $company->account->id);
    }


    /**
     * @param Company $company
     *
     * @throws \Exception
     *
     * @return \Illuminate\Http\RedirectResponse
     *
     */
    public function remove(Company $company)
    {
        $company->delete();

        Toast::info(__('Company was deleted.'));

        return redirect()->route('platform.gestion.accounts.edit', $company->account->id);
    }
}

for method save , if push two parameters like this

public function save(Company $company, Request $request)

or

public function save(Account $account, Request $request)

i get a error 404 but

public function save(Request $request)

i can process my save method but i need $company and $account .. I don't know how to accomplish that...

@TomNealBorneman
Copy link

TomNealBorneman commented May 20, 2022

I have the same issue, after some digging through the screen flow of Orchid I found the following in src/Screen/Screen.php:197

/**
     * @param mixed ...$parameters
     *
     * @throws Throwable
     *
     * @return Factory|View|\Illuminate\View\View|mixed
*/
public function handle(...$parameters)
{
  Dashboard::setCurrentScreen($this);
  abort_unless($this->checkAccess(), 403);

  if (request()->isMethod('GET')) {
      return $this->redirectOnGetMethodCallOrShowView($parameters);
  }

  $method = Route::current()->parameter('method', Arr::last($parameters));

  $prepare = collect($parameters)
      ->merge(request()->query())
      ->diffAssoc($method)
      ->all();

  return $this->callMethod($method, $prepare) ?? back();
}

The handle function uses diffAssoc to remove the method (save in our case) from the parameters. But this only works if method is the first in the parameter collection. So if you have a parent in the parameters it does not remove the method. Then the method gets picked up as an identifier for your object, resulting in a 404 because there is no object with ID 'save'.

Replacing diffAssoc with just diff seems to work for me, but I don't know if that would break other cases.

edit: format

@xenocent
Copy link

xenocent commented Jun 9, 2022

any workaround with this @tabuna ? more than one params?

@danvirsen
Copy link

Any updates for this issue?

@danvirsen
Copy link

My issue was that I wanted to use a "create or update" method, which caused me to get a 404.
This worked fine with one parameter in the URL, but not when there were two.
For my use I just added separate "create" and "update" methods instead and call on them when appropriate.

So I guess I was having an altogether different issue.

@bravohex
Copy link

same error...😢

@bravohex
Copy link

bravohex commented Jun 2, 2023

The problem was resolved when updated Larvel

@demian18
Copy link

Hi @danvirsen,

I had a problem even passing one parameter. I've been following this article https://orchid.software/en/docs/quickstart-crud/

If you do everything as indicated, "editing" will work, but there will be a problem when "creating" - 404 (route not found).

I changed the $post variable to $post and was able to "create", but an error occurred - 404 when "editing".

I will be glad if you share your experience or advice. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
7 participants