I am uploading image to the server using Dropzone.js.
I want to associate the uploaded image to a user .
So basically this is a one-to-one realtionship between a user and his profile image.
How can I associate the image with a user?
I have also 2 tables : users and profile_images .I have also created a pivot table with 2 columns : user,image_path .
Right now this is the code I have on the view to upload the images (including dropzone.js code).
view:
<div class="container">
<!-- <div class="dropzone" id="dropzoneFileUpload">
</div> -->
<form id="dropzoneFileUpload" method="post" action="{{action('DropzoneController@uploadFiles')}}" class="dropzone">
<div class="fallback">
<input name="file" type="file" />
<!-- <input type="hidden" name="_token" value="{{Session::getToken()}}"> -->
</div>
</form>
</div>
<script type="text/javascript">
Dropzone.options.dropzoneFileUpload = {
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
maxFiles: 1,
addRemoveLinks: true,
init: function(){
this.on("success", function(file,resp){
if(resp=="success"){
alert("Image uploaded successfully");
}
else {
alert("Faild to upload image! Try again");
}
});
}
};
</script>
Controller:
public function index()
{
return view('dropzone_demo');
}
public function uploadFiles()
{
$message="Profile Image Created";
$input = Input::all();
$rules = array(
'file' => 'image|max:3000',
);
$validation = Validator::make($input, $rules);
if ($validation->fails()) {
return Response::make($validation->errors->first(), 400);
}
$destinationPath = 'uploads'; // upload path: public/uploads
$extension = Input::file('file')->getClientOriginalExtension(); // getting file extension
$fileName = rand(11111, 99999) . '.' . $extension; // renameing image
$upload_success = Input::file('file')->move($destinationPath, $fileName); // uploading file to given path
if ($upload_success) {
return Response::json('success', 200);
} else {
return Response::json('error', 400);
}
}
EDIT:
I am trying to insert the uploaded image path to the profile_images table. This is still experimental code, and I have to know yet to associate the image with the authenticated user:
public function __construct()
{
$user=Auth::user();
}
public function associate()
{
if(!isset($this->user->id->profile_image))
{
echo "no image profile has been uploaded";
}
else
{
// I need to attach the uploaded image to the authenticated user.
$profile_image = new AppProfileImage; // Creating a new instance of the model.
$profile_image->profile_images = $user->id;
$profile_image->profile_image=
}
}
Question is : how do I process the image that I have uploaded to be set as a profile image of the user?
Would appreciate your suggestions, thanks.