In Laravel im using this code in my controller to get files from directory:
public function galleryImages($album) {
$images = File::allFiles('gallery/'.$album);
return View::make('galleryimages', ['images' => $images]);
}
and in my 'galleryimages' view is:
#foreach($images as $image)
<img src="???"> <-- how to get file url here?
#endforeach
Now how can I get pathName of $image variable? Var_dump of $image is returning:
object(Symfony\Component\Finder\SplFileInfo)#247 (4) {
["relativePath":"Symfony\Component\Finder\SplFileInfo":private]=> string(0) ""
["relativePathname":"Symfony\Component\Finder\SplFileInfo":private]=> string(11) "garden5.jpg"
["pathName":"SplFileInfo":private]=> string(25) "gallery/Album/garden5.jpg"
["fileName":"SplFileInfo":private]=> string(11) "garden5.jpg" }
I was trying $image->pathName, but it doesn't work.
You have to use the getter method
$image->getPathname()
The Symfony class extends from SplFileInfo. You can find the reference to all its methods on php.net
Related
I am developing a web application in laravel 5.3. The problem i am getting is that attributes doesn't add up in stdClass object, however you can see my code where i am explicitly setting/adding a new attributes in a object.
problematic code
1- $sender = Sender::where('id', $this->senderId)->first();
2- if ($sender != null) {
3- var_dump($sender->bundle);
4- $sender->bundle->latitude = $this->lat;
5- $sender->bundle->longitude = $this->long;
6- $sender->bundle->location = $response->formattedAddress();
7- var_dump($sender->bundle);
8- $sender->save();
9- error_log('resolved');
10- } else {
11- error_log('no sender');
12- }
In above code you can clearly see that i am setting some new properties for bundle object.
$sender->bundle->latitude = $this->lat;
$sender->bundle->longitude = $this->long;
$sender->bundle->location = $response->formattedAddress();
but in output you can clearly see that it doesn't get add up. The code basically runs in laravel queue. In above code $sender is laravel eloquent model and the attribute bundle is being json_decode via laravel mutators.
mutator code
public function setBundleAttribute($value) {
$this->attributes['bundle'] = json_encode($value);
}
public function getBundleAttribute($value) {
return json_decode($value);
}
output of above problematic code
object(stdClass)#686 (3) { #output of line 3
["city"]=>
string(0) ""
["province"]=>
string(0) ""
["country"]=>
string(0) ""
}
object(stdClass)#686 (3) { #output of line 7
["city"]=>
string(0) ""
["province"]=>
string(0) ""
["country"]=>
string(0) ""
}
[2017-07-25 16:11:03] Processed: App\Jobs\LocationResolverQueue
resolved
Problem Solved
laravel Accessors & Mutators cause the problem.
Accessor and Mutator get called whenever we get or set property respectively, in my case the mutator was calling whenever i try to access $sender->bundle->property however accessor didn't get called because i was setting a $sender->bundle->property rather than $sender->bundle
i solved problem like
$sender = Sender::where('id', $this->senderId)->first();
if ($sender != null) {
$temp = $sender->bundle;
$temp->latitude = $this->lat;
$temp->longitude = $this->long;
$temp->location = $response->formattedAddress();
$sender->bundle = $temp;
$sender->save();
}
How can I use Laravel file validation to a multiple file inside a loop?
Usually in a single file validation, I can just do like
$validator = Validator::make($request->all(),[
'image' => 'image|max_width:1000|max_height:1000'
]);
if($validator->fails()){
//validation fails
}
but now I want to validate multiple files in a single process, like
//store to a variable
$file = $request->file('image');
if($file){ //if request file image exist
foreach($request->file('image') as $i){
//here, validate each file image, but it just I don't know how
}
}
Now for the front end side, to elaborate about the process of sending multiple files in one post.
$(document).on("change",'input[type="file"]',function(){
if($(this).val()!==""){
var files = this.files;
handleFileUpload(files);
$(this).val("");
}
});
function handleFileUpload(files)
{
//create a formData
var fd = new FormData();
//loop through e
for (var i = 0; i < files.length; i++)
{
//append each file object to the formData
fd.append('image[]', files[i]);
}
//this function is where ajax request will be process
sendFileToServer(fd);
}
and so in the back end if I dump the request, I get these
array(2) { [0]=> object(Illuminate\Http\UploadedFile)#214 (7) {
["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
bool(false)
["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(50) "12400457_643171672489204_2452820305362127031_n.jpg"
["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(10) "image/jpeg"
["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(18413)
["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(0) ["pathName":"SplFileInfo":private]=> string(25)
"C:\wamp64\tmp\phpBAE4.tmp" ["fileName":"SplFileInfo":private]=>
string(11) "phpBAE4.tmp" } [1]=>
object(Illuminate\Http\UploadedFile)#217 (7) {
["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
bool(false)
["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(39) "13296200_702501119889592_55691535_n.jpg"
["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(10) "image/jpeg"
["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(95383)
["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(0) ["pathName":"SplFileInfo":private]=> string(25)
"C:\wamp64\tmp\phpBAE5.tmp" ["fileName":"SplFileInfo":private]=>
string(11) "phpBAE5.tmp" } }
any ideas, clues, help please on how to use Laravel file validation on a multiple files inside a loop?
PS: I'm running on Laravel 5.2
Try sending the image file input field as an array and then using array validation. If you're using Laravel 5.2 you can use Array Validation and ditch the foreach loop for building the rules.
Firstly, you would need to set up your file input as an array...
If you're using one input field:
<input type="file" name="image[]" multiple>
Your request would then come through looking something like this:
array:2 [▼
"_token" => "oX3sTXPDnNiVj9mX20VOHe2xo1Ahx6sWzU4VHZxh"
"image" => array:2 [▼
0 => UploadedFile {#147 ▶}
1 => UploadedFile {#146 ▶}
]
]
Then using array validation your rules would be declared in the validator by utilising the asterix symbol to indicate an array field:
$validator = Validator::make($request->all(),[
'image.*' => 'image|max_width:1000|max_height:1000'
]);
Check the docs here for more info: https://laravel.com/docs/5.2/validation#validating-arrays
You can try the following:
foreach($this->input('image') as $key => $value){
$rules['image.'.$key] = 'image|max_width:1000|max_height:1000';
}
been searching for a solution all day long...
my variable name is $categoryFetchedAsObj
var_dump results :
array(104) {
[0]=>
object(stdClass)#411 (1) {
["categoryHive"]=>
string(4) "asfa"
}
[1]=>
object(stdClass)#412 (1) {
["categoryHive"]=>
string(13) "ENERGY DRINKS"
}
[2]=>
object(stdClass)#413 (1) {
["categoryHive"]=>
string(7) "KETCHUP"
}
[3]=>
object(stdClass)#414 (1) {
["categoryHive"]=>
string(6) "STICKS"
}
and so on
i call a function...
createSelectBlock("category",$categoryFetchedAsObj,"onchange=\"this.form.submit()\"",null,'categoryHive');
here is the instance of the function
createSelectBlock($name,$fetchedNamesAsArrayOfObj,$selectEvent,$objPropertyNameForOptionsValue,$objPropertyNameForOptionsName)
and here is where the error raises
foreach ($fetchedNamesAsArrayOfObj as $rowArrayObj) {
if(isset($objPropertyNameForOptionsValue) && isset($objPropertyNameForOptionsName)){
$selectBlock.="<option value=\"$rowArrayObj->$objPropertyNameForOptionsValue\">$rowArrayObj->$objPropertyNameForOptionsName</option>";
}
if(!isset($objPropertyNameForOptionsValue) && isset($objPropertyNameForOptionsName)){
$selectBlock.="<option value=\"$rowArrayObj->$objPropertyNameForOptionsName \">$rowArrayObj->$objPropertyNameForOptionsName</option>";
}
}
(Object of class stdClass could not be converted to string)
if i replace the
$rowArrayObj->$objPropertyNameForOptionsName
with
$rowArrayObj->categoryHive
everything works but the args of the function are not the same all the time .. obviously
i search this site found some suggestions.. but nothing worked
here is what i tryied
$objPropertyNameForOptionsName='$objPropertyNameForOptionsName';
$rowArrayObj->{'$objPropertyNameForOptionsName'};
any ideas?
after testing and over testing .. finlay i got it working...
this was the correct answer for my situation
if(isset($objPropertyNameForOptionsValue) && isset($objPropertyNameForOptionsName)){
$selectBlock.="<option value=".$rowArrayObj->$objPropertyNameForOptionsValue.">".$rowArrayObj->$objPropertyNameForOptionsName."</option>";
}
if(!isset($objPropertyNameForOptionsValue) && isset($objPropertyNameForOptionsName)){
$selectBlock.="<option value=".$rowArrayObj->$objPropertyNameForOptionsName.">".$rowArrayObj->$objPropertyNameForOptionsName."</option>";
}
hope it helped someone
i am trying to get this working: It is ment to return any function or method where the different 'functional parts' are subgrouped. I have problems with the maybe nested {..{.{} } } function body. At the moment the search breaks of after the first following }.
I do have a little bit a hard time understanding lock arround and recursive handling of Regs. So if someone would like to give a brief overall view - would be great!
i understand enough for this..The Regex:
to work with preg_* in PHP with "/im" set
([\w ]+)?(function)+([\s]+[\w\d]+)([\s]*?\(([^\)]+)?\))([\s]?\{+[^\}]*\})
Here a sample of use (i dont have named subpatterns on this sample...):
<?php
$s=file_get_contents('sometestcode');
$p='/
([\w ]+)?(function)+([\s]+[\w\d]+)([\s]*?\(([^\)]+)?\))([\s]?\{+[^\}]*\})
/im';
$m=array();
preg_match_all($p,$s,$m,PREG_SET_ORDER);
var_dump($m);
?>
Here comes the output as expected:
// Here the subject:
public static private function __set($name,$val){
$this->DATA[$name] = $val;
foreach( /*do it*/ ){
//work it...
}
return $this;
}
// The return
array(6){
[1]=>
string(7) "public static private " //makes sence, ha?
[2]=>
string(8) "function"
[3]=>
string(6) " __set" // func/meth name
[4]=>
string(12) "($name,$val)"
[5]=>
string(10) "$name,$val" // (if passed)$argument...
[6]=>
string(60) "{
$this->DATA[$name] = $val;
foreach( /*do it*/ ){
//work it...
}
// Here is the my link
// How to style the regExpPress??
}"
Let's say I have a function which returns an object with one of it's parameters set to some certain value:
public function search($jobsdone, $date)
{
foreach ($jobsdone as $jd) {
if ($jd->date_worked == $date) return $jd;
}
}
Printing search($jobsdone, $key) yields such results:
object(JobDone)#378 (19) {
...
["attributes":protected]=>
array(9) {
["id"]=>
int(3593)
["user_id"]=>
int(13)
["object_id"]=>
int(99)
["job_id"]=>
int(130)
["date_worked"]=>
string(10) "2013-10-01"
["min_from"]=>
int(780)
["min_to"]=>
int(1080)
}
...
}
However, if I want to print out search($jobsdone, $key)->id, all I get is an error message of:
Trying to get property of non-object
What could I be missing here?
Your search function doesn't always return an object. Therefore, you get error Trying to get property of non-object whenever your search couldn't find a $jobdone object.