Problem with table data insertion via Aravel

Asked

Viewed 145 times

0

I have a project in Laravel at the same time I learn to use the framework, I need to insert a certain value in a table field, for this there is a condition I put in the controller:

public function setExpert()
{
    if($concessionaria->user_id = 0)
    {
        $concessionaria->expert = 0;
        $concessionaria->save();
    }
    else
    {
        $concessionaria->expert = 1;
        $concessionaria->save();
    }

The idea is to create a button where an expert can no longer be an expert at a dealership, for this I created a condition that if there is already an expert at a given dealership the value of a boolean field is set to 0(Not specialist, this when he presses this button) automatically, and if it does not exist will be set as 1 (when pressing the button), I do not know if it was clear, if necessary I explain again. This is the model with the data I need:

class ConcessionariaDelete extends Model
{
public $table = "concessionaria";

public $fillable = [
    'user_id',
    'expert',
    'nome',
    'expert_da_concessionaria',
    'slug',
    'tipo',
    'area',
    'cep',
    'endereco',
    'view_count',
    'status',
    'created_at',
    'updated_at'
];

inserir a descrição da imagem aqui

These are the fields that need to be changed

What I did create was a model to manage the database table, and a controller connected to the model with the conditions to set whether or not it is expert, my problem being to perform the function, I just need to reload the page? Is it necessary to put it on some page to be executed? I didn’t understand correctly how to make changes to my controller’s conditions in my database.

  • I can help you, but first I need to understand a little better the structure of your project. Will each dealership have only one specialist? What are the actions that this expert will do within your project?

  • The idea is that each dealership has 3 experts, he becomes expert from points that he makes in the course of the career, but I have not yet made a button to stop being expert, I intend to use the function setExpert() in a button made in javascript via ajax, the problem is that I am not able to assign this function to the button, and even if I manage not know if it will work, I believe that only if I reload my database should already appear the changes I conditioned in setExpert().

  • You have to see the other fields are not mandatory if you need to pass the registration if an update is wrong

2 answers

0

Eloquent should not be identifying the primary key of the table.

class ConcessionariaDelete extends Model
{
 ...
 protected $primaryKey = "sua chave primária"
 ...
  • No need to specify if the name is ID the ID already interprets the ID as primaryKey

0

If an update follows an example:

public function setExpert($id)
{
   $concessionaria =  Model::find($id);
   if($concessionaria->expert === 0){
        $concessionaria->expert = 0;  
   }else{
     $concessionaria->expert = 1;
   }


   $concessionaria->save();

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.