Postingan lainnya
Laravel Store to Database doesn't work and false redirect
| <?php
|
| namespace App\Http\Controllers\Auth;
|
| use App\Http\Controllers\Controller;
| use App\Models\User;
| use Illuminate\Support\Facades\DB;
| use Illuminate\Http\Request;
| use Illuminate\Support\Facades\Auth;
| use Illuminate\Support\Facades\Hash;
| use Illuminate\Support\Facades\Redirect;
|
| class RegisterController extends Controller
| {
| public function index()
| {
| return view("auth.register");
| }
|
| public function postRegister(Request $req)
| {
|
| $credentials = $req->validate([
| "name" => 'required',
| "identity" => 'required|digits:10|numeric',
| "email" => 'required|email',
| "password" => 'required'
| ]);
|
| if(!Auth::check($credentials))
| {
| return redirect()->back();
| }else{
|
| DB::table("users")->insert([
| 'name' => $req->name(),
| 'identity' => $req->identity(),
| 'email' => $req->email(),
| 'roles' => "student",
| 'password' => Hash::make($req->password())
| ]);
|
| $req->session()->regenerate();
|
| return redirect("/dashboard");
| };
|
| }
Permisi semuanya, izin bertanya terkait kode diatas yang tidak bisa memasukkan data ke database dan salah redirect. Thank You!
1 Jawaban:
<div>Terdapat beberapa perubahan yang perlu dilakukan pada kode di atas untuk memperbaiki masalah tersebut:<br><br></div><ol><li>Pada bagian <strong>if(!Auth::check($credentials))</strong> ganti menjadi <strong>if(!Auth::attempt($credentials))</strong>, karena fungsi <strong>Auth::check()</strong> hanya memeriksa apakah pengguna telah login, sedangkan <strong>Auth::attempt()</strong> digunakan untuk mencoba melakukan otentikasi pengguna dengan kredensial yang diberikan.</li><li>Pada bagian <strong>DB::table("users")->insert([ 'name' => $req->name(), 'identity' => $req->identity(), 'email' => $req->email(), 'roles' => "student", 'password' => Hash::make($req->password()) ]);</strong> pastikan bahwa nama kolom pada tabel database sesuai dengan nama yang diberikan pada method <strong>insert()</strong>. Jika terdapat perbedaan nama, maka perlu dilakukan perubahan pada nama kolom.</li><li>Pada bagian <strong>return redirect("/dashboard");</strong> pastikan bahwa rute "/dashboard" telah didefinisikan pada file <strong>web.php</strong> dan ditangani oleh controller yang sesuai.</li></ol><div><br></div><pre><?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller; use App\Models\User; use Illuminate\Support\Facades\DB; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Redirect;
class RegisterController extends Controller { public function index() { return view("auth.register"); }
public function postRegister(Request $req)
{
$credentials = $req-&gt;validate([
"name" =&gt; 'required',
"identity" =&gt; 'required|digits:10|numeric',
"email" =&gt; 'required|email',
"password" =&gt; 'required'
]);
if(!Auth::attempt($credentials))
{
return redirect()-&gt;back();
}
else
{
DB::table("users")-&gt;insert([
'name' =&gt; $req-&gt;name,
'identity' =&gt; $req-&gt;identity,
'email' =&gt; $req-&gt;email,
'roles' =&gt; "student",
'password' =&gt; Hash::make($req-&gt;password)
]);
$req-&gt;session()-&gt;regenerate();
return redirect("/dashboard");
}
}
} <br></pre>
Tanggapan
Baik, saya akan coba kak!