I'm working on the Excel exports in my laravel project. Now I get this error:Class 'App\Export\UsersExport' not found I made 3 others and they worked. This one doesn't and I can't seem to figure out what causes this error. I'm using "Maatwebsite's Laravel-Excel".
My exports controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
//exports
use App\Export\UsersExport;
use App\Exports\StudyClassExport;
use App\Exports\EducationsExport;
use App\Exports\IntakesExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class ExportsController extends Controller
{
public function users()
{
return Excel::download(new UsersExport, 'gebruikers.xlsx');
}
public function educations()
{
return Excel::download(new EducationsExport, 'opleidingen.xlsx');
}
public function classes()
{
return Excel::download(new StudyClassExport, 'klassen.xlsx');
}
public function intakes()
{
return Excel::download(new IntakesExport, 'intakes.xlsx');
}
}
UsersExport:
<?php
namespace App\Exports;
use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
class UsersExport implements FromCollection
{
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
return User::all();
}
}
I have no idea what causes this error. The others work but this one doesn't
You have a typo:
use App\Export\UsersExport;
use App\Exports\UsersExport;
ExportS
Related
I just started learning Laravel.
I get an error message of "Error: Call to undefined function addPost()". What seems to be the problem in my code?
$command == "addPost";
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class PostsController extends Controller
{
function checkPostsCommand(Request $request) {
$command = $request->btn;
$data = $this->$command($request->all());
return response()->json($data);
}
private function addPost() {
return 'wew';
}
}
use it like this :
$this->$command();
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class PostsController extends Controller
{
function checkPostsCommand(Request $request)
{
$command = $request->btn;
return $this->addPost();
}
private function addPost() {
echo 'wew';
}
}
I just upgraded Laravel 5.4 to 5.5 and now I have to change all the coding that used the old Laravel-Excel.
I'm using php 7.2.25, Windows/Wamp.
I am trying to upload an excel file, get it's data, do lots of check and calculations on it (Not in the code yet) and then create a new excel file and give the user the Windows 'Save File' option.
Not sure how to get the Windows save file window, don't see any explanation on the documentation.
If I can't get the Windows save file window, I'm not sure how to set the path to: My Documents\test for example.
With the code I have, I get this error:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR)
Type error: Return value of Maatwebsite\Excel\Sheet::mapArraybleRow() must be of the type array, string returned
My Import class code:
namespace App\Imports;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use App\Exports\TimesheetsExport;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Facades\Excel;
use App\User;
class TimesheetsImport implements ToCollection, WithHeadingRow
{
private $user;
public function __construct($param)
{
$this->user = $param;
}
public function collection(Collection $rows)
{
$data = new Collection([$this->user->fullName()]);
foreach ($rows as $row)
{
$data->put($row['date'], $row['in'], $row['out']);
}
return Excel::download(new TimesheetsExport($data), 'testtttt.xlsx');
My Export class:
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Support\Collection;
class TimesheetsExport implements FromCollection
{
protected $rows;
public function __construct(Collection $rows)
{
$this->rows = $rows;
}
public function collection()
{
return $this->rows;
}
}
Can someone please help?
After a day and a half, I managed to make it work.
The working code:
Import Class:
namespace App\Imports;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Facades\Excel;
use App\User;
class TimesheetsImport implements ToCollection, WithHeadingRow
{
public $data;
public function collection($rows)
{
$this->data = $rows;
}
}
Export class:
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
class TimesheetsExport implements FromCollection
{
protected $rows;
public function __construct($rows)
{
$this->rows = $rows;
}
public function collection()
{
return $this->rows;
}
}
My controller:
public function importTimesheets(Request $request)
{
$import = new TimesheetsImport;
$rows = Excel::toCollection($import, $request->file('file'));
return Excel::download(new TimesheetsExport($rows), 'test.xlsx');
}
With this code, I get the Windows 'Save File' as well.
This was not fun, but it's done, I hope it will help someone else.
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Excel;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Excel as ExcelType;
...
$array = [[1,2,3],[3,2,1]];
return \Excel::download(new class($array) implements FromArray{
public function __construct($array)
{
$this->array = $array;
}
public function array(): array
{
return $this->array;
}
},'db.xlsx', ExcelType::XLSX);
decided so without additional frames. Laravel 7*, Maatwebsite 3*
in local host i haven't this problem but in linux host in CPannel i receive this error
Class 'App\Service' not found
it's just an example i have same problem in some models..
my relations doesnt work properly in original host but in local host i haven't got any problem
my models:
<?php
namespace App;
use App\Category;
use App\Project;
use App\Service;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = [
'title', 'parent_id','title_en',
];
public function category(){
return $this->hasMany(Category::class,'parent_id');
}
public function parent(){
return $this->category('parent');
}
public function project(){
return $this->belongsToMany(project::class);
}
public function service(){
return $this->belongsToMany(service::class);
}
}
my controller :
namespace App\Http\Controllers;
use App\Service;
use Illuminate\Http\Request;
class ServiceController extends Controller
{
public function landpage(){
$services=Service::with('cat')->get();
return view('services.index',compact('services'));
}
public function detail($id){
$services=service::with('cat')->findOrFail($id);
return view('service_detail.index',compact('services'));
}
}
ok as i found .. the models are sensitive to capital words..
i changed all of the models to capital words..
App/service => App/Service
and it worked
I created a Mailable called Class UserRequest
I'm trying to call it from inside a controller buy this is the error I get:
Class 'App\Http\Controllers\UserRequest' not found
I also tried ->send(new \UserRequest($msgdata)); but it still doesn't work.
Controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class ContactController extends Controller
{
public function index()
{
return view('contact');
}
public function sendemail(Request $request)
{
$msgdata = array('subject'=>$request->subject,'email'=>$request->email, 'name'=>$request->name,'body'=>$request->body);
try
{
Mail::to('dddddddd#dddsdsf.com')
->send(new UserRequest($msgdata));
}
catch(Exception $e)
{
}
}
}
Include your class at the top like this
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\UserRequest; // including your class
class ContactController extends Controller
{
public function index()
{
return view('contact');
}
public function sendemail(Request $request){
$msgdata = array('subject'=>$request->subject,'email'=>$request->email,
'name'=>$request->name,'body'=>$request->body);
try {
Mail::to('dddddddd#dddsdsf.com')->send(new UserRequest($msgdata));
}catch(Exception $e){
// Log your exception
}
}
}
add 'App\Http\Controllers\UserRequest' to the head
use App\Http\Controllers\UserRequest;
at the top.
You will need to add the right path to the top as stated by other.
Also check the namespace in the UserRequest class
I'm following the Laracasts series and have run into an issue on the episode Laravel 5.4 From Scratch: Route Model Binding.
Laravel version:
Laravel Framework 5.6.13
The error:
Class App\Http\Controllers\Panel does not exist
This shows on both the /panel and /panel/1 pages
App\Http\Controllers\PanelController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
// Code works if I uncomment below line, and change the show function to "show($panel)"
//use App;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
routes/web.php
// Main panel view
Route::get('/panel', 'PanelController#index');
// Individual panel view
Route::get('/panel/{panel}', 'PanelController#show');
App/Panel.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Panel extends Model
{
public static function activePanels()
{
return static::where('status', 1)->get();
}
}
Add this line in panel controller before the class
use App\Panel;
You need to add use App\Panel; to top of class
Or call it by full namespace $panels = App\Panel::all();
You don't included your model to class.
Add App\Panel to main include section:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Panel;
class PanelController extends Controller
{
public function index()
{
$panels = Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}
or load model in your class method manually:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PanelController extends Controller
{
public function index()
{
$panels = App\Panel::all();
return view('panel/index', compact('panels'));
}
public function show(Panel $panel)
{
return $panel;
return view('panel/show', compact('panel'));
}
}