We help IT Professionals succeed at work.

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!
Comment
Watch Question

Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
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 FuerteDeveloper and Analyst

Author

Commented:
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.
Most Valuable Expert 2018
Distinguished Expert 2019

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

Eduardo FuerteDeveloper and Analyst

Author

Commented:
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?
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
Right OK.

The $fillable property needs to go on your Vitrine Model, not on your Controller. It basically tells your Model which properties can be mass-assigned:

class Vitrine extends Model
{
    ...
    protected $fillable = ['id', 'url', 'inicio', 'termino', 'title'];

Open in new window

At least we know the method is being fired correctly.
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
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 FuerteDeveloper and Analyst

Author

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

Just to say everything is running now!!!
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Chris

Thank you for another outstanding assistance!
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Excellent news