I hope so im getting close to final stage. I uploading file to storage/files/ and create uniq folder for each upload file with id_message without problem and store file data in table files
Final path of file is /storage/files/{id_message}/{file_name} both variables id_message and file_name are in table files.
FileController function for fileUpload:
function fileUpload(Request $request)
{
$request->validate([
'id_message' => 'required|min:6'
]);
$idParameter = $request->id_message=$request->id_message;
$result=$request->file('file_path')->store('files/'.$idParameter);
$file = new File;
$file->id_message=$idParameter;
$file->file_path=$result;
$file->file_name=$request->file('file_path')->getClientOriginalName();
$file->save();
after upload i have this data in table files:
id id_message file_name
1 000001 Myfile.zip
/storage/app/files/000001/Myfile.zip
FileController : getDownload
public function getDownload($id)
{
$resultFile = DB::table('files')->where('id_message',$id)->first();
$attachment = store_path().'/' . $resultFile->id_message . '/' . $resultFile->file_name;
return response()->download($attachment);
}
route
Route::get('download/{id}/{fileName}', 'FileController#getDownload')->name('downloadFile');
view.blade
<td>Download</td>
error
Undefined variable:resultFile
do i getting closer to finaly download file in laravel ?
controller for view table users
public function postLogin(Request $request)
{
request()->validate([
'id_message' => 'required',
'sms_code' => 'required',
]);
$credentials = $request->only('id_message', 'sms_code');
$request->session()->flash('success','');
if ($user=User::where($credentials)->first()) {
auth()->login($user);
return redirect()->intended('dashboard')->with('success','');
}
return Redirect::to("login")->with(['url_attribute' => $url_attribute,'id_message' => $id_message])->with('error','');
}
public function dashboard()
{
if(Auth::check())
{
return view('dashboard');
}
return Redirect::to("login")->withSuccess('Opps! You do not have access');
}
Leading zeros may be getting stripped if its converted to integer.
Try adding padding to $id once it's passed to getDownload:
str_pad($id, 6, '0', STR_PAD_LEFT);
Related
I am trying to update image to image/brand/ folder but unexpectedly error is occurred.
public function update(Request $request,$id){
$validated = $request->validate([
'brand_name' => 'required|max:4',
// 'brand_image' => 'required|mimes:jpg,jpeg,png',
]);
$old_image=$request->old_image;
$brandimage=$request->file('brand_image');
$image_gen=hexdec(uniqid());
$image_exten=strtolower($brandimage->getClientOriginalExtension());
$image_name=$image_gen.'.'.$image_exten;
$image_location='image/brand/';
$image_uplioad= $image_location.$image_name;
$brandimage->move($image_location,$image_name);
unlink($old_image);
Brand::find($id)->update([
'brand_name' =>$request->brand_name,
'brand_image'=>$image_uplioad,
'Created_at'=> Carbon::Now()
]);
return Redirect()->back()->with('success','Brand image updated Successfully');
}
ErrorException
unlink(): Invalid argument this is the error what i got i need over come this problem please help
You could optimize it by update the image from model by creating updateImage method inside the model like this
Brand Model
.....
public function updateImage($image)
{
$oldImage = $this->brand_image?? '';
$this->brand_image = $image->store('brand', 'public');
$this->save();
Storage::disk('public')->delete($oldImage);
}
.....
after that the controller will be like this
$validated = $request->validate([
'brand_name' => 'required|max:4',
// 'brand_image' => 'required|mimes:jpg,jpeg,png',
]);
$brand = Brand::find($id)->update([
'brand_name' =>$request->brand_name,
'Created_at'=> Carbon::Now()
]);
if($request->hasFile('brand_image'))
$brand->updateImage($request->file('brand_image'));
return Redirect()->back()->with('success','Brand image updated Successfully');
please pass the $old_image=$request->old_image; value from in blade.
I want to export csv file using Maatwebsite in laravel. The process is running well, but in the csv file contains blank line in last data when i open it using notepad. Displayed at image below
This is the function to get the data
public function view(): View
{
$data = DB::table('transaction as a')
->join('transaction_details as b', 'a.id', 'b.header_id')
->select('b.order_number', 'b.changed_number')
->whereIn('a.id', $this->id)
->where('a.approve', 1)
->get();
return view('vendor.voyager.transaction.excel-csv.generate-csv', [
'data' => $data
]);
}
public function getCsvSettings(): array
{
return [
'enclosure' => ''
];
}
This is function to call the function above
public function generate(Request $request)
{
$id = $request->ids;
return Excel::store(new ExportTransaction($id), 'test.csv', 'ftp');
}
How to solve my problem to remove blank line in the csv fie
In my update method in the controller when i try to dump the requested data
all attributes comes fine but the image printed as object not the imagePath that is saved in the database
Update method
public function update(Request $request, SlideShow $slideshow)
{
$slideshow->update($request->validate([
'title_en' => 'required',
'title_ar' => 'required',
'link' => 'nullable|url',
'image' => 'image'
]));
dd($slideshow);
$slideshow->uploadImage();
session()->flash('success', __('dashboard.slideshow.edit_success'));
return redirect()->route('admin.slideshow.index');
}
SlideShow model
class SlideShow extends Model
{
protected $fillable = ['title_en', 'title_ar', 'link', 'image'];
public function uploadImage($imageName = 'image')
{
if(request()->has($imageName)){
\Storage::delete($this->image);
$uploadedImage = request()->$imageName->store('slideshow/');
\Image::make('storage/'.$uploadedImage)->resize(870, null, function ($constraint) {
$constraint->aspectRatio();
})->save();
$this->update(['image' => $uploadedImage]);
}
}
public static function search($request)
{
return static::where(lang('title'), 'like', '%' . $request->search . '%')->paginate(10);
}
}
This is normal behavior. When the request is made, Laravel converts the files in the request to the UploadedFile class. Since you're accessing the validated data from the request, this is what is returned. You're directly filling these values as attributes, and the UploadedFile doesn't automatically convert to a path, so this should probably not be included in the first update.
Anyway, in your code you're actually including in your question has the dd() method before you call the uploadImage method, and this method saves the path and does not try to safe the entire UploadedFile class. So if you dump it after it probably has the correct value there, can you verify this?
I'd do it like this:
public function update(Request $request, SlideShow $slideshow)
{
// validate first
$data = $request->validate([
'title_en' => 'required',
'title_ar' => 'required',
'link' => 'nullable|url',
'image' => 'image'
]);
// only pull the data that we can update right now
$slideshow->update($request->only(['title_en', 'title_ar','link']));
// handle and store image if needed
$slideshow->uploadImage();
session()->flash('success', __('dashboard.slideshow.edit_success'));
return redirect()->route('admin.slideshow.index');
}
and then the method on the model:
public function uploadImage($imageName = 'image')
{
if(request()->has($imageName)){
\Storage::delete($this->image);
$uploadedImage = request()->$imageName->store('slideshow/');
\Image::make('storage/'.$uploadedImage)->resize(870, null, function ($constraint) {
$constraint->aspectRatio();
})->save();
$this->update(['image' => $uploadedImage->path()]);
}
}
I am using laravel-5.4 make:auth. Added one extra field in user table -> profile picture in views\auth\register.blade.php. I want to do the following:
1. How to store the image path in database for all users who registers.
Have tried defining store function in existing app\Http\Controllers\Auth\RegisterController.php. It doesn't even get into store function. Where and how to write the store function , so that the controller executes it.
If you're handling image uploading with JS and image_path field in the form already has path to the image, then just modify create() method in Auth/RegisterController.php controller:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'image_path' => $data['image_path'],
'password' => bcrypt($data['password']),
]);
}
If you don't get to your method, just check if your form matches it properly. E.g.:
<form method="POST" action="route('register.user)">
...
</form>
Route & controller:
Route::post('/users/register', [
'uses' => 'UserController#register',
'as' => 'register.user'
]);
public function register(Request $request){
if ($file = $request->file('photo_id')) {
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file' => $name]);
}
...other methods/logic...
}
I have problem with saving data to m:n table layout in laravel 5. I have table appliances and table documentations, where pivot table is documentation_appliance.
Models are:
class Appliances extends Model
{
public function documentations()
{
return $this->belongsToMany('documentations');
}
}
and
class Documentation extends Model
{
public function appliances()
{
return $this->belongsToMany('appliances');
}
}
Now I try to save data to table in my Controller
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:255',
'file_name' => 'required',
]);
if($request->hasFile('file_name') ) {
$fname = $request->file('file_name')->getClientOriginalName();
$request->file('file_name')->move(
base_path() . '/public/files/documentation/', $fname
);
}
$document = new Documentation();
$document->name = $request->name;
$document->filename = $fname;
if($document->save()) {
$doc_ids = $request->documentation_appliance;
$document->appliances()->sync($doc_ids);
}
return view('backend.documentation.index', [
'documentations' => $this->documents->getDocuments(),
]);
}
Data to table documents are saved corectly, image is stored, but I have problem with saving data to pivot table. Screen displays me this error:
FatalErrorException in compiled.php line 10191:
Class 'appliances' not found
in compiled.php line 10191
nothing more, I guess I have bad use of class somewhere or am I doing bad something else? Thanks everyone for help.
according to https://laravel.com/docs/5.2/eloquent-relationships#many-to-many your table name must be appliance_documentation not documentation_appliance.