Home>

I want to upload an image file from the posting screen.
siteI wrote an image file upload program with reference to
I get the following error message and I don't know how to solve it.
Would you please teach me how to resolve the error so that I can upload the image?

Error message: Call to a member function storeAs () on string
C: \ xampp \ htdocs \ jinminshinbun \ app \ Http \ Controllers \ ArticleController.php: 78

An error has occurred in the following part of the controller (ArticleController.php: 78).
$article->image_url = $request->image_url->storeAs ('public/post_images', $time.'_'. $Request->user ().'. Jpg');

controller

<? php
namespace App \ Http \ Controllers;
use Illuminate \ Http \ Request;
use App \ Article;
use App \ Http \ Requests \ ArticleRequest;
use Illuminate \ Support \ Facades \ Auth;
class ArticleController extends Controller
{
    // //
    // //
    public function index ()
    {
      Abbreviation ...
    }
    public function create ()
    {
       Abbreviation ...
    }
    public function store (ArticleRequest $request, Article $article)
    {
        $article->title = $request->title;
        $article->body = $request->body;
        $article->user_id = $request->user ()->id;
        // Upload image
        // $time = date ("Ymdhis");
        // $article->image_url = $request->image_url->storeAs ('public/post_images', $time.'_'. $Request->user ().'. Jpg');
        // $article->image_url = $request->image_url->Storage :: putFile ('image_url', $request->file ('image_url'));
        $article->image_url = $request->image_url->storeAs ('public/post_images', $time.'_'. $Request->user ().'. Jpg');
        dd ($article);
        $article->save ();
        return redirect ()->route ('articles.index');
    }
}

View

<!-Title->

  <label>Title</label>
  <input type = "text" name = "title" required value = "{{old ('title')}}">

<!-Body->

  <label></label>
  <textarea name = "body" required rows = "16" placeholder = "body">{{old ('body')}}</textarea>

<!-Post image->

    
      <span>Choose files<i aria-hidden = "true"></i></span>
      <input type = "file" name = "image_url" value = "{{old ('image_url')}}" multiple>
    
Postscript: form part
@section ('content')
  
    
      
        
          
            @include ('error_card_list')
            
            <form method = "POST" action = "{{route ('articles.store')}}">
                @include ('articles.form')
              <button type = "submit">Post</button>
            </form>
            
          
        
      
    
  
@endsection
Postscript: Result of dd ($request);
+ request: Symfony \ Component \ HttpFoundation \ ParameterBag {# 1353 ▼
    #parameters: array: 4 [▼
      "_token" =>"7Ah0JObQ5QhecxGgq7ntwnOMECp7xYUgCWjLsaQv"
      "title" =>"dadad"
      "body" =>"dada"
      "image_url" =>"IMG_2693.JPG"
    ]
  }
Sites browsed for resolution:

I tried to change the avatar part and variables by copying the following description written in the file name specification in the file upload column of the following site, but it did not work.

$path = $request->file ('avatar')->storeAs (
    'avatars', $request->user ()->id
);


https://readouble.com/laravel/5.8/ja/filesystem.html

  • Answer # 1

    Of viewformElements of theenctypeTo the attributemultipart/form-dataTry to specify.

    <form method = "POST" action = "{{route ('articles.store')}}" enctype = "multipart/form-data">
    Postscript

    Looking at the contents you checked, I feel that it is not multipart/form-data for some reason.
    In the case of multipart/form-data, if you look at the body part of the request with Firefox developer tools, it will look like the figure below. Looking at the red frame, it feels like binary data is being sent.

    It should look the same in Chrome, but for some reason, it's just a screenshot when submitting without attaching the file. When you try it yourself, around the arrowButton called "view source"There is, so please press it.

  • Answer # 2

    + request: Symfony \ Component \ HttpFoundation \ ParameterBag {# 1353 ▼
    #parameters: array: 4 [▼
    "_token" =>"7Ah0JObQ5QhecxGgq7ntwnOMECp7xYUgCWjLsaQv"
    "title" =>"dadad"
    "body" =>"dada"
    "image_url" =>"IMG_2693.JPG"
    ]
    }

    You only receive the file name.

    It should look like this.


    ... abbreviation ... $request->image_url->storeAs (... abbreviation ...)

    Why not use file ()?

    Sites browsed for resolution:
    https://readouble.com/laravel/5.8/ja/filesystem.html

    Is described as follows.

    https://readouble.com/laravel/5.8/ja/filesystem.html#file-uploads

    $path = $request->file ('avatar')->storeAs (
    'avatars', $request->user ()->id
    );

  • Answer # 3

    It has been deleted. It has been deleted.