I have an edit form with a text field and an image field where a user can edit and upload new text or a image if he/she wants to. But if the user does not upload a new image I just want to keep the old image in the database.
My Problem
The problem is when I hit upload button, I'm redirected to the article page and no error is shown but the DB has not been updated what so ever.
Any help would be appreciated as I have tried multiple methods with no success.
Thank you in advance.
web.php
Route::get('/tests/edit/{id}', 'TestController#edit');
Route::patch('/tests', 'TestController#update');
TestController.php
public function edit(Request $request)
{
$test = Test::findOrFail($request->id);
return view('test.edit', ['test' => $test]);
}
public function update(Request $request, Test $test)
{
$test->user_id = $request->user()->id;
$test->name = $request->name;
if($request->hasFile('image')) {
Storage::delete('public/image/' . $test->image); //Delete old image
$path = $request->file('image')->store('public/image');
$test->image = basename($path);
}
$test->update();
return redirect('/tests')
}
edit.blade.php
#extends('layouts.app')
#section('content')
<div>
<form action="/tests" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('patch') }}
name:<input type="text" name="name" value='{{ $test->name }}'><br>
image: <input type="file" name="image"><br>
<input type='submit' value='upload'>
</form>
</div>
#endsection
edit.blade.php add this :
<input type="hidden" name="id" value="{{ $test->id }}">
TestController.php
use $test->save(); instead of $test->update();
I had the same problem as you.I work on a ordering website with laravel that you can get data from laravel and show it to user and also he can order something from the website and the database will be updated.But, I cannot neither get or update data in database.I work with wampserver, mysql and the problem is solved once I switched to MariaDB instead of mysql in phpmyadmin: http://localhost/phpmyadmin/
Related
Hi im new to laravel 9 and im trying to upload file from view to controller, but somehow the input file didnt pass the file to controller.
im using laravel 9 with boostrap in it.
heres my code :
view
<form enctype="multipart/form-data" action="{{ route('profile.put') }}" method="POST">
#csrf
#method('POST')
<div class="card-body">
<input id="file" name="file" type="file">
<button type="submit" class="btn btn-block btn-primary">{{ __('Save') }}</button>
</div>
</form>
controller
public function put(Request $request){
return $request;
}
i try to see the $request variable but the result is like this
enter image description here
i try the solution like add enctype="multipart/form-data" but still didnt work.
thank you in advance
I think you simply should get the request by its name or using `$request->all()`, you can do these two things:
In your controller you should write your put method like below:
public function put(Request $request){
$file = '';
if($request->file('file'))
{
$file = $request->get('file);
}
return $file;
}
or you can use the below code in your put method
public function put(Request $request){
return $request->all();
}
Note: If you don't send the file as a JSON Response you should return it to view after saving operation;
and also, there is no need to put #method('POST') inside the form;
my Route :
Route::resource('/posts',PostsControllerWithAll::class);
my form :
i try to read the given data from this form
<form method="POST" action="{{ action('PostsControllerWithAll#store') }}">
#csrf
<input type="text" name="title" placeholder="enter title">
<input type="submit" name="submit">
</form>
my controller :
public function create()
{
return view("posts.create");
}
public function store(Request $request)
{
return $request->all();
}
and this is my route list
route list image , please check the link image
Try using the named route, like you show on your print screen.
<form method="POST" action="{{ action('posts.store') }}">...</form>
To validated the fields values on store method, use dd($request->all()) this dump the variable and die, it's good to see what is expected.
I'm learning Laravel from Lacast and i'm trying to create a CRUD application.I've implemented the index,show,create and store correctly,but with the edit form when i try to submit data it's throwing BadMethodCallException.
Here are my routes
Route::get('/projects','ProjectsController#index');
Route::get('/projects/{id}','ProjectsController#show')->where('id','[0-9]+');
Route::get('/create','ProjectsController#create');
Route::post('/projects','ProjectsController#store');
Route::get('/projects/{id}/edit','ProjectsController#edit')->where('id','[0-9]+');
Route::put('/projects/{id}','ProjectsController#update')->where('id','[0-9]+');
Route::delete('/projects/{id}','ProjectsController#destroy');
Here is the edit form:
#extends('template');
#section('content')
<h2>Create new project</h2>
<p>/projects/{{ $project->id }}</p>
<form method="POST" action="/projects/{{ $project->id }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<div>
<input value="{{ $project->title }}" type="text" name="title" id="title" placeholder="Project title">
</div>
<div>
<textarea name="description" placeholder="Enter the project description">{{ $project->description }}</textarea>
</div>
<div>
<button type="submit">Update project</button>
</div>
</form>
#endsection
The controller code:
public function edit($id){
$project= Project::find($id);
return view('projects.edit',compact('project'));
}
public function update($id){
$project= Project::find($id);
$project->title=request('title');
$project->description('description');
$project->save();
return redirect('/projects');
}
The edit form displays as expected with data coming from the database,after submit i get the following error page:
With the example of the instructor the code has worked perfectly,and before using the PUT either on my form and in my controller i used PATCH like the instructor but always the same result.
Here is the instructor video link: Faking PATCH and DELETE Requests
The error is pretty straightforward.
public function update($id){
$project= Project::find($id);
$project->title=request('title');
$project->description('description'); // You don't have a method description()
$project->save();
return redirect('/projects');
}
This is what you probably meant to do:
public function update($id)
{
$project= Project::find($id);
$project->title = request('title');
$project->description = request('description');
$project->save();
return redirect('/projects');
}
What is the correct way of adding a custom method to a resource controller in Laravel 5.6?
What I have so far is a new method in my ProfileController:
public function approve($id){
$user = User::find($id);
$user->state = '1';
$user->save();
return redirect('/dashboard')->with('success', 'User approved.');
}
As well as the following lines added to my web.php file:
Route::post('/profile/{$id}/approve', 'ProfileController#approve');
Route::resource('profile', 'ProfileController');
The form in my view is (afaik) correctly rendered to:
<form method="POST" action="http://myurl.com/profile/10/approve" accept-charset="UTF-8">
<input name="_token" type="hidden" value="v3F1RRhi7iJL2o4egOhcRiuahaGQBwkGkfMal1lh">
<input name="_method" type="hidden" value="PATCH">
<input class="btn btn-success" type="submit" value="Approve User">
</form>
Unfortunately nothing happens, except the "Sorry, the page you are looking for could not be found." page to be shown.
What am I missing? And to expand a bit on this question also, is this even a valid way to implement "single field updates" on a db entry?
Thank you for your help!
i see you have two problems:
firstly correct the route like that
Route::post('/profile/{id}/approve', 'ProfileController#approve');
secondly you have to delete
<input name="_method" type="hidden" value="PATCH">
or replace your route like that:
Route::patch('/profile/{id}/approve', 'ProfileController#approve');
You would want to remove the $ sign from your route:
Route::post('/profile/{id}/approve', 'ProfileController#approve');
The rest of it is correct.
You have written the parameter like var: $id, and you may write it without '$'.
But really you can use the Laravel implicit model binding function to do this:
Route::post('/profile/{user}/approve', 'ProfileController#approve');
And then in your controller:
public function approve(User $user){
// Delete this line--> $user = User::find($id);
$user->state = '1';
$user->save();
return redirect('/dashboard')->with('success', 'User approved.');
}
I am using laravel 5.2. I want to print the database content that is stored in my database dynamically on the desired page. I tried but an error appears everytime i.e;( undefined variable:). I just want to print whatever content I store in my database table dynamically.
My code is here:
My model name is:gallery
My routes:
Route::get('/gallery/list' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/gallery/save' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);
My controller:
public function viewgallerylist()
{
$galleries = gallery::all();
return view('gallery')->with('galleries', $galleries);
}
public function savegallery(Request $request)
{
$gallery1=$request['gallery_name'];
$gallery=new gallery();
$gallery->name=$gallery1;
$gallery->save();
return redirect()->route('viewgallery');
}
My desired page:
<form method="post" action="{{ route('savegallery') }}">
<input class="form-control" type="text" name="gallery_name">
<button type="submit" class="btn btn-primary" id="upl">Create+</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
#foreach($galleries as $gallery)
<p>{{ $gallery->name }}</p>
#endforeach
Most model Classes will have a capital letter. Are you sure your Model isn't called Gallery instead of gallery?
which means that you need to call Gallery::all() in your controller and make sure use App\Gallery; is at the top of your page.
public function viewgallerylist()
{
$galleries = Gallery::all();
return view('gallery')->with('galleries', $galleries);
}
The problem might be with your model.... please provide a larger view
Route::get('/' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);