Support the ongoing development of Laravel.io →
posted 8 years ago
Database
Last updated 1 year ago.
0

Are you trying to store the image itself in your database or the path to the image? If you are trying to store the image itself in the database, I recommend using base64_encode and base64_decode to encode/code your image. You should also access your image file using the file method, not the input method, like this:

$pic = $request->file('pic');
0

The image should be stored in the database not the path. I tried it with base64 encode / decode but it didnt work. The base64_encode / serialize method works but not the base64_decode / unserialize. Is binary the same than blob?

Migrations

 public function up()
    {
           Schema::create('entriesdistance', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('kunde_id');
			$table->double('distanz');
            $table->dateTime('datum');
			$table->double('kalorien');
			$table->string('dauer');
			$table->double('geschwindigkeit');
		    $table->integer('locationpoint_id');
			$table->binary('pic');                     
			$table->rememberToken();
            $table->timestamps();
        });
    }

Receive controller

  public function store(Request $request)
    {
        $kundenid = $request->input('idkunde');
		$distanz = $request->input('distanz');
		$datum = $request->input('datum');
		$kalorien = $request->input('kalorien');
		$dauer = $request->input('dauer');
		$geschwindigkeit = $request->input('geschwindigkeit');
		$locationpoint_id = $request->input('idlocation');
		$pic = $request->input('pic');      <<<<< "pic" is a bitmap converts to byte[]  
			
		$entryDistance = new Entry_distance;
		$entryDistance->kunde_id = $kundenid;
		$entryDistance->distanz = $distanz;
		$entryDistance->datum = $datum;
		$entryDistance->kalorien  = $kalorien;
		$entryDistance->dauer  = $dauer;
		$entryDistance->geschwindigkeit  = $geschwindigkeit;
		$entryDistance->locationpoint_id  = $locationpoint_id;
		$entryDistance->pic  = base64_encode(serialize($pic));
		$entryDistance->save();
		
	$test = 'entryDistance gespeichert';
		return response()->json($test);
}

Send controller

  public function show($id)
    {
         
	
	  $results = DB::select('SELECT * FROM entriesdistance WHERE entriesdistance.kunde_id = ?', [$id]); 
	
		$test = unserialize(base64_decode($results[0]->pic));
	

		    return response()->json(test);    
    }

Output: nothing

Send controller

  public function show($id)
    {
         
	  $results = DB::select('SELECT * FROM entriesdistance WHERE entriesdistance.kunde_id = ?', [$id]); 
	
		    return response()->json($results[0]->pic); 

     }

Output: YToxOTA3NTg6e2k6MDtpOi0xM.........

Last updated 8 years ago.
0

Laravel's binary method creates a BLOB column. But anyway, here's how you can do it.

First, like I said in the previous post, you should access your image file using the file method, not the input method.

$pic = $request->file('pic');

Next, you need to get the contents of that file.

$picContent = file_get_contents($pic);

Note: You can also use the File facade, but anyway, that's not important. Lets move on.

When you store it, you can then use base64_encode. The serialize method is not necessary.

$entryDistance->pic  = base64_encode($picContent);

When you retrieve it, you can use base64_decode, and not unserialize.

$test = base64_decode($results[0]->pic);
Last updated 8 years ago.
0

Thanks for your help. I tried it but it doesnt work. Here is my code:

Receive controller

 public function store(Request $request)
    {
    	
		$picContent = file_get_contents($request);
        $kundenid = $request->input('idkunde');
		$distanz = $request->input('distanz');
		$datum = $request->input('datum');
		$kalorien = $request->input('kalorien');
		$dauer = $request->input('dauer');
		$geschwindigkeit = $request->input('geschwindigkeit');
		$locationpoint_id = $request->input('idlocation');
		$pic = $request->input('pic');
			
		$entryDistance = new Entry_distance;
		$entryDistance->kunde_id = $kundenid;
		$entryDistance->distanz = $distanz;
		$entryDistance->datum = $datum;
		$entryDistance->kalorien  = $kalorien;
		$entryDistance->dauer  = $dauer;
		$entryDistance->geschwindigkeit  = $geschwindigkeit;
		$entryDistance->locationpoint_id  = $locationpoint_id;
		$picContent = file_get_contents($pic);
		$entryDistance->pic  = base64_encode($picContent);
		$entryDistance->save();
		
	$test = 'entryDistance gespeichert';
		return response()->json($test);
    }

My client (Andorid App) gets no answer with these code changes.

Here are more information. I hope it could help :)

Object that the client will send to the server

public class EintraegeDistanz {

    private long ideintraegedistanz;
    private long idkunde;
    private double distanz;
    private String datum;
    private double kalorien;
    private String dauer;
    private double geschwindigkeit;
    private int idlocation;
    private byte[] pic;


    public EintraegeDistanz(long ideintraegedistanz, long idkunde, double distanz, String datum, double kalorien, String dauer, double geschwindigkeit, int idlocation, byte[] pic){
        this.ideintraegedistanz=ideintraegedistanz;
        this.idkunde=idkunde;
        this.distanz=distanz;
        this.datum=datum;
        this.kalorien=kalorien;
        this.dauer=dauer;
        this.geschwindigkeit=geschwindigkeit;
        this.idlocation=idlocation;
        this.pic=pic;
    }
.
.
.
.

The json object from the client (Android App) send via POST looks like the following:

{"datum":"08 Okt. 2015 13:08:17","dauer":"00:00:02","pic":[-119,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0.....]....}

I hope this is not confusing....

0

I don't know much about Android so I can't help you there, but change this line:

// Old line
$pic = $request->input('pic');
// New line
$pic = $request->file('pic');
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.