Avatar of Eduardo Fuerte
Eduardo Fuerte
Flag 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:

img015
Thanks in advance!
LaravelJavaScriptPHPjQueryAJAX

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
Chris Stanyon

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?
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.
Chris Stanyon

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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Eduardo Fuerte

ASKER
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:

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

Open in new window


With error:
img002

Could you check also?
ASKER CERTIFIED SOLUTION
Chris Stanyon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Chris Stanyon

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.
Eduardo Fuerte

ASKER
Yes, I saw this, it's due an adaptation from another table. I had adjusted it.

Just to say everything is running now!!!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Eduardo Fuerte

ASKER
Chris

Thank you for another outstanding assistance!
Chris Stanyon

Excellent news