Laravel error: Call to a member function store() on null - laravel

When I tried to send any file I get this message
It's form in home.blade.php
<form action="{{ URL::to('/upload') }}" enctype="multipart/form-data" method="post">
<input type="file" name="something" >
{{ csrf_field() }}
<button type="submit" name="button">Upload</button>
</form>
It's function inside a Controller
public function store(Request $request)
{
$path = $request->file('something')->store('upload');
echo $path;
}

Try to change controller code like this and see the result:
public function store(Request $request) {
$path = $request->something;
echo $path; }

Related

why Resource Controllers not working in laravel 8?

route link
Delete
define route
Route::resource('users', UserController::class);
controller function
public function destroy($id)
{
$delete = DB::table('users')->delete($id);
if ($delete) {echo 'success';}
}
in the delete route, the method should be delete, you can write it in this way
<form action="{{ route('users.destroy', $user->id) }}" method="post">
#csrf
#method('delete')
<button type="submit">Delete</button>
</form>
first of all you need to find specific id then delete the same one
$user = User::findOrFail($id);
$user ->delete();

404 not found on laravel post request

I'm making a post request in my form. The csrf token and the method are there.
<div class="flex">
<form action="{{ route('profile.store.follower', $user) }}" method="post">
#csrf
<input value="follow" type="hidden" name="follow" value="{{$user->id}}" >
<button type="submit" class="bg-green-700">Follow</button>
</form>
</div>
The route:
Route::get('/usersprofile/{user:name}/index', [ProfileController::class, 'index'])->name('profile.index');
Route::post('/usersprofile/{user:name}/index', [ProfileController::class, 'store'])->name('profile.store.follower');
The controller:
public function store(User $user, Request $request) {
dd($user);
$user = user::findOrFail($request->follow);
Auth::user()->following->attach($user->id);
return back();
}
The following function in the user model:
public function following() {
return $this->belongsToMany(User::class, 'followers', 'user_id', 'following_id');
}
For some reason, it's not going through. I've tried php artisan route:cache and config:cache but the error persists. I've checked the route list and the route exists.
Your hidden input has two value attributes, that's probably why it doesn't find a user and fails.
Remove the hidden input and the line $user = user::findOrFail($request->follow); - the user you want to follow already exists in $user via your route. Then use Auth::user()->following->attach($user); to follow.
<div class="flex">
<form action="{{ route('profile.store.follower', $user) }}" method="post">
#csrf
<button type="submit" class="bg-green-700">Follow</button>
</form>
</div>
public function store(User $user, Request $request)
{
Auth::user()->following->attach($user);
return back();
}

Update data in laravel 6

I try to create crud in laravel 6. Create, Read and Delete process is running well. But when Update process, the data in table not change. Could anyone help me to find the problem ? The following my code.
Route
Route::get('/blog', 'BlogController#index');
Route::get('/blog/add','BlogController#add');
Route::post('/blog/store','BlogController#store');
Route::get('/blog/edit/{id}','BlogController#edit');
Route::post('/blog/update','BlogController#update');
Controller
public function index()
{
$blog = DB::table('blog')->get();
return view('blog',['blog' => $blog]);
}
public function edit($id)
{
$blog = DB::table('blog')->where('blog_id', $id)->get();
return view('edit', ['blog'=>$blog]);
}
public function update(Request $request)
{
DB::table('blog')->where('blog_id',$request->blog_id)->update([
'blog_title' => $request->title,
'author' => $request->author]);
return redirect('/blog');
}
View
#foreach ($blog as $n)
<form method="post" action="/blog/update" />
{{ csrf_field() }}
Title <input type="text" name="title" value="{{ $n->title}}">
Author<input type="text" name="author" value="{{ $n->author}}">
<button type="submit" class="btn btn-secondary">Update</button>
</form>
#endforeach
You must provide id in your route
Route::post('/blog/update/{id}','BlogController#update');
In update method add parameter id and then find product against id
public function update(Request $request, $id)
{
DB::table('blog')->where('blog_id',$id)->update([
'blog_title' => $request->title,
'author' => $request->author]);
return redirect('/blog');
}
#foreach ($blog as $n)
<form method="post" action="{{ route('your route name'), ['id' => $$n->id] }}" />
{{ csrf_field() }}
Title <input type="text" name="title" value="{{ $n->title}}">
Author<input type="text" name="author" value="{{ $n->author}}">
<button type="submit" class="btn btn-secondary">Update</button>
</form>
#endforeach
try separating the update into two statements like so
$blog = DB::table('blog')->where('blog_id',$id)->first();
$blog->update([
'blog_title' => $request->title,
'author' => $request->author]);
Also you might want to use models in the future so you can do it like
$blog = Blog::where('blog_id',$id)->first();
Doesn't really shorten your code but it improves the readibility.
Do your update like this:
public function update(Request $request)
{
$post = DB::table('blog')->where('blog_id',$request->blog_id)->first();
$post->blog_title = $request->title;
$post->author = $request->author;
$post->update();
return redirect('/blog');
}

Laravel 5.8 request data is empty

router.php
Route::get('/p00001/edit/{center}/{id}',
'Dashboard\Programs\P00001Controller#edit');
Route::put('/p00001/update/{center}/{id}',
'Dashboard\Programs\P00001Controller#update');
form.blade.php
<!DOCTYPE html>
<html dir="ltr" lang="zh-TW">
<body>
<form id="mainform" action="/dashboard/programs/p00001/update/JPN/3121-111907230007" method="post" class="form-horizontal">
{{ csrf_field() }}
{{ method_field('put') }}
<input type="text" value='1234' id="inpTest" name="inpTest">
<input type="submit" name="inpSubmit">
</form>
</body>
</html>
P00001Controller.php
public function __construct(Request $request, P00001Service $P00001Service )
{
$this->request = $request;
}
public function update($center, $id)
{
$data = $this->request->all();
echo "<pre>", print_r($_POST, 1), "</pre>";
echo "<pre>", print_r($this->request->all(), 1), "</pre>";
}
I got empty array.
I created a test.php
<?php echo "<pre>", print_r($_POST, 1), "</pre>"; ?>
If I change the action to test.php, it's ok, I can get $_POST data.
I searched for hours, no solution can fix my problem.
Delete the Request object from construct and add it into the update function.
public function update(Request $request, $center, $id) {
$data = $request->all();
//code
}
Edit:
I added the link of laravel documentation for you.
To obtain an instance of the current HTTP request via dependency injection, you should type-hint the Illuminate\Http\Request class on your controller method.
Visit this to explore more.
Thanks
Make route as named route
Route::get('/p00001/edit/{center}/{id}','Dashboard\Programs\P00001Controller#edit')->name('edit');
Route::put('/p00001/update/{center}/{id}', 'Dashboard\Programs\P00001Controller#update')->name('update');
In view
<form id="mainform" action="{{route('update',['center' => 'JPN','id' => '3121-111907230007'])}}" method="post" class="form-horizontal">
{{ csrf_field() }}
{{ method_field('put') }}
<input type="text" value='1234' id="inpTest" name="inpTest">
<input type="submit" name="inpSubmit">
</form>
In controller
public function update(Request $request,$center, $id)
{
$data = $request->all();
echo "<pre>", print_r($_POST, 1), "</pre>";
echo "<pre>", print_r($request->all(), 1), "</pre>";
}
Don't create request object in constructor...Create in function definition.
I find out the problem. I use LaravelLocalization package. So if the locale is not provided, it redirects! After redirect, there are only url parameters!!
The action should be
/ja-JP/dashboard/programs/p00001/update/JPN/3121-111907230007
Check if you have the DB facade:
use Illuminate\Support\Facades\DB;

post form action to controller in laravel

First of all I want to say that my problem is very similar to this question, only the answer is not working in my case.
I am trying to connect my form action to my UserController. it's an update avatar function. Here's what it look like.
in my profile/show.blade
<div class="col-md-12 justify-content-center">
<form action="{{ action('UsersController#update_avatar') }}" method="post" enctype="multipart/form-data">
#csrf
</form>
</div>
UsersController
public function avatar()
{
$user = Auth::User();
return view('dashboard.profile'.$user->id,compact('user',$user));
}
public function update_avatar(Request $request){
$request->validate([
'avatar' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$user = Auth::User();
$folder = 'avatars';
Storage::delete($folder.'/'.$user->avatar);
$avatarName = $user->id.'_avatar'.'.'.request()->avatar->getClientOriginalExtension();
$request->avatar->storeAs('avatars',$avatarName);
$user->avatar = $avatarName;
$user->save();
return back()
->with('success','You have successfully upload image.');
}
routes/web.php
Route::get('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#avatar');
Route::post('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#update_avatar');
here's the error I am receiving
Thank you so much in advance!
Change these routes:
Route::get('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#avatar');
Route::post('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#update_avatar');
To these:
Route::get('dashboard/profile/{{profile}}', 'UsersController#avatar')->name('user.avatar');
Route::post('dashboard/profile/{{profile}}', 'UsersController#update_avatar')->name('user.update_avatar');
And in your form use route instead of action
<form action="{{ route('user.update_avatar') }}" method="post" enctype="multipart/form-data">
#csrf
</form>
You can try do something like this:
Route::post('dashboard/profile','Dashboard\UsersController#update_avatar')->name(profile.update);
and use it in the form like this:
<form action="{{ route('profile.update') }}">

Resources