getClientOriginalName() on null - laravel

I have a little problem with one request file in my Store controller !
I get the orginal file name extension like this :
$licencie_amateur->cert_medical_surclassement = $request->file('cert_medical_surclassement')->getClientOriginalName();
Sometimes the user don't need to put a file "cert_medical_surclassement" i would like to do a condition like : If the file was added ok but if there is no files in the field i pass to the other variable .
Someone know how i could do this ? thanks a lot in advance

Use the hasFile() method
if ($request->hasFile('cert_medical_surclassement')) {
$licencie_amateur->cert_medical_surclassement = $request->file('cert_medical_surclassement')->getClientOriginalName();
} else {
// else code
}
More info: Documentation

Related

Put default value in url

Hi I am trying to put default id value in my route url in Laravel. How do I achieve it? Please Help me. Thank you.
Route::get('manageattendance/{id}',[App\Http\Controllers\ManageAttendanceController::class, 'index'])-
>name('manageattendance');
replace {id} with {id?} and if you have id as parameter on the index function, set it to the default value like:
public function index($id = 1){
// your code
}

[Laravel][Routing] How to pass param to controller respective with route parameter

I met a problem, although i searched many places but could not find the answer. So, i ask here for a support.
I want to pass param into controller function that respective with param in route.
Please see below example:
I want these routes to Check controller , function getByTime($time)
Route::get('/hourly', 'Check#getByTime');
Route::get('/daily', 'Check#getByTime');
Route::get('/weekly', 'Check#getByTime');
class Check {
function getByTime($time) {}
}
Example conditions:
hourly: $time=1
daily: $time=24
weekly: $time = 24*7
I mean like this:
Route::get('/hourly', 'Check#getByTime(1)');
Route::get('/daily', 'Check#getByTime(24)');
Route::get('/weekly', 'Check#getByTime(168)');
Please help me to resolve it.
Sorry for my bad explanation, and thanks for your help.
Not sure if I have understood your exact problem, but this will do a trick
Route::get('{slug}', function ($slug){
if ($slug == "hourly") {
App::call('App\Http\Controllers\Check#getByTime', [1]);
} else if ($slug == "daily") {
App::call('App\Http\Controllers\Check#getByTime', [24]);
} else if ($slug == "weekly") {
App::call('App\Http\Controllers\Check#getByTime', [168]);
}
});
But you have add this at top of all routes.
This is so simple,If you check laravel document you will find solution easily.here is the link.
By the way you can do it simply like below
Route::get('/hourly/{hour}', 'Check#getByTime');
Route::get('/daily/{day}', 'Check#getByTime');
Route::get('/weekly/{week}', 'Check#getByTime');
class Check {
function getByTime($time) {}
}
and pass parameter in url like
http://yourhost/hourly/1
http://yourhost/daily/24
http://yourhost/weekly/168
As per your requirement you could try this:
Route::get('/hourly', 'Check#getByTime')->defaults('hour', 1);

Can't get Another field from session

I got problem about session data, in my web, i want to make a different view based on previledge user, here my code in controller :
$previledge = $this->session->userdata['previledge'];
$data['previledge']=$previledge;
if($data['previledge'] == 'admin'){
$this->load->view("app_admin/global/header",$d);
$this->load->view("app_admin/materi/home");
$this->load->view("app_admin/global/footer");
}
else {
$this->load->view("app_admin/global/header2",$d);
$this->load->view("app_admin/materi/home");
$this->load->view("app_admin/global/footer");
}
}
if i run my code above, previledge with admin can't go to the right view.
if there any suggestion, please inform,
thanks
Its something like syntax error.
$previledge = $this->session->userdata('previledge');
This will help you.
Below code must have to work
$this->session->userdata('previledge')=='admin'

Laravel s3 check if directory exists.

How would I check with Laravel storage if a directory exists?
In the documentation there is only documented how to check if a file exists.
Storage::disk('local')->exists('file.jpg');
https://laravel.com/docs/5.3/filesystem#retrieving-files
None of the answers here seems to answer specifically to check if a particular folder exists in S3. This is what I use and it works:
Storage::disk('s3')->exists('FOLDER_NAME')
The above will return a boolean. The above check works for both folders and filenames.
In Laravel 5.7 use:
The has() method is used to determine if a given file exists on the disk:
$exists = Storage::disk('s3')->has('file.jpg');
Try to get directories list in a parent directory and check if the directory you're looking for is in an array:
in_array('directoryYoureLookingFor', Storage::disk('s3')->directories('parentDirectory'));
I've just checked and this solution works in Laravel 5.3.
You can using isDirectory method:
if (File::isDirectory($filename))
{
echo "Yes. It's a directory.";
}
I have also faced same issue many time for check file is exists or not on S3 with Laravel. I am working on Laravel 7.* and exists() and has() functions not providing proper response when file is uploaded on sub directory of S3 bucket. I have write a function which is working perfectly for me.
public static function fileExistsS3($directoryOnS3, $image) {
$directoryOnS3 = Config::get('custom.AWS_PUBLIC_ENDPOINT').$directoryOnS3;
$exists = #getimagesize($directoryOnS3.$image);
if(isset($exists) && is_array($exists)) {
return true;
} else {
return false;
}
}
It will return true if file available on S3 else it will return false.
You can do the following thing i.e u can get the filename and compare it with the file name urtrying to upload if it exists give a maessage else u can insert
$file = Storage::disk('local')->get($filename);
if($file){
echo "file exist";
} else {
Storage::disk('local')->put($filename,FILE::get($file));
}

accessing lang variables from controller + codeigniter

How can I access my lang variables in controller?
function index()
{
$seo_title = $this->lang->line('blablabla');
$data['page_title'] = $seo_title;
$this->load->view('contact/index.php',$data);
}
in my lang file blablabla has string in, and it works well when I call from view file. but when I want to call from controller, it doesnt return anything :(
any idea about problem? appreciate!!
You need to load the language file you want to use before you grab lines from it:
$this->lang->load('filename', 'language');

Resources