Reputation: 337
I am studying Laravel and trying to insert a new row in my table estabelecimentos
.
Why is this working to insert a new row:
$estabelecimento = Estabelecimento::create([
'nome' => $nome
]);
And this is not working? Using var_dump
on save
method it returns null
.
$estabelecimento = new Estabelecimento();
$estabelecimento->nome = $nome;
$estabelecimento->save;
I already tried using try/catch
but it doesn't throw errors, it just don't insert the row and returns null
on save method.
My Class:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Estabelecimento extends Model
{
public $timestamps = false;
protected $fillable = ['nome'];
public function Comentarios()
{
return $this->hasMany(Comentario::Class);
}
}
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comentario extends Model
{
public $timestamps = false;
protected $fillable = ['nome', 'votes'];
public function Estabelecimento()
{
return $this->BelongsTo(Estabelecimento::Class);
}
}
estabelecimentos
name | type | extra |
---|---|---|
id | bigint(20) | PK - AUTOINCREMENT |
nome | varchar(100) | - |
Upvotes: 0
Views: 1587
Reputation: 3450
save
is a method and hence you call it with ()
.
so, replace the following line of code:
$estabelecimento->save;
with the following:
$estabelecimento->save(); // focus on `()`
Remember to always look at the documentation: https://laravel.com/docs/8.x/eloquent-relationships#the-save-method
Upvotes: 3