Call to a member function getRealPath() on string while sending an email - laravel

Call to a member function getRealPath() on string error occurs while sending an email.
Controller
public function store(CareerRequest $request)
{
$requestData = $request->all();
$filenameWithExt = $request->file('resume')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('resume')->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extension;
$request->file('resume')->storeAs('candidateResume', $fileNameToStore);
$requestData["resume"] =$fileNameToStore ;
Career::create($requestData);
return redirect()->back();
}
Mailable
class CareerMail extends Mailable
{
public $data;
public function __construct($data)
{
$this->data = $data;
}
public function build()
{
return $this->subject('Career - '. $this->data->subject)
->view('emails.career')
->attach($this->data['resume']->getRealPath(),
[
'as' => $this->data['resume']->getClientOriginalName(),
'mime' => $this->data['resume']->getClientMimeType(),
]);
}
}
Error on line
->attach($this->data['resume']->getRealPath(),

You are trying to use function getRealPath() on your $fileNameToStore, which is a string : $filename.'_'.time().'.'.$extension.
getRealPath() will only work on $request()->file('resume')->getRealPath().
If you want to get the information of your file, you should use Uploaded file instance instead or get the file instance you stored.

Related

How to send data to view on send email in Laravel

i try this but it show Undefinded data
public function forgotPassword(Request $req){
$token = rand();
$data = $token;
Mail::to($req->email)->send(new sendPass($data));
}
First up, that code is a little bit inefficient. Why write :
$token = rand();
$data = $token;
Mail::to($req->email)->send(new sendPass($data));
when you could just write :
Mail::to($req->email)->send(new sendPass(rand()));
The issue is almost certainly because you've not declared $data in your SendPass class as a private variable :
class SendPass extends Mailable {
use Queueable, SerializesModels;
private $data;
public function __construct(string $data)
{
$this->data = $data;
}
public function build()
{
$data = $this->$data;
... rest of your code goes here.
}

Laravel - Maatwebsite/Laravel-Excel without input file

I have implemented Maatwebsite/Laravel-Excel with input file, this works fine.
public function import(Request $request)
{
$path = $request->file('file');
if (!is_file($path))
{
dd("file excel missing");
}
Excel::import(new ProductsSerialsImport,request()->file('file'));
return redirect()->back()->with('success', 'File updated!');
}
Route::post('products_serials/import', 'ProductsSerialsController#import')->name('products_serials.import');
I done something of this, but don't understand where I can receive excel file without pass through input file.
public function import(Request $request)
{
$path = "storage/excel/file.xlsx";
if(!File::exists($path)) {
dd("missing excel file!");
}
Excel::import(new ProductsSerialsImport, $path);
return redirect()->back()->with('success', 'File updated!');
}
How I can update excel file from route => 'products_serials/import' skipping upload file through the input file?
Thanks in advance!
There's a constructor. from docs it will be like this
ProductsSerialImport.php
class ProductsSerialImport implements ToCollection {
private $file;
public function __construct($file)
{
$this->file = $file;
}
public function collection(Collection $row)
{
foreach($row as $r) {
Model::create(['name', $this->file]); // Just for example
}
}
}
YourController.php
Excel::import(new ProductsSerialsImport(request()->file('file')));

Fetch image as a url from mysql database in laravel Api

I want to fetch the image from database as a URL like http://127.0.0.1:8000/uploads/category/image.png
but the result I am getting is the name of the file not the URL.
This is the model class:
class CategoryModel extends Model
{
protected $table="tb_category";
public $timestamps = false;
protected $fillable = [
'id',
'name',
'image'
];
}
This is my controller class:
class CategoryController extends Controller
{
//
public function viewPage(){
return view('category');
}
public function saveCategory(Request $request){
$category = new CategoryModel();
$category->name=$request->input('name');
if($request->hasfile('image')){
$file=$request->file('image');
$extension = $file->getClientOriginalExtension();
$filename=time().'.'.$extension;
//$headers = array('Content-Type' => 'application/octet-stream');
$file->move('uploads/category/',$filename);
$category->image =$filename;
$category->_image =$filename;
}
else{
return $request;
$category->image='';
$category->_image=null;
}
$category->save();
return view('category')->with('category',$category);
}
public function getCategories(){
$res = CategoryModel::get(
['id','name','image'
]
);
return response()->json($res,200);
}
}
In your saveCategory method, you're storing $filename in 'image' column.
Change it to store path.
public function saveCategory(Request $request){
$category = new CategoryModel();
$category->name=$request->input('name');
if($request->hasfile('image')){
$file=$request->file('image');
$extension = $file->getClientOriginalExtension();
$filename=time().'.'.$extension;
$file->move('uploads/category/',$filename);
$category->image ='http://127.0.0.1:8000/uploads/category/'.$filename; //This is where you need to save absolute path of the image.
}
else{
return $request;
}
$category->save();
return view('category')->with('category',$category);
}
One more problem here is that you've written code after return $request; in else statement. Any code written after return is unreachable for the interpreter and will not be executed.

Laravel Excel 3.1 passing data from controller to class

I have upgraded the laravel excel library (Maatswebsite) from 2x to 3.1 (running Laravel 5.6/php 7.1) and trying to make my old data work (download exported file) and cannot work out how to pass my $data (which is an array from a foreach DB query (not eloquent) in controller) to the UsersExport.php class...
If I manually create a test collection (mirroring my $data array) in the class:
return collect([
[
'name' => 'F Name 1',
'surname' => 'Last Name 1',
'email' => 'Email 1'
'date_completed' => 'xx/xx/xx'
],
[
'name' => 'F Name 2',
'surname' => 'Last Name 2',
'email' => 'Email 2',
'date_completed' => 'xx/xx/xx'
]
]);
the above works perfect and the file is created and downloads when I run:
return Excel::download(new UsersExport, 'Test.xlsx');
But I want to pass my array ($data) from the controller to the class and not sure HOW I do this... I am trying to get something like this to work:
return Excel::download(new UsersExport($data), 'Test.xlsx');
From reading the specific posts I could find, I believe I need to create a constructor in the Class to accept my $data - but not sure how, and how to return that data if I succeed in my class accepting the data etc... Is the FromCollection the right option?
private $data;
public function __construct($data)
{
$this->data = $data;
}
Appreciate any assistance.... Thanks in advance.
Your approach is right. then use the collection() function to return that data.
private $data;
public function __construct($data)
{
$this->data = $data;
}
public function collection()
{
return $this->data;
}
if you want passing param data to class you use construct.
Example Controller:
<?php
namespace App\Http\Controllers\Reports;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\CustomerinvoiceExport;
use App\Model\OrderInvoiceList;
use Illuminate\Http\Request;
class CustomerInvoiceController extends Controller
{
public function index(Request $request)
{
if ($request->has('start_date')) {
$start_date = $request->start_date;
} else {
$date_now = Carbon::now();
$start_date = $date_now->toDateString();
}
if ($request->has('end_date')) {
$end_date = $request->end_date;
} else {
$date_now = Carbon::now();
$end_date = $date_now->toDateString();
}
$customer_invs = OrderInvoiceList::customer_invoice($start_date, $end_date);
return Excel::download(new CustomerinvoiceExport($customer_invs), 'Customer_Invoice_Report.xlsx');
}
}
}
Class Export
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
class CustomerinvoiceExport implements FromCollection
{
protected $customer_invs;
/**
* Customer Invoice Report
*/
public function __construct($customer_invs)
{
$this->customer_invs = $customer_invs;
}
/**
* #return invoice_list
*/
public function collection(): array
{
$invoice_list = $this->invoice_list;
...........your logic here....
}
}

laravel notifications not storing into my database plus error

i couldn't store the notification into my notification table inside my database,
i was trying to make notification every time there is a new Post how can i make this work.
Error:
Notification:
use Queueable;
public $post;
public function __construct()
{
}
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
return [
'title' => $this->post->title,
];
}
public function toArray($notifiable)
{
return [
];
}
}
Post Controller:
public function store(Request $request)
{
$this->validate($request, [
'title'=>'required|max:100',
'body' =>'required',
]);
$title = $request['title'];
$body = $request['body'];
$post = Post::create($request->only('title', 'body'));
Auth::user()->notify(new NotifyPost($post));
return redirect()->route('posts.index')
->with('flash_message', 'Article,
'. $post->title.' created');
}
You need to inject the post within the construct, or else it never gets resolved.
protected $post;
public function __construct(Post $post)
{
$this->post = $post;
}

Resources