mysql - Filling database laravel php
Get the solution ↓↓↓I can fill my table with User::create(). It works, but I want fill with class, which I create in migrations. How I can do it? I am beginner at php laravel and can`t find information on this case. web.php:
<?php
Route::name('user.')->group(function (){
Route::view('/private','private')->middleware('auth')->name('private');
Route::get('/login',function(){
// Проверка на авторизацию пользователя. Если уже авторизован, то редирект на страницу профиля
return view('login');
})->name('login');
Route::post('/login',[\App\Http\Controllers\LoginController::class,'login']);
Route::get('/logout',function(){
Auth::logout();
return redirect('/');
})->name('logout');
Route::get('/registration', function () {
if(Auth::check()){
return redirect(route('user.private'));
}
return view('registration');
})->name('registration');
Route::post('/registration',[\App\Http\Controllers\RegisterController::class,'save']);
});
registration.blade.php:
@extends('layouts.app')
@section('title-block')
Регистрация
@endsection
@section('content')
<h1>Р’С…РѕРґ</h1>
<form class="col-3 offset-4 border rounded" method="POST" action="{{route('user.registration')}}">
@csrf
<div class="form-group">
<label for="number" class="col-form-label-lg">Р?РјСЏ</label>
<input class="form-control" id="number" name="number" type="text" placeholder="number">
</div>
<div class="form-group">
<label for="surname" class="col-form-label-lg">surname</label>
<input class="form-control" id="surname" name="surname" type="text" placeholder="surname">
</div>
<div class="form-group">
<label for="name" class="col-form-label-lg">Р?РјСЏ</label>
<input class="form-control" id="name" name="name" type="text" placeholder="Р?РјСЏ">
</div>
<div class="form-group">
<label for="password" class="col-form-label-lg">Пароль</label>
<input class="form-control" id="password" name="password" type="password" placeholder="Пароль">
</div>
<div class="form-group">
<button class="btn btn-lg btn-primary" type="submit" name="sendMe" value="1">Войти</button>
</div>
</form>
@endsection
RegisterController.php:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
class RegisterController extends Controller
{
public function save(Request $request){
if(Auth::check()){
return redirect(route('user.private'));
}
$validateFields = $request->validate([
'number' =>'required',
'password' => 'required',
]);
if(User::where('number',$request['number'])->exists()){
return redirect(route('user.registration'))->withErrors([
'number' => 'Такой пользователь уже зарегистрирован'
]);
}
$user = User::create([
'password' => $request['password'],
'name' => $request['name'],
'surname'=>$request['surname'],
'number'=>$request['number']
]);
if($user){
Auth::login($user);
return redirect(route('user.private'));
}
return redirect(route('user.login'))->withErrors([
'formError' => 'Произошла ошибка при сохранении пользователя'
]);
}
}
Migration file. This file creates the users schema. With this class I want to save users create_users_table:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users',function (Blueprint $table) {
$table->increments('id');
$table->string('surname',255)->nullable(false);
$table->string('name',255)->nullable(false);
$table->string('number',255)->nullable(false)->unique();
$table->string('father`s name',255)->nullable(false);
$table->string('parent`s number',255)->nullable(false);
$table->string('password',255)->nullable(false);
$table->string('studies',255)->nullable(false);
$table->string('classes',255)->nullable(false);
$table->string('remember_token',100)->nullable(true);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non well formed numeric value encountered
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.