The form is not being saved in the comic

Asked

Viewed 76 times

0

Controller

public function cadastro()
{
    return View('/cadastro');
}

public function novo()
{        
    $user = \App\User::where('User',"=", Input::get("User"))->first();

            $user->name = Input::get('name');
            $user->host = Input::get('host');
            $user->login = Input::get('login');
            $user->password = hash('sha256', Input::get('password'));
            $user->save($user);

}

Routes

Route::get('cadastro', 'Auth\RegisterController@cadastro');
Route::post('cadastro', 'Auth\RegisterController@novo');

View

<body>        

<section method="POST" action="cadastro/novo">

    <nav class="navbar navbar-default navbar-static-top">
        <h1><b>CADASTRE SUA EMPRESA!</b></h1>
        <hr>        
    </nav>  
    <div id="area">
        <form id="formulario">       
            <fieldset style = "width: 200%; margin: 0px auto;">                    
                        <img src="/imagens/cliente.png" width="60px" height="60px" required/>
                        <input type="text" name="name" class="name" placeholder="Nome:" required><br>
                        <input type="text" name="host" class="host" placeholder="Host:" required><br>
                        <input type="text" name="email" class="email" placeholder="Email:" required><br>
                        <input type="password" name="password" class="senha" placeholder="Senha:" required>
                        <legend><input type="submit" value="Cadastrar" onclick="return change(this);"/></legend>                    
            </fieldset>
        </form>
    </div>    

</section>

 <img class="canto" src="/imagens/unius.png"/>    

 <footer>
    <p>Desenvolvido por: Vitória</p>
</footer>

Model User

    class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email','password','tipo'
    ];

    protected $hidden = [
        'password', 'remember_token', 'tipo'
    ];

    public function clients ()
    {
      return $this->belongsToMany('App\client', 'client_user');  
    }

    class clientUser extends Model
    {
        public $timestamps = false;
    }
}

Table clients: This is my client table, which is where I’m struggling, because the form is not saving on it.

Table users: Where are my users.

Table clienteUser: Onde faz a relação das duas tabelas... Who is an admin user selects which clients developer users can see.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Register view!

inserir a descrição da imagem aqui

  • Is there an error? this would be a new registration record?

  • No error, just not saved in the BD! And yes, it is a new record that should be saved in a table.

  • You are wearing Eloquent?

  • Yes, I’m!!!!!!

  • I made a reply helped you @Vitoria?

  • More or less @Virgilio.. Gives this error "Errorexception in Model.php line 485: Argument 1 passed to Illuminate Database Eloquent Model::save() must be of the type array, Object Given, called in C: Users Vit ria Desktop Project app Http Controllers Auth Registercontroller.php on line 189 and defined" so if I try to put a timestamps on the model, it gives error this error: "syntax error, Unexpected 'class' (T_CLASS), expecting Function (T_FUNCTION)"

  • Put your User things in question

  • I already put the code!

  • You put the class clientUser within the User, is wrong that too and one user can be multiple Clientesuser? Look at your programming that is wrong first and why of so many problems!

  • It can be yes. Pq is a relationship table in Mysql.

  • Do the following Vitória put the layout of these two tables with the relationships in your question, I can generate the models and position you better, if you want of course...

  • @Virgilionovic I set the tables!

  • I put an answer if you saw Victoria?

  • @Virgilionovic N solved for me!

Show 9 more comments

2 answers

0

There are many mistakes starting with method and action in the wrong places they should be contained in the tag <form>, as an example below:

Html: View

<section>
    <nav class="navbar navbar-default navbar-static-top">
        <h1><b>CADASTRE SUA EMPRESA!</b></h1>
        <hr>        
    </nav>  
    <div id="area">
        <form id="formulario" method="POST" action="/cadastro/novo">       
            <fieldset style = "width: 200%; margin: 0px auto;">                    
                <img src="/imagens/cliente.png" width="60px" height="60px" required/>
                <input type="text" name="name" class="name" placeholder="Nome:" required><br>
                <input type="text" name="host" class="host" placeholder="Host:" required><br>
                <input type="text" name="email" class="email" placeholder="Email:" required><br>
                <input type="password" name="password" class="senha" placeholder="Senha:" required>
                <legend><input type="submit" value="Cadastrar" onclick="return change(this);"/></legend>                    
            </fieldset>
        </form>
    </div>    
</section>
 <img class="canto" src="/imagens/unius.png"/>    
 <footer>
    <p>Desenvolvido por: Vitória</p>
</footer>

In the part of the routes are like this:

Route::get('cadastro', 'Auth\RegisterController@cadastro');
Route::post('cadastro/novo', 'Auth\RegisterController@novo');

And in the methods of controller:

public function cadastro()
{
    return View('/cadastro');
}

public function novo()
{        
    $user = new \App\User();
    $user->name = Input::get('name');
    $user->host = Input::get('host');
    $user->login = Input::get('login');
    $user->password = hash('sha256', Input::get('password'));
    $user->save();
}

This is the basic form, I’m not taking into account redirects, but so basically I should save the information, despite that has the field email in view which is not being used in the controller, then there may be more mistakes there !!!

@Edit

There would be 3 classes to represent your database model, and each recording and its given table, thus need to expose the intermediate table and can be done operations normally:

class User extends Authenticatable
{
    use Notifiable;

    protected $table = "users";
    protected $primaryKey = "id";
    protected $fillable = ['name', 'email','password','tipo'];
    protected $hidden = ['password', 'remember_token'];   

    public function ClientUser()
    {
        return $this->hasMany(ClientUser::class, 'user_id', 'id');
    } 

}

class Client extends Model
{
    protected $primaryKey = "id";
    protected $table = "clients";  

    public function ClientUser()
    {
        return $this->hasMany(ClientUser::class, 'client_id', 'id');
    }      
}

class ClientUser extends Model
{
    protected $primaryKey = "id";
    protected $table = "client_user"; 
    protected $fillable = ['user_id', 'client_id'];   
    public $timestamps = false;

    public function User()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public function Client()
    {
        return $this->hasOne(Client::class, 'id', 'client_id');
    }
}

Now just create the routes and pages (Views) for each model and save the data.

0

You are trying to search for a field that is not on the form.

Input::get("User");

If you are a new user, you should create a new instance, for example:

$user = new \App\User;

$user->name = Input::get('name');
$user->host = Input::get('host');
$user->login = Input::get('login');
$user->password = hash('sha256', Input::get('password'));

$user->save($user);
  • It wasn’t :( It’s the same way!

  • Just try "$user->save()"; If it doesn’t work, before "$user->save($user);", put "echo $user; die;" without the quotes and post here what is returned.

  • syntax error, Unexpected 'User' (T_STRING), expecting Function (T_FUNCTION)

  • The bug is now in Model

  • In this command: "class User extends Model () { public $timestamps = false; }"

Browser other questions tagged

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