Not updating database and not throwing error in laravel - laravel

I have an issue while updating the user. When I try to update the user after clicking the save button then it redirect me to the same page and not throwing me any error but also its not updating anything in the database. Below is my code. I have no idea what's going on here. Help me :)
Controller
public function update(ReportRequest $request, $id)
{
$report = Report::findOrFail($id);
$input = $request->all();
if ($file = $request->file('photo_id')) {
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file' => $name]);
$input['photo_id'] = $photo->id;
}
$report->update($input);
return redirect()->back();
}
Route
Route::resource('admin/reports', 'ReportController', ['names'=>[
'index'=>'admin.reports.index',
'create'=>'admin.reports.create',
'edit'=>'admin.reports.edit',
]]);
Models
class Report extends Model
{
protected $fillable = [
'student_id',
'student_name',
'class_id',
'subject',
'teacher_name',
'report_categories_id',
'total_marks',
'obtained_marks',
'percentage',
'position',
'photo_id',
];
public function photo() {
return $this->belongsTo('App\Photo');
}
public function studentsClass() {
return $this->belongsTo('App\StudentsClass', 'class_id');
}
public function student() {
return $this->belongsToMany('App\Student');
}
}

Make sure you have your $fillable properties in your Photo and Report models, otherwise the create() and update() methods won't work as expected.

Check the $fillable fields in the Model as above. If the error persists check your laravel log on storage/logs/laravel.log.

In controller:
public function update(ReportRequest $request, $id){
$report = Report::findOrFail($id);
$input = $request->all();
try{
if ($request->photo_id != '') {
$path = 'images/';
$file = $request->photo_id;
$name = time() . $file->getClientOriginalName();
$file->move($path, $name);
$photo = Photo::create(['file' => $name]);
$report->update(['photo_id' => $photo->id]);
}
return redirect()->back();
}catch(\Exception $e){
return redirect()->back()->with('error_message', $e->getMessage());
}
}

Related

How to select specfic id and update in Laravel?

I'm studing Laravel CRUD.
Laravel Framework is 6.18.15
I would like to select of a record and update.
This is photo gallery.
Now if I click one of photo I can get below URL
https://mywebsite.net/public/edit?id=59
but in edit.blade.php I got this error
Undefined variable: id
Could someone teach me correct code please?
These are current code
Controller UPDATED
public function edit(Request $request)
{
$images = ImageGallery::find($id);
return view('edit',compact('images'));
}
public function editpcs(Request $request)
{
$this->validate($request, [
'title' => 'required',
'image' => 'required|mimes:jpeg,jpg'
]);
$input['image'] = time().'.'.$request->image->getClientOriginalExtension();
if($request->hasFile('image')) {
$image = $request->file('image');
$filename = time().'.'.$request->image->getClientOriginalExtension();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(1280, null, function ($image) {$image->aspectRatio();});
$image_resize->save(public_path('images/ServiceImages/' .$filename));
}
$request->image->move(public_path('images'), $input['image']);
$input['title'] = $request->title;
// ImageGallery::update($input);
$update = DB::table('image_gallery')->where('id', $id)->update( [ 'title' => $request->title, 'image' => $request->image]);
return view('edit',compact('images'))->with('sucess','sucessfully updated');
}
web.php
//edit view
Route::get('edit', 'ImageGalleryController#edit');
Route::post('edit', 'ImageGalleryController#edit');
//edit procces
Route::get('editpcs', 'ImageGalleryController#editpcs');
Route::post('editpcs', 'ImageGalleryController#editpcs');
UPDATE
#if($images->count())
#foreach($images as $image)
<div class='text-center'>
<small class='text-muted'>{{$image['id']}}/ {{$image['title']}} </small>
</div>
#endforeach
#endif
MODEL
namespace App;
use Illuminate\Database\Eloquent\Model;
class ImageGallery extends Model
{
protected $table = 'image_gallery';
protected $fillable = ['title', 'image'];
}
Actually $id is really undefined here, it would be $request->route('id') or request('id') or $_GET['id'] or $request->input('id') :
public function edit(Request $request)
{
$id = request('id');
$images = ImageGallery::findOrFail($id); // use findOrFail() id not exist in table, it throw a 404 error
return view('edit',compact('images'));
}
Take a look at the $_GET and $_REQUEST superglobals. Something like the following would work for your example:
$id = $_GET['id'];
$country = $_GET['country'];
In laravel you can to use Input::get(), But Input::get is deprecated in newer version of laravel, prefer the $request->input instead of Input::get :
$id= $request->input('id');
$country= $request->input('country');
It looks to me like this function:
public function edit(Request $request)
{
$images = ImageGallery::find($id);
return view('edit',compact('images'));
}
Should be something like this perhaps?
public function edit(Request $request)
{
$id = $request->input('id', null);
$images = ImageGallery::find($id);
return view('edit',compact('images'));
}
As it is, $id appears to be undefined before you attempt to pass it into the find() method. But according to your URL it is in the $request object. So you need to get it from there and into the function. You can read about this method in the docs.
public function edit(Request $request)
{
$id = request('id');
$images = ImageGallery::where('id',$id)->first();
return view('edit',compact('images'));
}

How do i update image in the repository by upload?

I'm setting up a CRUD, similar to an ecommerce
I uploaded the image, it is stored in the public folder. The name of the image is replaced by the value of an input that the user places. This same value is saved in the database with the image extension.
I was also able to delete the image, but I don't know how to update it. Can someone please give me a light?
project controller:
public function store(Request $request)
{
$nameFile = $request->input('imageName', '');
if($request->file('imageFile')->isValid()){
$nameFile .= '.' . $request->file('imageFile')->getClientOriginalExtension();
$request->file('imageFile')->storeAs('projects', $nameFile);
}
$project = new Project();
$project->name = $request->input('name');
$project->price = $request->input('price');
$project->imageName = $nameFile;
$project->save();
return redirect()->route('ProjectControllerCreate');
}
public function destroy($id)
{
$project = Project::find($id);
// Image
$filePathName = public_path().'/storage/projects/'. $project->imageName;
if( file_exists($filePathName) ){
unlink($filePathName);
}
// Data
if(!$project)
return redirect()->back();
$project->delete();
return redirect()->route('ProjectControllerCreate');
}
public function update(Request $request, $id)
{
$project = Project::find($id);
if(!$project)
return redirect()->back();
$project->update($request->all());
return redirect()->route('ProjectControllerCreate');
}
This is how you can update :
use Illuminate\Support\Facades\Storage;
public function update(Request $request, $id)
{
$project = Project::find($id);
if(!$project)
return redirect()->back();
// Update new image
if($request->file('imageFile')->isValid()){
// Delete old image
$old_image = $project->imageName;
Storage::delete($old_image);
$nameFile = $request->input('imageName', '');
$nameFile .= '.' . $request->file('imageFile')->getClientOriginalExtension();
$request->file('imageFile')->storeAs('projects', $nameFile);
$project->imageName = $nameFile;
}
$project->name = $request->input('name');
$project->price = $request->input('price');
$project->save();
return redirect()->route('ProjectControllerCreate');
}

get point after submit Story

how to add points after submitting a post ?
This is my Controller :
public function store(PostRequest $request)
{
$story = Auth::user()->post()->create($request->all());
$image = $request->File('image');
if ($image) {
$image_name = time().'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('upload');
$image->move($destinationPath, $image_name);
$story->image = $image_name;
}
$story->update();
if ($story) {
flash()->success('Your Story Has Been successfully added');
}
return redirect()->back();
}
This is my table users :
protected $fillable = [
'name', 'email', 'password', 'point'
];
This is my table post story :
protected $fillable = [
'user_id','title','description','image'
];
It is up to you to implement the logic of when you increment the point field but here is how you can.
$story->increment('point');
Or
$story->point++;
And of course $story->save();

laravel 5 Call to undefined method Illuminate\Database\Query\Builder::posts()

i want to store this information but that doesn't work i still have this error "Call to undefined method Illuminate\Database\Query\Builder::posts()
"
public function store(Request $request)
{
$this->validate($request,[
'title'=>'required',
'slug'=>'required',
'excerpt'=>'required',
'body'=>'required',
'created_at'=>'date_format:Y-m-d H:i:s',
'team_id'=>'required',
'image'=>'mimes:jpg,jpeg,png,bmp',
]);
$data = $this->handleRequest($request);
$request->user()->posts()->create($data);
return redirect('/backend/blog')->with('message', 'Your post was created successfully!');
}
private function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image'))
{
$image = $request->file('image');
$fileName = $image->getClientOriginalName();
$destination = $this->uploadPath;
$image->move($destination, $fileName);
$data['image'] = $fileName;
}
return $data;
}

Laravel - update one to one field

I have an update post form, where I need to update the name of the post in posts table and the associated text within the text table. I can't seem to get it to work at all.
Model - Post.php
public function text()
{
return $this->hasOne('Text');
}
Model - Text.php
public function post()
{
return $this->belongsTo('Post');
}
Controller - PostController.php
public function updateQuestionForm($id)
{
$post = Post::find($id);
$input = Input::all();
$rules = array(
'text' => 'required',
);
$validation = Validator::make($input, $rules);
if ($validation->fails()) {
return Redirect::back()->withErrors($validation)->withInput();
} else {
$post->title = Input::get('title');
$post->save();
$text = $post->text();
$text->text = Input::get('text');
$post->text()->save($text);
$message = "Post updated";
return Redirect::to('question/'.$post->id.'/'.$post->slug.'/')->with('message', $message);
}
}

Resources