You need to point your form to the route where the destination is the function you will be entering, for example;
Your form:
<form method="post" action="{{ route('cadastro/novo') }}">
...
</form>
Your Route:
Route::post('cadastro/novo', 'UsuariosController@create');
No Controller UsuariosController
you can use the class User
to manipulate BD data because every model is linked to a table in the MAS database in the plural, for example;
model user
-> table users
model cat
-> table cats
use App\User;
class UsuarioController extends Controller {
// A função Request tera todos os dados enviados via post.
public function create(Request $request) {
// Pega os dados enviado atraves do POST exceto o input _token
// que é enviado por padrão atraves do form.
$dados = $request->except('_token');
// A inserção acontece aqui...
User::create($dados);
}
To search the database you can continue using the model you want because it already has several methods to facilitate manipulation.
$user = User::find(1); // Busca o usuario com o ID 1
$user = User::where('nome', 'oNome'); // Busca o usuario onde o campo "nome" seja "oNome".
In the case of udpate you can do;
User::where('id', 999)->update(['sobrenome' => 'novoSobreNome']);
// Buscara e atualizara o usuário com ID 999 substituindo o campo sobrenome com "novoSobreNome"
OR
$user = User::where('id', 999);
$user->idade += 1;
$user->save();
You can consult all available methods accessing the page of Eloquent
of the Laravel.
But one of the fields is a code and I need to add 1 to this code before giving the Insert. How could I do this?
– Marcos Birro
All models have methods to search for information such as
where
, then you first need to fetch the data you want in the database to perform the update and/or send the update directly.– RFL
I am already playing from the form action to the function below in the controller and it worked/; public Function writOrdem() { $major code = DB::table('ordemservicos')->max('code'); $major code = $major code + 1; Return $major code;
– Marcos Birro
This is where you want Mysql itself already does when placing the column
auto_increment
.– Diego Souza
@Marcosbirro yes, this way also works, see the change I made.
– RFL
The field cannot be autoincrement. I already have the ID as the Prim and autoincrement key. I need to do it manually but by example with the model I believe it will work because I will take field by field and I can add one to the code (Which is not primary Heart).
– Marcos Birro
I tested and is giving an error in $request.
– Marcos Birro
Errorexception in Ordemservicocontroller.php line 293: Undefined variable: request
– Marcos Birro
But it is stated?:
– Marcos Birro
class Ordemservicocontroller extends Controller { protected $request; public Function __Construct(Request $request) { $this->request = $request; }
– Marcos Birro
you do not need to define the object of
Request´ como atributo da classe, use apenas como
$request->`– RFL