Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point why a Controller method code is subtly returning to a jquery ajax without running and returning OK to JS ?

Hi Experts

Could you point why a Controller method code is subtly returning to a jquery ajax without running and returning OK to JS ?

Accordingly to:

Vitrine.prototype.salvar = function(){
    
  console.log ('JS salvar');
  
  $.ajax({
    url: '/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


and Controller method:


 public function salvar(Request $request){
   
	 //Forcing an error
	 //aaa;
	
	
	// Otherwise subtly exit with no traces.
	info('xxx');
   
	dd('aaa');

	$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



The forced error:

User generated image
Thanks in advance!
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hey Eduardo,

Your Controller method has a dd('xxx') line in it. This basically 'dumps and dies' so nothing after it will run.  As far as AJAX is concerned, your method ran successfully (it did what is was supposed to do without error), which is why you get the success handler being fired. The info returned to your success handler will just be 'xxx', but your handler is trying to read data.retorno, which obviously doesn't exist.

What exactly is it you're trying to do with the dd('xxx') line?
Avatar of Eduardo Fuerte

ASKER

Hi Chris

Really.
This is a symptom of the last question whith my attempts to make the method to be called.
Now that the method seens to be called what I need is to trace what is done on it.
This method, called by JS, must to save data on table Vitrines - that isn't done.
As soon as you call dd(), your method will stop - nothing after it will run, so your code never gets to the part that saves the record.

Remove the dd() line and see how you go.

You can also make your life a little easier by using the firstOrNew() method.

try{
    $vitrine= Vitrine::firstOrNew(['id' => $id]);
    $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->save();
} catch()

Open in new window

Hi Chris

So did I then:

     public function salvar(Request $request){

        try{
            info($request);
            
            $id=$request->input('id');
            $url=$request->input('url');
            $inicio=$request->input('inicio');
            $termino=$request->input('termino');
  
            $vitrine= Vitrine::firstOrNew(['id' => $id]);
            $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->save();
        } catch (\Exception $e) {
            
            info($e);
            
            //return response()->json(new Resultado(true,'','Erro ao salvar o vídeo'), 400);
        }

		return response()->json(new Resultado(false,'','Foto cadastrada com sucesso'));
}

Open in new window


At least I could trace the error:

User generated image
What I did then:
protected $fillable = ['id', 'url', 'inicio', 'termino', 'title'];

Open in new window


With error:
User generated image

Could you check also?
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
Something else to look at is your variable names. Your request contains _token, id, url, ignicio, termino, title, description, points but your method is using variable called $titulo, $descricao, $pontuacao which are never set anywhere.
Yes, I saw this, it's due an adaptation from another table. I had adjusted it.

Just to say everything is running now!!!
Chris

Thank you for another outstanding assistance!
Excellent news