I am trying to upload an image to the public storage directory for a an instagram clone project I am currently doing. But when I refresh the page on the web, I get a Call to a member function store() on string error. I have already created and migrated the database for Posts, and added all the necessary fields. The functionality is for the user to be able to upload an 'image' and a 'caption '
My App\Http\Controllers\PostsController looks like this
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use illuminate\Support\Facades\Auth;
use App\Http\Controllers\PostsController;
class PostsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function create()
{
return view('posts.create');
}
public function store()
{
$data = request()->validate([
//'another =>'', Tells laravel to ignore all other fields
'caption'=>'required',
'image'=> 'required' ,
]);
$imagePath = request('image')->store('uploads','public');
Auth::user()->posts()->create([
'caption'=>$data['caption'],
'image'=>$imagePath,
]);
return redirect('/profile/' . Auth::user()->id);
//goes ahead and grabs the authenticated user
}
}
The create.blade.php file looks like this
@extends('layouts.app')
@section('content')
<div class="container">
<form action = "/p" encrypt="multipart/form-data" method="post">
@csrf
<div class="row">
<div class="col-8 offset=2">
<div class="row">
<h1>Add New Post</h1>
<div class="row mb-3">
<label for="caption" class="col-md-4 col-form-label">Post Caption</label>
<input id="caption"
type="text"
class="form-control @error('caption') is-invalid @enderror"
name="caption"
value="{{ old('caption') }}"
autocomplete="caption"
autofocus>
@error('caption')
<span class="invalid-feedback"
role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="row">
<label for="image" class="col-md-4 col-form-label">Post Image</label>
<input type="file" class="form-control-file" id="image" name="image">
@error('image')
<strong>{{ $message }}</strong>
@enderror
</div>
<div class="row pt-4">
<button class="btn btn-primary">Add New Post</button>
</div>
</form>
</div>
@endsection
```Routes
Route::get('/p/create', [App\Http\Controllers\PostsController::class, 'create'])->name('posts.create');
Route::post('/p', [App\Http\Controllers\PostsController::class, 'store']);
Route::get('/Profile/{user}', [App\Http\Controllers\ProfilesController::class, 'index'])->name('Profile.show');
Does your Laravel application already have the folders where you write with write permissions? If not, these are the permissions that should be applied to the following folders (cd to app folder to execute):
chmod -R a+w storage/app
chmod -R a+w storage/framework
chmod -R a+w storage/logs
chmod -R a+w storage/cache
chmod -R a+w public/user_content/
chmod -R a+w bootstrap/cache
chmod -R a+w .env
Other:
In the third line of App\Http\Controllers\PostsController
There should not be this "use": use App\Http\Controllers\PostsController;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.