Ben kayıt olurken başka bir table da user_id
yaratıyorum
$user->about()->create(['user_id'=>Auth::id(),'mac' => $request->ip()]);
bu table da about table ve burda verileri güncellemek istiyorum fakat güncellediğimde şöyle bir hata veriyor
exception: "Error"
file: "D:\\TaskProject\\app\\Http\\Controllers\\Api\\AboutController.php"
line: 33
message: "Attempt to assign property \"name\" on null"
ve verileri günceleyemiyorum.
web.php
de Route::post("/api/abouts",[AboutController::class,"Post"]);
yaratıp güncellediğimde güncelliyor
public function Post(Request $request) {
$about = About::find(Auth::id());
$about->name = $request->name;
$about->save();
return new AboutResource($about);
}
Fakat ben bu yolla değilde apiResource ile güncellemek istiyorum. Yardım ederseniz sevinirim.
api.php
Route::apiResource('abouts',AboutController::class);
AboutController.php
public function store(Request $request)
{
$about = About::find(Auth::id());
$about->name = $request->name;
$about->about = $request->about;
$about->public_email = $request->public_email;
$about->website = $request->website;
$about->university = $request->university;
$about->experience = $request->experience;
$about->work_places = $request->work_places;
$about->identity = $request->identity;
$about->county = $request->country;
$about->city = $request->city;
$about->birthday = $request->birthday;
$about->save();
return new AboutResource($about);
}
AboutResource
public function toArray($request)
{
return [
'id' => $this->id,
'user_id' => $this->user_id,
'name' => $this->name,
'img' => $this,
'about' => $this->about,
'public_email' => $this->public_email,
'website' => $this->website,
'university' => $this->university,
'experience' => $this->experience,
'work_places' => $this->work_places,
'identity' => $this->identity,
'mac' => $this->mac,
'county' => $this->county,
'city' => $this->city,
'birthday' => $this->birthday
];
}
UserAbout.vue
<script>
import { ref } from 'vue'
import axios from 'axios'
export default {
setup() {
const form = ref({
'img' : '',
'name' : '',
'about' : '',
'public_email' : '',
'website' : '',
'university' : '',
'experience' : '',
'work_places' : '',
'country' : '',
'city' : '',
'birthday' : ''
})
const errors = ref("")
const success = ref("")
const addUserAbout = async () => {
const data = new FormData();
data.append('img',form.value.img)
data.append('name',form.value.name)
data.append('about',form.value.about)
data.append('public_email',form.value.public_email)
data.append('website',form.value.website)
data.append('university',form.value.university)
data.append('experience',form.value.experience)
data.append('work_places',form.value.work_places)
data.append('country',form.value.country)
data.append('city',form.value.city)
data.append('birthday',form.value.birthday)
const config = {
headers: {
'Content-Type': "multipart/form-data; charset=utf-8; boundary=" + Math.random().toString().substr(2)
}
}
await axios.post('/api/abouts',data,config);
}
const fileUpload = async (e) => {
form.value.img = e.target.files[0];
}
return {
form,
fileUpload,
addUserAbout,
errors,
success
}
}
}
</script>
About.php Model
class About extends Model
{
use HasFactory;
protected $table = "abouts";
protected $fillable = ['user_id','name','img','about','public_email','website','university','experience','work_places','identity','mac','country','city','birthday'];
public function user() {
return $this->belongsTo(User::class);
}
}