1

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'];
}
greeniron
  • 131
  • 11

3 Answers3

1

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');
STA
  • 30,729
  • 8
  • 45
  • 59
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/218108/discussion-on-answer-by-sta-how-to-select-specfic-id-and-update-in-laravel). – Samuel Liew Jul 18 '20 at 17:20
0

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.

dhnwebpro
  • 134
  • 4
  • Thank you very much for helping me. I used your code then now I got this error Trying to access array offset on value of type bool (View:/edit.blade.php) . I update my blade file. Could you teach me what is wrong my code? – greeniron Jul 18 '20 at 06:23
  • Before you get to `@if($images->count())` can you place the following: `{{ dd($images->get(0)) }}` and past the result of that value? It could be that `$image` is boolean, but I'm wondering if there's another problem in your view beyond that. – dhnwebpro Jul 18 '20 at 06:45
  • Dear @dhnwebpro Thank you again. I did $dd. message is here SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: select `0` from `image_gallery`) – greeniron Jul 18 '20 at 10:36
0
public function edit(Request $request)
{
    $id = request('id');
    $images = ImageGallery::where('id',$id)->first();

    return view('edit',compact('images'));
}
Ahmed Amir
  • 124
  • 6