I am building a blog system with laravel.
Now I have a blogger table that has a name, email address, and password.
In addition to the default account table, I want to save a profile image and introduction. They belong to the blogger table in my case. But I cannot save those two records.
I cannot figure out why profile records cannot be inserted into my DB.
And my user role is a blogger.
I can see ?_token on the url.
blogger table
Schema::create('bloggers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
blogs table
Schema::create('blogs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('bloggers');
$table->string('image');
$table->string('introduction');
$table->timestamps();
});
blogger.php
public function blogs()
{
return $this->hasMany(Blog::class, 'user_id');
}
blog.php
public function user(){
return $this->belongsTo(Blogger::class, 'user_id');
}
bloggersController.php
public function store(Request $request, Blogger $blogger_id){
$blogger_id = DB::table('bloggers')->where('id', $blogger_id)->get();
Auth::guard('blogger')->user();
if($request->hasfile('image')){
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$filename = time().'.'.$ext;
$file->move('bloggers/', $filename);
$blog = Blog::updateOrCreate(
['user_id' => $blogger_id],
[
'image'=>$filename,
'introduction' => $request->introduction,
]
);
}
return view('bloggers.create')->with('bloggers', Blogger::all())->with('blogs', Blog::all());
}
web.php
Route::get('/create', 'BloggersController@store')->name('blogs.store');
create.blade.php
<form action="{{ route('blogs.store') }}" enctype="multipart/form-data">
@csrf
<img src="{{asset('blog-image/alexandre-chambon-zxmSX2-GQBc-unsplash.jpg')}}" alt="card-background" class="card-img">
<div class="image-preview" id="imagePreview">
@if(empty(Auth::guard('blogger')->user()->blog->image))
<img src="{{asset('avatar/avatar.png')}}" id="image-preview__image" alt="avatar">
@else
<img src="{{asset('bloggers/')}}/{{ Auth::guard('blogger')->user()->blog->image}}" id="preview" alt="profile image">
@endif
</div>
<input type="text" class="name" value="{{ Auth::guard('blogger')->user()->name }}" name="name">
<textarea name="introduction" id="" cols="30" rows="10" class="profile">
@if(!empty(Auth::guard('blogger')->user()->blog->introduction)){{ Auth::guard('blogger')->user()->blog->introduction }}@endif
</textarea>
<div class="preview">
<input type="file" id="file" class="file1" accept="image/*" name="image">
<label for="file">
Add profile photo
</label>
</div>
<button type="submit" id="register">Register</button>
</form>