Home > Mobile >  Laravel 9 CRUD photo sent to temp file
Laravel 9 CRUD photo sent to temp file

Time:09-10

In my Laravel 9 CRUD, when I update or create date and when I enter a photo, it's sent to php temp file although I've specified the path destination, it works fine with normal forms, but when it comes to CRUD, it doesn't, why?

My functions:

public function store(Request $request)
   {
       $request->validate([
           'photo' => 'required',
           'parts' => 'required', 
           'writer_name' => 'required', 
           'title' => 'required', 
           'field' => 'required', 
           'photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
       ]);

       $input = $request->all();

       if ($photo = $request->file('photo')) {
           $destinationPath = 'uploads/books';
           $profileImage = date('YmdHis') . "." . $photo->getClientOriginalExtension();
           $photo->move($destinationPath, $profileImage);
           $input['photo'] = $profileImage;
       }
     
       Book::create($input);
      
       return redirect()->route('books.index')
                       ->with('success','Book inserted successfully.');
   }

My index:

        @foreach ($books as $book)
        <tr>
            <td><img src="uploads/books/{{ $book->photo }}" width="100px"></td>
            <td>{{ $book->copies }}</td>
            <td>{{ $book->note }}</td>
            <td>{{ $book->parts }}</td>
            <td>{{ $book->publication }}</td>
            <td>{{ $book->documentation }}</td>
            <td>{{ $book->review }}</td>
            <td>{{ $book->writer_name }}</td>
            <td>{{ $book->title }}</td>
            <td>{{ $book->field }}</td>
            <td>{{ $book->created_at }}</td>
            <td>{{   $i }}</td>

My model:

class Book extends Model
{
    use HasFactory;
    protected $fillable = [
        'photo', 'copies', 'note', 'parts', 'publication', 'documentation', 'review', 'writer_name', 'title', 'field', 'created_at'
    ];
}

CodePudding user response:

Here is the answer:

if(!empty($request->hasFile('photo'))){
     $image = $request->file('photo');
     $photoname = date('YmdHis').'.'.$image->extension();
     $filePath = public_path('/uploads/books/');
     $image->move($filePath, $photoname);
     $input['photo'] = $photoname;
            
     }

CodePudding user response:

You can do that in this way into your controller, it worked for me. Also why you are validating photo two times make it in one 'photo' => 'required|image|mimes:jpg,jpeg,png|max:2048'

My previous answer

if(!empty($request->hasFile('photo'))){
$image = $request->file('photo');
$input['imagename'] = date('YmdHis').'.'.$image->extension();
$filePath = public_path('/uploads/books/');
$image->move($filePath, $input['imagename']);
$input['photo'] = $input['imagename'];
    
}
$name = $request->file('image')->getClientOriginalName();
 
$path = $request->file('image')->store('public/images');
 
 
$save = new Photo;
 
$save->name = $name;
$save->path = $path;
 
$save->save();
This line of code will upload an image into the images directory:
$path = $request->file('image')->store('public/images');

  • Related