The form is not being saved in the DB

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;
    }
}

Customers table: This is my clients table, which is where I'm having difficulty, because the form is not saving on it.

Users table: Where are my users.

ClientUser table: Where does the relationship of the two tables ... Who is the admin user selects which clients the developer users can see.

Viewofregister!

    
asked by anonymous 24.04.2017 / 19:18

2 answers

0

You are trying to fetch a field that is not in 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);
    
25.04.2017 / 03:35
0

There are many errors starting with method and action in the wrong places they should be contained in 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 routes part they look like this:

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

And in 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 am not taking redirects into account, but, so basically I should save the information, although it has the email field in view that is not being used in the controller method, so you may have more errors there!

@Edit

There would be 3 classes to represent your database model, and each one recording and your particular table, so you need to expose the middle table and the operations can be done 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.

    
25.04.2017 / 16:05