radiosabines's avatar

Wrong image path in my DB

In my app , when i create a new post in the form we have two input file to upload 2 images, they goes to a public folder call "rutas" after create the post, i can see the images in that folder, but the path saved in the db is /private/var/folders/38/dctw6dxj5d35c91z8hzz6csw0000gn/T/php9N8FnJ why that happens ? how to solve it

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Ruta extends Model
{
    use HasFactory;

    protected $guarded = ['id', 'created_at', 'updated_at'];

    public function getRouteKeyName()
    {
        return 'slug';
    }
}
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;

class StoreRutaRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Gate::allows('ruta_access');
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'legNumber'      => 'required',
            'name'           => 'required',
            'slug'           => 'required|unique:rutas',
            'imageDep'       => 'required|image|mimes: jpeg,png,jpg,gif|max:2048',
            'icaoDep'        => 'required',
            'icaoDepContent' => 'required',
            'icaoDes'        => 'required',
            'icaoDesContent' => 'required',
            'imageDes'       => 'required|image|mimes: jpeg,png,jpg,gif|max:2048',
            'descriptionLeg' => 'required',
            'rutaIfr'        => 'required',
            'departures'     => 'required',
            'arrivals'       => 'required',
            'approachs'      => 'required',
        ];
    }
}
public function store(StoreRutaRequest $request)
{
   
  
    if ($image =$request->file('imageDep')) {
        $destPath = 'rutas/';
        $profileImage = "des" . date('YmdHis') . "." . $image->getClientOriginalExtension();
        $image->move($destPath, $profileImage);
        $ruta['imageDep'] = $profileImage;
    }

    if ($image2 =$request->file('imageDes')) {
        $destPath2 = 'rutas/';
        $profileImage2 = "dep" . date('YmdHis') . "." . $image2->getClientOriginalExtension();
        $image2->move($destPath2, $profileImage2);
        $ruta['imageDes'] = $profileImage2;
    }


    $ruta = Ruta::create($request->validated());

    return redirect()->route('admin.rutas.index')->with('info', 'La ruta ha sido creada con éxito');
}
0 likes
2 replies
jlrdw's avatar

Just store the image name, resolve path with the asset helper.

Just example:

<img src="{{ asset('upload/imgdogs') . '/' . $row->dogpic }}" alt="" class="image"></a></td>
    

Note if private image serve through a route and controller and use proper authorization.

1 like
radiosabines's avatar

thx for read! I think I have explained myself wrong in the routes folder I have the image with its name = dep20220527185517.jpeg but in the database in the imagedep field I have: /private/var/folders/38/dctw6dxj5d35c91z8hzz6csw0000gn/T/php9N8FnJ in the view:

<div class="rounded-lg h-64 overflow-hidden">
                                            <img alt="content" class="object-cover object-center h-full w-full"
                                                src="/rutas/{{ $ruta->imageDep }}">
                                        </div>

in the db i must have the same name of the image upload to the public folder 'rutas'....

Please or to participate in this conversation.