0

I'm working with Laravel on my school project (beginner in programming), and I have an error that I don't know how to solve.

So when I try to add/update something in my table, the following error occurs:

Insomnia POST

enter image description here

the table, model and controller are as follows:

Schema::create('TblAnunUsuario', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->id('idAnunUser');
            $table->string('TituloAnunUser');
            $table->string('DescAnunUser');
            $table->integer('PrecoAnunUser');
            $table->string('RequisitosAnunUser');
            $table->string('ImgAnunUser');
            $table->char('StatusAnunUser', 1);
            $table->dateTime('DataAnunUser');
            $table->unsignedBigInteger('idUserAnunUser')->unsigned();
            $table->foreign('idUserAnunUser')->references('idUser')->on('TblUsuario');
            $table->unsignedBigInteger('idTipoServAnunUser')->unsigned();
            $table->foreign('idTipoServAnunUser')->references('idTipoServ')->on('TblTipoServico');  
use HasFactory;
    public $timestamps = false;
    protected $primaryKey = 'idAnunUser';
    protected $table = 'TblAnunUsuario';
    protected $fillable = ['TituloAnunUser', 'DescAnunUser', 'PrecoAnunUser', 'RequisitosAnunUser','ImgAnunUser', 'StatusAnunUser', 'DataAnunUser', 'idUserAnunUser', 'idTipoServAnunUser'];
public function addAnunUsuario(Request $request)
    {
        $anunuser = New TblAnunUsuario();

        $anunuser->TituloAnunUser = $request->TituloAnunUser;
        $anunuser->DescAnunUser = $request->DescAnunUser;
        $anunuser->PrecoAnunUser = $request->PrecoAnunUser;
        $anunuser->RequisitosAnunUser = $request->RequisitosAnunUser;
        $anunuser->ImgAnunUser = $request->ImgAnunUser;
        $anunuser->StatusAnunUser = $request->StatusAnunUser;
        $anunuser->DataAnunUser = $request->DataAnunUser;
        $anunuser->idUserAnunUser = $request->idUserAnunUser;
        $anunuser->idTipoServAnunUser = $request->idTipoServAnunUser;

        $result = $anunuser->save();

        if($result)
        {
            return response()->json([
                'status'=>'200',
                'message'=>'Anúncio inserido com sucesso',
            ]);
        } else {
            return response()->json([
                'status'=>'400',
                'message'=>'Falha ao inserir o anúncio',
            ]); 
        }
    }
James Z
  • 12,209
  • 10
  • 24
  • 44
Shalgun
  • 3
  • 1
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Nov 19 '22 at 09:29

1 Answers1

2

first option you have : $table->string('DescAnunUser')->nullable(); and run migration again And second option you have:

public function addAnunUsuario(Request $request)
{           $request->validate([
            'DescAnunUser' => 'required'
            ]);  
      $anunuser = New TblAnunUsuario();

    $anunuser->TituloAnunUser = $request->TituloAnunUser;
    $anunuser->DescAnunUser = $request->DescAnunUser;
    $anunuser->PrecoAnunUser = $request->PrecoAnunUser;
    $anunuser->RequisitosAnunUser = $request->RequisitosAnunUser;
    $anunuser->ImgAnunUser = $request->ImgAnunUser;
    $anunuser->StatusAnunUser = $request->StatusAnunUser;
    $anunuser->DataAnunUser = $request->DataAnunUser;
    $anunuser->idUserAnunUser = $request->idUserAnunUser;
    $anunuser->idTipoServAnunUser = $request->idTipoServAnunUser;

    $result = $anunuser->save();

    if($result)
    {
        return response()->json([
            'status'=>'200',
            'message'=>'Anúncio inserido com sucesso',
        ]);
    } else {
        return response()->json([
            'status'=>'400',
            'message'=>'Falha ao inserir o anúncio',
        ]); 
    }
}
Umer Fayyaz
  • 357
  • 3
  • 12
  • the first solution is not valid because I need to add a new anun, and all the fields are null in the Insomnia post (the image), the second option works for the Insomnia POST, but in MYSQL it doesn't create a new anun in the table. – Shalgun Nov 19 '22 at 05:20
  • @Shalgun add TituloAnunUser filed also in required field like $request->validate([ 'DescAnunUser' => 'required', TituloAnunUser => 'required' ]); – Umer Fayyaz Nov 19 '22 at 07:55
  • you must add required validation in all your fields bcz if user not fill in field then it send a required msg – Umer Fayyaz Nov 19 '22 at 07:56