URL of image not saved into database - laravel

I am trying to add an image to profile in my application. The code returns the correct URL and also the image is stored into the specified folder. But the URL is not saved into the databaseenter image description here

I see two things :
1 - You move your picture with public_path() and you set the $url with URL::to() which is not really logic. Try this way :
$url = public_path() . '/profile_photo/'.$file->getClientOriginalName();
2 - At the end of your if statement, your set an exit() method, which exit from your method. Take it off !
EDIT ----
To save it to your db, do this way :
$user = User::find($id);
$user->profile_photo = $url;
$user->save();

Related

Laravel store dynamic value to the config file or any files

I tried lot of ways but not work. I don't need to store .env file. I need store value permanent and rewrite using api call dynamically.
The "Config::set();" function is not work because of that value temporary.
try this to overwrite a (.php) file, you need to create it first if the file not exist
first make the file e.g test.php in project_folder/config/ and write <?php, save the file
next overwrite value, in this case is config aray with key
$array = [
'config_key' => 'config_value',
'next_key' => 'next_value'
];
$fp = fopen(base_path('config/test.php'), 'w');
fwrite($fp, '<?php return ' . var_export($array, true) . ';');
fclose($fp);
to get the value you just set config('test.config_key');

Laravel Store only filename when uploading a file

I have the following code to upload and store the file.
$user->update([
'display_profile' => request()->display_profile->storeAs('avatars', $name,'public')
]);
This stores the file in the display_profile as avatars/filename.jpg.
Since I have multiple versions of the files for displaying around the views I am using prefixes like follow
thumb_filename.jpg
small_filename.jpg
large_filename.jpg
I will need to do a lot of string replace to insert the prefix in place to show the right version of the images. Is there anyway I can save just the filename in the database insteat of the full path?
If not whats the best way to show my files in the view?
I use this approach mostly:
$file = $request->file('display_profile');
$name = $file->getClientOriginalName() . '.' . $file->extension();
$file->storeAs('public/avatars', $name);
$user->update([
'display_profile' => $name
]);
When you retrieve a file from the request, this will be converted as an UploadedFile object. You can easily retrieve the original name as follow
$file = $request->display_profile;
$user->update([
'display_profile' => $file->getClientOriginalName()
]);
For your second question I suggest you to create a relation with a Image class where you can store all the image conversion you require. But this can be a little tricky and may require time to be coded. Fortunately there are tons of libraries that can handle this for you. My favourite is Laravel medialibrary

How to get data from variable in ln laravel 5.2

My database table has name, phone, email etc.. fields. Now I store particular field data in different variable and pass them. Here is my code. I tried it from controller function. What should I do?
$var = DB::select("SELECT * FROM reg where email = '$c_email' and Password = '$c_pass' and type = '$c_type'");
$var2 = $var->name;
$var3 = $var->phone;
return redirect('farmer')->with('key', $var2)->with('key2', $var3);
You may try the given way
return view('farmer',compact('var2', 'var3'));
Here 'farmer' is your view page
http://www.easylaravelbook.com/blog/2015/03/09/passing-multiple-variables-into-a-laravel-5-view/
When you're using redirect()->with(), you're flashing data to the session. So to get this data after redirect, use session() helper:
session('key')
https://laravel.com/docs/5.3/responses#redirecting-with-flashed-session-data
use this code. if farmer is view part. or use view part name.
$var=DB::table('reg')
->where('email', $c_email)
->where('password ', $c_pass)
->where('type', $c_pass)
->select('name','phone')
->first();
return view('farmer')->with('var', $var);
get data in view part,
$name=$var->name;
$phone=$var->phone;

what is question mark means in the url in php codeigniter framework

i'm beginner in php so i want to ask this question
I read about url in Codeigniter and know url is include controller name and then method name but i didn't know what's question mark means yet ?
for example
where form post data if the url after click on submit button is
http://localhost/code/forums/topic/25674318?addpost=Publish
i'm using Codigniter may i use this url to post submitted data to model i mean insert that data to database.
This is the symbol ? which starts the begining of query in url. And after that you can use & symbol
<?php echo $this->input->get('addpost');?>
http://www.codeigniter.com/user_guide/libraries/input.html?highlight=get
Or
<?php echo $_GET['addpost'];?>
http://php.net/manual/en/reserved.variables.get.php
And then should return Publish
The fist query in url good to have ? then any other query you can use &
Example: http://www.demo.com/example/?token=123456&something=test
$var1 = 123456;
$var2 = 'test';
site_url('example/'. '?token=' . $var1 . '&somthing=' . $var2);
Config
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-&=?';

Serving a stored file with a controller and best place to store file

I'm creating a feed in CSV file format for products in magento.
We already have some CSV files generated on the fly and returned by magento controller.
But I would like to pre-generate the file and save it in some place and directly return the file upon request.
Where is the best place to save this file?
And how can I return it from controller?
BTW, the file is rarely accessed, so I don't want to store it in memcache, just as a generic file.csv
Creating a directory under {Magento Base Install Path}/var/ would be a logical place to cache the file.
To return the file from the controller you can use:
$fileName = 'file.csv'
$filePath = Mage::getBaseDir('var') . '/csvfilecache/' . $fileName;
$content = array('type' => 'filename', 'value' => $filePath);
$this->_prepareDownloadResponse($fileName, $content, 'text/csv');
See Mage_Core_Controller_Varien_Action::_prepareDownloadResponse

Resources