How to select specfic id and update in Laravel?

663 Views Asked by At

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'];
}
3

There are 3 best solutions below

1
STA On BEST ANSWER

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');
3
dhnwebpro On

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.

1
Ahmed Amir On
public function edit(Request $request)
{
    $id = request('id');
    $images = ImageGallery::where('id',$id)->first();

    return view('edit',compact('images'));
}