laravellaravel-routinglaravel-route

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

Solution

  • 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');