1

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>
Yohei Umezu
  • 421
  • 1
  • 6
  • 30
  • Are those columns in the `fillable` attribute of your model? – aynber Jan 20 '20 at 16:15
  • yes. I have a protected $fillable . – Yohei Umezu Jan 20 '20 at 16:18
  • Why Auth::guard('blogger')->user(); is not assigned to a variable ? – Foued MOUSSI Jan 20 '20 at 16:25
  • @YoheiUmezu There is typo in the method name. It should be `$request->hasFile` not `$request->hasfile` . In addition to that your controller function has second parameter as Blogger instance but it should be string if your are passing it to the route. Moreover you should submit images (files) using POST or PUT requests. – PassionInfinite Jan 20 '20 at 16:26
  • @PassionInfinite Thank you I fixed to hasFile. And do you mean I need to change from get to POST in route ? – Yohei Umezu Jan 20 '20 at 16:39
  • Yes. So if you are updating the resource. You should follow the endpoint convention having POST request to `/blog`. If you are using GET it won't be able to upload the file to the server. It can only get the resources from the server. So your `$request->hasFile` will be false every time. – PassionInfinite Jan 20 '20 at 16:42
  • https://stackoverflow.com/questions/8659808/how-does-http-file-upload-work This link might help you to understand you more about file upload. – PassionInfinite Jan 20 '20 at 16:42
  • @PassionInfinite I changed to Route::POST('/create', 'BloggersController@store')->name('blogs.store'); But the situation is still same. When I click the register btn, the page redirects pretty fast. So it seems I am failing to upload img file. – Yohei Umezu Jan 20 '20 at 16:50
  • Write `dd($request->all())` in the first line of your store function and try to submit the form once again. These will show you all your request parameters. Please post it in the reply of the comments so that I can also understand what is going on. Thanks! – PassionInfinite Jan 20 '20 at 17:01
  • @PassionInfinite Nothing happens. – Yohei Umezu Jan 20 '20 at 17:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206294/discussion-between-passioninfinite-and-yohei-umezu). – PassionInfinite Jan 20 '20 at 17:11

1 Answers1

0
public function store(Request $request, Blogger $blogger){

         // If you are using route model you already have a model instance
        $blogger_id = $blogger->id

        //Auth::guard('blogger')->user();

       if($request->hasFile('image')){

           $file = $request->file('image');
           $ext = $file->getClientOriginalExtension();
           $filename = time().'.'.$ext;
           $file->move(public_path("bloggers"), $filename);
           $path = '/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());

}
Foued MOUSSI
  • 4,643
  • 3
  • 19
  • 39