Avatar of Eduardo Fuerte
Eduardo Fuerte
Flag for Brazil asked on

Could you clear why this Controller method isn't called as expected?

Hi Experts


Could you clear why this Controller method isn't called as expected?

Accordingly with a previous question this form have 02 phases

1st: Upload a picture to a defined path - In my tests by using  
use Illuminate\Support\Facades\Storage;

Open in new window

It's perfectly locally uploaded.


2nd  The image url and details must to be saved at the table Vitrines

So, this method is called

Vitrine.prototype.salvar = function(){
    
  console.log ('JS salvar');  // Pass here
  
  $.ajax({
    url: '/admin/vitrine/salvar',
    method: "POST",
    data: {
      _token: $('input[name="_token"]').val(),
      id: $('#id').val(),
      url: $('#url').val(),
      inicio: $('#inicio').val(),
      termino: $('#termino').val(),
      title: $('#title').val(),
      description: $('#description').val(),
      points: $('#points').val()
    },
    error:function(data){
      hotsite.openModalCustom("Erro", data.responseText, "Entendi", "error");
    },
    success: function(data){
      hotsite.openModalCustom("Sucesso ao salvar", data.retorno, "Entendi", "success");
      hotsite.closeModal('mEditor');
      //hotsite.vitrine.filtrar();
    }
  });
};

Open in new window


img008
This method is configured at routes

Route::post('/admin/vitrine/salvar',['uses'=>'admin\VitrineController@salvar'])->middleware('cadastro');

Open in new window


But it doesn't seen it's called at VitrineController

 public function salvar(Request $request){
 
       info('Em salvar');

       //----------It must to stop here if it's called. But not.
        dd($request);
       //---------------------------------------------

        $id=$request->input('id');
        $url=$request->input('url');
        $inicio=$request->input('inicio');
        $termino=$request->input('termino');

        $titulo = $request->input('title');
        $descricao = $request->input('description');
        $pontuacao = $request->input('points');
  
        try{
            if($id != '' &&  $id > 0){
              $vitrine = Vitrine::find($id);
            }
  
            if(empty($vitrine)){
              $vitrine = new Vitrine();
              $vitrine->created_at=new \DateTime();
            }
                
            $vitrine->title = $titulo;
            $vitrine->description = $descricao;
            $vitrine->points = CustomFuncs::brancoParaNulo($pontuacao);
            $vitrine->url = $url;
            $vitrine->validity_start = empty($inicio) ? null : CustomFuncs::formatarDataUS($inicio);
            $vitrine->validity_end = empty($inicio) ? null : CustomFuncs::formatarDataUS($termino);
            $vitrine->updated_at=new \DateTime();
            
            info($vitrine);
            
            
            $vitrine->save();
  
        } catch (\Exception $e) {
            return response()->json(new Resultado(true,'','Erro ao salvar o vídeo'), 400);
        }
  
        return response()->json(new Resultado(false,'','Vídeo cadastrado com sucesso'));
    
    }

Open in new window



And a success message is presented by the above JS code:
 img007

But nothing is saved at table Vitrines.


Could you check?

Thanks in advance
LaravelJavaScriptPHP

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Eduardo Fuerte

ASKER
Hi Chris

Something wrong occured:

  img009
Chris Stanyon

That looks like you're trying to use Passport but haven't installed it correctly. You need to run

php artisan passport:install

so that the correct keys are created in the storage folder.
Eduardo Fuerte

ASKER
Exact!

I'm seeking the routes....
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Chris Stanyon

Once you've got Passport isntalled correctly, then run the php artisan route:list command and you'll see a list of all your routes.
Eduardo Fuerte

ASKER
So, I have:

For vitrines:
img010
For videos:
img011
Something different on the middleware...
Eduardo Fuerte

ASKER
The routes:

/* Vitrine  EF 2020 */
Route::get('/vitrine',['uses'=>'VitrinesController@index'])->middleware('cadastro');
Route::get('/vitrine/{id}',['uses'=>'admin\VitrineController@obter'])->middleware('cadastro');
Route::get('/vitrine/{id}/obter',['uses'=>'admin\VitrineController@obter'])->middleware('cadastro');
Route::post('/vitrine/salvarescolha',['uses'=>'VitrinesController@salvarEscolha'])->middleware('cadastro');
// Rota adicionada:
Route::post('/admin/vitrine/salvarVitrine',['uses'=>'admin\VitrineController@salvarVitrine'])->middleware('cadastro');
Route::post('/admin/vitrine/salvar',['uses'=>'admin\VitrineController@salvar'])->middleware('cadastro');



/* Vídeos */
Route::get('/videos', 'VideoController@index')->middleware('cadastro')->middleware('auth');
Route::get('/videos/{type}/filtrar', 'VideoController@filtrar')->middleware('cadastro')->middleware('auth');
Route::post('/videos/watched', 'VideoController@watched')->middleware('cadastro')->middleware('auth');
Route::get('/videos/sugestao', 'VideoController@sugestionIndex')->middleware('cadastro')->middleware('auth');
Route::post('/videos/sugestao', 'VideoController@sugestion')->middleware('cadastro')->middleware('auth');

Open in new window


Isn't it a matter of to change the middleware to 'auth' ?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Eduardo Fuerte

ASKER
This project has a folder routes

And the file  admin.php controls it too (I guess)

 img012
Chris Stanyon

OK.

I think we're missing a vital part of your Route setup. In the screenshots you've just shown me, your route is setup like

Route::get('/videos/obter', 'VideoController@obter');

But when you run route:list, it clearly shows that the routes have the admin url prefix and the admin namespace added to them. This leads me to believe that somewhere in your routes setup, you're declaring a prefix('admin') and a namespace('admin'), but you haven't shown any of that.

That's a vital part of getting your routes set up correctly.
Eduardo Fuerte

ASKER
I don't know if I really understood what you meant....

In your example the method "obter" is under admin folder:

img013
img014
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Eduardo Fuerte

ASKER
I adjusted the web.php to  consider the route outside \admin\    by using VitrinesController ( at participants area like all the other existents methods)

Route::post('/vitrine/salvarVitrine',['uses'=>'VitrinesController@salvarVitrine'])->middleware('cadastro');
Route::post('/vitrine/salvar',['uses'=>'VitrinesController@salvar'])->middleware('cadastro');

Open in new window


The method is reached.

My dificulty now is to understand why does it subtly exit the method returning to JS as OK aparently doing nothing - doesn't allow trace nothing.

If I force an error I see it capture it.

     public function salvar(Request $request){
     
     
         aaa;
        
        //info('xxx');
       
        dd('aaa');

Open in new window


img015
But that's would be another subject...
Eduardo Fuerte

ASKER
Chris

Thank you for the help!
Chris Stanyon

No worries Eduardo. Glad I could help.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.