Laravel7 Call to undefined method Illuminate\Database\MySqlConnection::find() - laravel-7

in my Laravel 7 app when i go to a page i am getting this
Call to undefined method Illuminate\Database\MySqlConnection::find()
In my app/Http/Controllers/clientController.php I have
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Client;
class ClientController extends Controller {
public function viewclient(Request $id)
{
$client = DB::find($id);
return view('viewclient', compact('client', 'id'));
}
}
In my app/Client.php I have
namespace App;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
protected $table = 'clients';
}
Not sure what I am missing.

You should mention the table name when use query builder :
$client = DB::find($id); // no table here
Change to
$client = DB::table('clients')->find($id); // here 'clients' is your table name
See Official Documentation

Related

codeigniter 4 Undefined property: App\Controllers\Test::$load

I'm switching to Condeigniter 4.
Trying to do some basic stuff.
I can't load the model into controller function.
Controller Test.php:
<?php
namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$this->load->model('user_model');
}
}
Model User_model.php
<?
namespace App\Models;
use CodeIgniter\Model;
class User_model extends Model
{
public function get_user_info($user_id)
{
return 1;
}
}
I'm getting error: Undefined property: App\Controllers\Test::$load
What am I doing wrong?
Codeigniter 4 and 3 have different way to load model. In codeigniter 4 you can use
use CodeIgniter\Model\User_model;
$modelUser = new \App\Models\User_model();
or
$userModel = model(User_model::class);
or you can see in Codeignter 4 User Guide, Accessing Models
Model loading approach has been changed to Codeigniter 4. The way of loading the models that you use in your code has been replaced. Check out the library Models and information about loading the model can be found here Accessing Models.
Controller - test code
<?php namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$TestModel = new TestModel;
$this->data['test'] = $TestModel->test(2);
return view('App\Views\test',$this->data);
}
}
Model - Test code
<?php namespace App\Models;
use CodeIgniter\Model;
class TestModel extends Model
{
public function test($var = 1)
{
return $var;
}
}
The above sample code allowed us to reference the $test variable in the view, which will display the value 2.

Maatwebsite / laravel give name to Worksheet in export

I am using Maatwebsite / laravel to export data , I need to rename the Worksheet ,
below my class :
<?php
namespace App\Exports;
use App\Models\Client;
use App\Models\Item;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
class ItemExport implements FromCollection, WithHeadings, ShouldAutoSize
{
protected $rows;
use Exportable;
public function __construct($client_id , $items , $company_name)
{
$this->client_id = $client_id;
$this->items = $items;
$this->company_name = $company_name;
}
public function collection()
{
$i = 1;
$client = Client::find($this->client_id);
$item_new= [];
foreach($this->items as $item){
$item_new[] = [$i++,
$this->company_name,
$this->item,
$client->client_name,
];
}
return collect($item_new);
}
public function title(): string
{
return $this->company_name;
}
public function headings(): array
{
return [
'#',
'Company',
'item',
'client',
];
}
}
but it seems the public function title(): string , not working .
i supposed that the title function will work , but seems no
any idea how to give name to Worksheet ?
thanks
So close. You just need to implement the WithTitle interface.
class ItemExport implements FromCollection, WithHeadings, WithTitle, ShouldAutoSize
An example is somewhat buried in the multiple sheets section of the documentation, but I can confirm it works for single sheets too.
https://docs.laravel-excel.com/3.1/exports/multiple-sheets.html#sheet-classes
Laravel-How to create multiple sheet in one excel file using maatwebsite/excel": "^3.1.
1st step: I created a multiple excel export file
php artisan make:export MultipleSheetExport
2nd Create a route and controller
In controller I have written this function
public function state_summary_export()
{
return Excel::download(new MultipleSheetExport, 'ADCC Summary.xlsx');
}
Note: 'ADCC Summary.xlsx' is my excel file name.
Don’t forget to add this facades in you controller
use App\Exports\MultipleSheetExport;
use Maatwebsite\Excel\Facades\Excel;
3rd step: Open MultipleSheetExport
<?php
namespace App\Exports;
use Illuminate\Support\Arr;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithTitle;
class MultipleSheetExport implements WithMultipleSheets
{
public function sheets(): array
{
$sheets = [
new StateSummaryExport(),
new BMRPerformanceExport(),
];
return $sheets;
}
}
4th step: Go to Child StateSummaryExport and add this given line.
class StateSummaryExport implements WithTitle
public function title(): string
{
return 'State Summary';
}
Note: ‘State Summary’ is your sheet name. Do the same in BMRPerformanceExport.
Thank you.

Codeigniter 4 - call method from another controller

How to call "functionA" from "ClassA" in "functionB" inside "ClassB" ?
class Base extends BaseController
{
public function header()
{
echo view('common/header');
echo view('common/header-nav');
}
}
class Example extends BaseController
{
public function myfunction()
// how to call function header from base class
return view('internet/swiatlowod');
}
}
Well there are many ways to do this...
One such way, might be like...
Assume that example.php is the required Frontend so we will need a route to it.
In app\Config\Routes.php we need the entry
$routes->get('/example', 'Example::index');
This lets us use the URL your-site dot com/example
Now we need to decide how we want to use the functions in Base inside Example. So we could do the following...
<?php namespace App\Controllers;
class Example extends BaseController {
protected $base;
/**
* This is the main entry point for this example.
*/
public function index() {
$this->base = new Base(); // Create an instance
$this->myfunction();
}
public function myfunction() {
echo $this->base->header(); // Output from header
echo view('internet/swiatlowod'); // Output from local view
}
}
When and where you use new Base() is up to you, but you need to use before you need it (obviously).
You could do it in a constructor, you could do it in a parent class and extend it so it is common to a group of controllers.
It's up to you.

How can I pass parameter in the laravel excel?

I get tutorial from here : https://laravel-excel.maatwebsite.nl/docs/3.0/export/basics
<?php
...
use App\Exports\ItemsDetailsExport;
class ItemController extends Controller
{
...
public function exportToExcel(ItemsDetailsExport $exporter, $id)
{
//dd($id); I get the result
return $exporter->download('Summary Detail.xlsx');
}
}
My export like this :
<?php
namespace App\Exports;
use App\Repositories\Backend\ItemDetailRepository;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Illuminate\Support\Facades\Input;
class ItemsDetailsExport implements FromCollection
{
use Exportable;
protected $itemDetailRepository;
public function __construct(ItemDetailRepository $itemDetailRepository)
{
$this->itemDetailRepository = $itemDetailRepository;
}
public function collection()
{
$test = Input::get('id');
dd('yeah', $test);
}
}
I want to pass id parameter to export file. I try like that, but I don't get the id. The id is null
How can I solve this problem?
For passing data from controller to laravel excel function we can pass and use data like below
For example, we have to pass data year like 2019 we will pass like below
in controller
Excel::download(new UsersExport(2019), 'users.xlsx');
In laravel import file
class UsersExport implements FromCollection {
private $year;
public function __construct(int $year)
{
$this->year = $year;
}
public function collection()
{
return Users::whereYear('created_at', $this->year)->get();
}
}
you can refer all following official documentation link
https://docs.laravel-excel.com/3.1/architecture/objects.html#plain-old-php-object
Unfortunately you can't use normal dependency injection when you have a specific parameter. This is what you can do though:
class ItemsDetailsExport implements FromCollection
{
use Exportable;
protected $itemDetailRepository;
protected $id;
public function __construct(ItemDetailRepository $itemDetailRepository, $id)
{
$this->itemDetailRepository = $itemDetailRepository;
$this->id = $id;
}
public function collection()
{
$test = $this->id;
dd('yeah', $test);
}
}
Now the problem is that the container doesn't know how to resolve $id however there are two ways around this.
Manual passing of $id:
public function exportToExcel($id)
{
$exporter = app()->makeWith(ItemsDetailsExport::class, compact('id'));
return $exporter->download('Summary Detail.xlsx');
}
Route injection:
Define your route as:
Route::get('/path/to/export/{itemExport}', 'ItemController#exportToExcel');
In your RouteServiceProvider.php:
public function boot() {
parent::boot();
//Bindings
Route::bind('itemExport', function ($id) { //itemExport must match the {itemExport} name in the route definition
return app()->makeWith(ItemsDetailsExport::class, compact('id'));
});
}
Then your route method is simplified as:
public function exportToExcel(ItemsDetailsExport $itemExport)
{
//It will be injected based on the parameter you pass to the route
return $itemExport->download('Summary Detail.xlsx');
}

"Class 'App\Http\Controllers\InvoicesExport' not found" in Maatwebsite Laravel

In my Laravel Project i use Maatwebsite\Excel for to export Data in Excel format. i use New Version 3.0 (Maatwebsite\Excel)
App/Exports.php
namespace App\Exports;
use App\Purchasepaymenttransaction;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class InvoicesExport implements FromView
{
public function view(): View
{
$purchasepayment=Purchasepaymenttransaction::Where('transaction_category',2)->OrderBy('transaction_date','DESC')->get();
return view('exports.purchasepayments', [
'purchasepayment' => $purchasepayment
]);
}
}
In my Payment Controller
<?php
namespace App\Http\Controllers;
use DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Purchasepaymenttransaction;
use Excel;
use App\Exports;
class PaymentController extends Controller
{
public function purchaseexport()
{
return Excel::download(new InvoicesExport, 'invoices.xlsx');
}
}
i got "Class 'App\Http\Controllers\InvoicesExport' not found" Error
return Excel::download(new \InvoicesExport, 'invoices.xlsx');
Normally you use backslash('\') before class when you get class not found exception. It will automatically find that class from your code and use it where you need and don't require to use it at top of file. It is similar to
use [YOUR_PATH]/InvoicesExport
If you use "use" statement then no need to backslash('\'). You can use any of above two. It will work.
use App\Exports\InvoicesExport as ExportsInvoicesExport;

Resources