Postingan lainnya
Laravel Socialite Trying to access array offset on value of type null
public function callback()
{
$usr = Socialite::driver('google')->stateless()->user();
$usr = User::where('google_id',$usr->id)->first();
if(isset($usr))
{
Auth::login($usr);
return redirect("/admin-wpa");
}else{
$new = User::create([
"name" => $usr['name'],
"email" => $usr['email'],
"google_id" => $usr['id'],
"roles" => "user",
"password" => Hash::make($usr['password'])]
);
Auth::login($new);
return redirect("/admin-wpa");
}
}
Izin bertanya, saya mengalami error saat menjalankan method kode diatas. setelah saya cari - cari caranya seperti downgrade, upgrade phpnya. Akan tetapi masih error seperti dijudul. Adakah solusi lainnya? composer update pun sudah saya lakukan dan hasilnya sama
1 Jawaban:
<div> Baris ini :</div><pre>$usr = User::where('google_id',$usr->id)->first();</pre><div>Seharusnya menghasilkan sebuah object, maka cara mengakses datanya adalah menggunakan tanda -> .</div><div>Solusinya coba agian ini :</div><pre>$new = User::create([
"name" => $usr['name'],
"email" => $usr['email'],
"google_id" => $usr['id'],
"roles" => "user",
"password" => Hash::make($usr['password'])]
);</pre><div><br>coba diubah menjadi seperti ini :</div><pre>$new = User::create([
"name" => $usr->name,
"email" => $usr->email,
"google_id" => $usr->id,
"roles" => "user",
"password" => Hash::make($usr->password)
);</pre><div>Semoga berhasil.</div>
Tanggapan
sama saja mas, tetap error dan errornya jadi ErrorException: Trying to get property of non-object
dan saya juga coba dd variabel new itu hasilnya cuman null
Cek baris ini $usr = User::where('google_id',$usr->id)->first(); apakah null?
Sudah kak seharusnya pake getEmail, get Name