Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag 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


User generated image
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:
 User generated image

But nothing is saved at table Vitrines.


Could you check?

Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eduardo Fuerte

ASKER

Hi Chris

Something wrong occured:

  User generated image
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.
Exact!

I'm seeking the routes....
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.
So, I have:

For vitrines:
User generated image
For videos:
User generated image
Something different on the middleware...
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' ?
This project has a folder routes

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

 User generated image
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.
I don't know if I really understood what you meant....

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

User generated image
User generated image
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


User generated image
But that's would be another subject...
Chris

Thank you for the help!
No worries Eduardo. Glad I could help.