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!

avatar Ihzarizky
@Ihzarizky

39 Kontribusi 9 Poin

Diperbarui 1 tahun yang lalu

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")-&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()) ]);</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>&lt;?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-&amp;gt;validate([
        &quot;name&quot; =&amp;gt; &#039;required&#039;,
        &quot;identity&quot; =&amp;gt; &#039;required|digits:10|numeric&#039;,
        &quot;email&quot; =&amp;gt; &#039;required|email&#039;,
        &quot;password&quot; =&amp;gt; &#039;required&#039;
    ]);

    if(!Auth::attempt($credentials))
    {
        return redirect()-&amp;gt;back();
    }
    else
    {
        DB::table(&quot;users&quot;)-&amp;gt;insert([
            &#039;name&#039; =&amp;gt; $req-&amp;gt;name,
            &#039;identity&#039; =&amp;gt; $req-&amp;gt;identity,
            &#039;email&#039; =&amp;gt; $req-&amp;gt;email,
            &#039;roles&#039; =&amp;gt; &quot;student&quot;,
            &#039;password&#039; =&amp;gt; Hash::make($req-&amp;gt;password)
        ]);

        $req-&amp;gt;session()-&amp;gt;regenerate();

        return redirect(&quot;/dashboard&quot;);
    }
}

} <br></pre>

avatar yukari06
@yukari06

137 Kontribusi 66 Poin

Dipost 1 tahun yang lalu

Tanggapan

Baik, saya akan coba kak!

Login untuk ikut Jawaban