Laravel-Excel toArray validation not working - php

I'm using the laravel-excel package and want to import excel into an array, it works well for the main code (without validation), but when I tried to add validation/rule, the validation not working properly (I mean, like the validation is just skipped, so if I upload a file that doesn't fit the format, the array output still comes out), here's my import code
<?php
namespace App\Imports;
use App\Models\Request;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Illuminate\Validation\Rule;
class RequestImport implements ToModel, WithValidation, WithHeadingRow
{
use Importable;
/**
* #param array $row
*
* #return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Request([
'no' => $row[0],
'username' => $row[1],
'name' => $row[2],
]);
}
// create validation by heading row
public function rules(): array
{
return [
'no' => 'required',
'username' => 'required',
'name' => 'required',
];
}
}
and here's my controller code when I call the RequestImport class
$file = request()->file('fileExcel');
// save file
if (isset($request->upload)) {
$this->saveFile($file);
}
// return excel import into array
$data = (new RequestImport)->toArray($file); //here I call the RequestImport
return response()->json([
'status' => 'success',
'message' => 'success,
'data' => $data[0],
]);
Is something wrong with my code? or I missed something?

it's not official but I found a workaround.
first, I added importable concern,
then defined a class attribute $data.
this will store rows once all validations are satisfied.
<?php
namespace App\Imports;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToArray;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
class ImportData implements ToArray, WithValidation, WithHeadingRow
{
use Importable;
public $data;
public function array(array $rows)
{
$this->data = $rows;
}
public function rules(): array
{
return [
// validation rules
];
}
}
in my controller
$import = new ImportData;
Excel::import($import, request()->file('file'));
return response()->json($import->data);

Related

Export Laravel excel Image failed

Dotrying to export my daanswerdata through laravel excel. https://docs.laravel-excel.com/3.1/getting-started/- this is the documentation I used for reference. At first, it is running successfully, but when I open the exported file, it exports the path of the image instead of exporting the actual images. Is there a way to export the image(the png file)?
This is exported file
this is the local path folder where the image saved
this is how its look like in my database .
here is my code:
1.Export.php
<?php
namespace App\Exports;
use App\Models\signature;
use Maatwebsite\Excel\Concerns\FromCollection;
//frow laravel excel drawing(image export)
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
class SignatureExport implements FromCollection, WithHeadings, WithColumnWidths
{
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
return signature::all();
}
public function drawings()
{
$drawing = new Drawing();
$drawing->setName('signature');
$drawing->setDescription('This is my signatuer');
$drawing->setPath(public_path('/uploads/signatures'));
$drawing->setHeight(90);
$drawing->setCoordinates('D1');
return $drawing;
}
public function headings(): array
{
return [
'id',
'name',
'description',
'signature',
'created_at',
'updated_at',
];
}
public function columnWidths(): array
{
return [
'A' => 30,
'B' => 30,
'C' => 30,
'D' => 30,
'E' => 30,
];
}
}
2.Export Controller
public function exportexcel()
{
return Excel::download(new SignatureExport, 'signaturelist.xlsx');
}
Do I miss anything? any answer is appreciated.
problem solved with the answers that are being accepted .
but ther is new problem came out
instead of 3 image , only 1 image exported sucessfully
<?php
namespace App\Exports;
use App\Models\signature;
use Maatwebsite\Excel\Concerns\FromCollection;
//frow laravel excel drawing(image export)
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use Maatwebsite\Excel\Concerns\WithDrawings;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
class SignatureExport implements FromCollection, WithHeadings, WithColumnWidths, WithDrawings
{
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
return signature::all();
}
public function drawings()
{
$drawing = new Drawing();
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('signature');
$drawing->setDescription('This is my signatuer');
$drawing->setPath(public_path('/uploads/signatures/62ac8a575af1a.png'));
$drawing->setHeight(90);
$drawing->setCoordinates('D1');
return $drawing;
}
public function headings(): array
{
return [
'id',
'name',
'description',
'signature',
'created_at',
'updated_at',
];
}
public function columnWidths(): array
{
return [
'A' => 30,
'B' => 30,
'C' => 30,
'D' => 30,
'E' => 30,
];
}
}
what should i do to get all image being exported?
In your SignatureExport class file you are missing the WithDrawings implements
class SignatureExport implements WithDrawings //like this
So your SignatureExport class should look something like this
class SignatureExport implements FromCollection, WithHeadings, WithColumnWidths, WithDrawings
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
This import is just to instance $drawing = new Drawing(); object.
For the export file to work you will need to add withDrawing concerns like you are adding WithHeadings and others

Is it possible to apply multiple filters to a single route in CodeIgniter 4

I have tried several ways to achieve this, but none seems to work,
it seems CodeIgniter 4 does not have the ability to apply multiple filters to a single route, currently here is what I am trying:
ProvideInfoFilter.php:
<?php namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class ProvideinfoFilter implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
echo "pinfo";
$account_data = new \App\Libraries\Account_Data;
return $account_data->no_info_redirect();
}
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
}
}
AccessFilter.php:
<?php namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class AccessFilter implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
$account_data = new \App\Libraries\Account_Data;
echo "accessf";
if ($request->uri->getSegment(1) !== 'm' && $request->uri->getSegment(2) !== 'm' && !$request->getGet('token'))
{
return $account_data->is_logged_in();
}
}
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
}
}
Filters.php:
<?php namespace Config;
use CodeIgniter\Config\BaseConfig;
class Filters extends BaseConfig
{
public $aliases = [
'toolbar' => \CodeIgniter\Filters\DebugToolbar::class,
'accesscontrol' => \App\Filters\AccessFilter::class,
'provide_info' => \App\Filters\ProvideinfoFilter::class
];
public $globals = [
'before' => [
],
'after' => [
'toolbar'
],
];
public $methods = [];
public $filters = [
'provide_info' => ['before' => ['user', 'user/*']],
'accesscontrol' => ['before' => ['user', 'user/*']]
];
}
I have added echo statements for debugging
The problem is, Commenting out either 'accesscontrol' => ['before' => ['user', 'user/*']] or 'provide_info' => ['before' => ['user', 'user/*']] applies either of the filters the and the echoed string can be seen in the output. But having both of them like demonstrated above does not apply both of them.
It is important for me to have both filters running as I want to apply specific exemptions for each of them using the $globals array.
If you return a Responce instance in a before filter it will be sent back to the client and the script execution will stop. https://codeigniter.com/user_guide/incoming/filters.html#before-filters
It's normal then that your second filter doesn't run.
In general, avoid to return anything in a before filter if you want it to not stop your script.
You are already created 2 filters which are right after that you have to create a alias of both filters on Filter.php file.
After that you have to open app/config/Feature.php file and chage this:
public $multipleFilters = false;
To
public $multipleFilters = true;
After that you go to app/config/routes.php file and then
if you create single route then
$routes->get('home', 'Dashboard::index', ['filter' => ['ProvideinfoFilter','AccessFilter']]);
In case you want to use group then:
$routes->group('msp', ['filter' => ['authGuard','AdminAuth']], function($routes){
$routes->get('/', 'MSPController::msp');
$routes->get('list_data', 'MSPController::list_data');
});
For more information follow this Codeigniter 4 Doc

How to generalize a resource function to be used in all controllers for different models?

In laravel API Resources:
I need a dynamic way to generalize a code for all resources to be used in all controllers instead of using resources in all methods for each controller .. for more clarification, I have a trait that includes generalized functions which return json responses with data and status code, lets take a "sample function" suppose it is showAll(Collection $collection) which is used for returning a collection of data of the specified model for example it is used for returning all users data ..
so I need to build a function that call what ever resource of the specified model, knowing that I have many models...
a) trait that include showAll method:
namespace App\Traits;
use Illuminate\Support\Collection;
trait ApiResponser
{
private function successResponse($data, $code) {
return response()->json($data, $code);
}
protected function showAll(Collection $collection, $code = 200) {
$collection = $this->resourceData($collection);
$collection = $this->filterData($collection);
$collection = $this->sortData($collection);
$collection = $this->paginate($collection);
$collection = $this->cacheResponse($collection);
return $this->successResponse([$collection, 'code' => $code], $code);
}
protected function resourceData(Collection $collection) {
return $collection;
}
}
b) usercontroller as a sample
namespace App\Http\Controllers\User;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;
class UserController extends ApiController
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$users = User::all();
// Here the showAll(Collection $collection) is used
return $this->showAll($users);
}
}
c) UserResource:
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
return [
'identity' => $this->id,
'name' => $this->name,
'email' => $this->email,
'isVerified' => $this->verified,
'isAdmin' => $this->admin,
'createDate' => $this->created_at,
'updateDate' => $this->updated_at,
'deleteDate' => $this->deleted_at,
];
}
}
generalize: means used everywhere without code redundancy
What about providers, you may load data there and make that data reachable at places where user data can be reachable ?
laravel docs
I found a simple solution.. by adding the following method
protected function resourceData($collection) {
$collection = get_class($collection[0]);
$resource = 'App\Http\Resources\\' . str_replace('App\\', '', $collection) .
'Resource';
return $resource;
}
The $collection[0] in the first line of this method will get the
model you are currently using.
get_class will get the model name ex: App\User
'App\Http\Resources\\' . str_replace('App\\', '', $collection):
This will get the path of the resource by adding 'App\Http\Resources\' before the
model
str_replace('App\\', '', $collection): will remove App\ path from the collection
name so App\User should be User
then 'Resource' would be concatenated with the previous results and the whole
string should be like that: App\Http\Resources\UserResource
So at the end you should return the whole string App\Http\Resources\UserResource
,finally you should call the resourceData() in
the showAll() method:
protected function showAll(Collection $collection, $code = 200) {
$collection = $this->resourceData($collection);
$collection = $this->filterData($collection);
$collection = $this->sortData($collection);
$collection = $this->paginate($collection);
//Calling resourceData() method
$resource = $this->resourceData($collection);
$collection = $this->cacheResponse($collection);
return $this->successResponse([$resource::collection($collection), 'code' => $code], $code);
}

How to seed multiple relationships in Laravel with Faker

I have a database with two columns, brands and shops. Each brand can owe several shops, and I want to seed my database via Fakers using Laravel.
So after setting up the migrations and the relationships in the models
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Brand extends Model
{
/**
* Get the shops for the brand.
*/
public function shops()
{
return $this->hasMany('App\Shop','sh_brand_id');
}
}
And:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Shop extends Model
{
public function user() {
return $this->belongsTo('App\Brand','sh_brand_id');
}
}
I want to use a Factory to seed the database.
<?php
use Faker\Generator as Faker;
$factory->define(App\Shop::class, function (Faker $faker) {
return [
'name' => $faker->company,
'address' => $faker->address,
];
});
And
use Faker\Generator as Faker;
$factory->define(App\Brand::class, function (Faker $faker) {
return [
'name' => $faker->company,
'logo_url' => $faker->imageUrl(640, 480),
'website' => $faker->url,
'description' => $faker->text(500),
'telephone_number' =>'31'. $faker->randomNumber(8),
'principal_address' => $faker->address,
'email' => $faker->unique()->safeEmail,
];
});
And finally I need to seed the database using those Factories. There are documentation in the website and many examples for do it, but each solution I've found let me generate only one shop for each brand, and I want to generate many shops for each brands.
What is the best way to do this?
Put it directly in your factory. I use a helper method getInstanceOf to pick a random instance of another model.
use Faker\Generator as Faker;
use App\Brand;
use App\Shop;
function getInstanceOf($class, $returnIdOnly = true) {
$instance = $class::inRandomOrder()->first() ?? factory($class)->create();
return $returnIdOnly ? $instance->id : $instance;
}
$factory->define(Shop::class, function (Faker $faker) {
return [
'name' => $faker->company,
'address' => $faker->address,
'sh_brand_id' => getInstanceOf(Brand::class)
];
});
Then when seeding,
factory(App\Brand::class, 10);
factory(App\Shop::class, 50);
I've found this workaround that works for me:
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* #return void
*/
public function run() {
factory(App\Brand::class, 50)->create()
->each(
function ($br) {
factory(App\Shop::class, 10)->create()
->each(
function($sh) use (&$br) {
$br->shops()->save($sh)->make();
}
);
}
);
}
}

Error on insert data into database using laravel5

I am new in laravel5 Framework. when I insert data into database using laravel5 at that time I get error like....
FatalErrorException in ClientFormRequest.php line 10:
Cannot make static method Symfony\Component\HttpFoundation\Request::create() non static in class App\Http\Requests\ClientFormRequest
my all files are below...
app/Http/Controller/RegisterController.php
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientFormRequest;
use Illuminate\Http\Request;
class RegisterController extends Controller {
public function create()
{
return view('Client.client');
}
public function store(ClientFormRequest $request)
{
return \Redirect::route('Client.client')
->with('message', 'Record Inserted!');
}
}
app/Http/Requests/ClientFormRequest.php
<?php namespace App\Http\Requests;
use Stringy\create;
use App\User;
use Validator;
use App\Http\Requests\ClientFormRequest;
class ClientFormRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
}
public function validator(array $data)
{
return Validator::make($data, [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
]);
}
public function create(array $data)
{
return client::create([
'fullname' => $data['fullname'],
'email' => $data['email'],
]);
}
}
Routes
Route::get('client', 'RegisterController#create');
Route::post('contact_store', 'RegisterController#store');
First of all, i would suggest you to watch Laravel 5 Fundamentals repeatedly since it is free. Other series also give great information.
Secondly, I would suggest you to use at least Sublime Text and some useful packages to be able to inspect the depth nested relations of system files (Namespaces, Interfaces, Inheritance Tree etc...). If you can't/might not, this friend will serve you anytime Laravel API
Third, AFAIK, Laravel Request is build onto the Symfony' Request Component. Since you are trying to overload one of its core function as non static, you are getting this error.
In addition, to be honest, i wouldn't put my user/client model creation logic into the requests. Laravel provides an good example for this kind of misconception. In the App\Services folder, you will find a registrar service for Laravel oem user model.
Let's inspect the problem with different cases.
but first, basic...
Lets assume that all logic should be put inside the controller.
RegisterController.php
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Request;
class RegisterController extends Controller {
public function create()
{
return view('Client.client');
}
public function store()
{
$data = Request::all(); //requested data via Facade
//prepare validatation
$validation = Validator::make($data, [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
]);
//validate
if ($validation->fails())
{
return redirect()->back()->withErrors($v->errors());
}
// create the client
Client::create([
'fullname' => Request::input('fullname'),
'email' => Request::input('email'),
]);
return \Redirect::route('Client.client')
->with('message', 'Record Inserted!');
}
}
Second Solution
You might be willing to separate the validation logic and apply some dependency injection.
RegisterController.php
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientFormRequest;
class RegisterController extends Controller {
public function create()
{
return view('Client.client');
}
public function store(ClientFormRequest $request)
{
// create the client
Client::create([
'fullname' => $request->input('fullname'),
'email' => $request->input('email'),
]);
return \Redirect::route('Client.client')
->with('message', 'Record Inserted!');
}
}
ClientFormRequest.php
use Stringy\create;
use App\User;
use Validator;
use App\Http\Requests\ClientFormRequest;
class ClientFormRequest extends Request {
public function authorize()
{
return true;
}
public function rules()
{
return [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users'
];
}
}
Third Solution
You might be willing to take things further and even separate the object creation logic as an service to use it anywhere. Now your request file would stay the same. However,
RegisterController.php
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientFormRequest;
use App\Services\ClientRegistrar;
class RegisterController extends Controller {
private $registrar;
public function __construct(ClientRegistrar $registrarService)
{
$this->registrar = $registrarService;
}
public function create()
{
return view('Client.client');
}
public function store(ClientFormRequest $request)
{
$newClient = $this->registrar->create($request->all());
return \Redirect::route('Client.client')
->with('message', 'Record Inserted!')->compact('newClient');
}
}
App\Services\ClientRegistrar.php
use App\Client;
use Validator;
use Illuminate\Contracts\Auth\Registrar as RegistrarContract;
class ClientRegistrar implements RegistrarContract {
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
public function validator(array $data)
{
return Validator::make($data, [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
]);
}
/**
* Create a new client instance after a valid registration.
*
* #param array $data
* #return Client
*/
public function create(array $data)
{
// create the client
return Client::create([
'fullname' => $data['fullname'],
'email' => $data['email'],
]);
}
}
To My Conclusion
There is no correct and best way to solve a problem. Stay with the best applicable and appropriate way for you and your project scale.
You also might be interested in;
Jeffrey Way's Laravel Auto Validate on Save
The error message tells you that you are overriding the create method in the ClientFormRequest class. So remove the method there. Instead create the new Client in your Controller.
Below I updated your classes to reflect the changes.
ClientFormRequest
class ClientFormRequest extends Request {
public function authorize()
{
return true;
}
public function rules()
{
}
public function validator(array $data)
{
return Validator::make($data, [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
]);
}
}
RegisterController
class RegisterController extends Controller {
public function create()
{
return view('Client.client');
}
public function store(ClientFormRequest $request)
{
// ClientFormRequest was valid
// create the client
Client::create([
'fullname' => $request->input('fullname'),
'email' => $request->input('email'),
]);
return Redirect::route('Client.client')
->with('message', 'Record Inserted!');
}
}

Categories