FastExcel import additional field data - php

Code
public function import(Request $request)
{
$id= $request->id;
$limit = $request->limit;
$path = $request->file('file')->store('uploads');
$products = (new FastExcel)->import('public/' . $path, function ($line) {
return Data::create([
'uname' => $line['name'],
'username' => $line['username'],
'limit' => $limit,
'group_id' =>$id
]);
});
flash(__('Your products imported successfully'))->success();
return redirect()->route('products.index');
}
Thhis is not woking id and limit not defined
I need solution for this im useing laravel 6.2 with FastExcel

Related

How can I redirect to the certain page when I clicked on my side-nav menu

I create a menu table and menu seeder inside my database. However when I tried to click on the company menu on my website it saying that $menu is undefined but when I tried to change to jobs.index(this the one which is working fine) for my route it works but it will goes to the job.blade.php not company.blade.php I do not know which part is wrong inside my code.
companycontroller.php
class CompanyController extends Controller
{
public function index()
{
$data = Company::latest()->paginate(5);
return view('company.index', compact('data'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
public function insert_logo(Request $request)
{
$request->validate([
'company_logo' => 'required|image|max:2048',
'company_name' => 'required'
]);
$image_file = $request->company_logo;
$image = Company::make($image_file);
Response::make($image->encode('jpeg'));
$form_data = array(
'company_logo' => $image,
'company_name' => $request->company_name,
);
Company::create($form_data);
return redirect()->back();
}
public function fetch_logo($image_id)
{
$image = Company::findOrFail($image_id);
$image_file = Company::make($image->company_logo);
$response = Response::make($image_file->encode('jpeg'));
$response->header('Content-Type', 'image/jpeg');
return $response;
}

insert using laravel 6 not working (function store) Array to string conversion

i tried to insert an annn but it gives me error Array to string conversion.knowing that all the fields are not obligatory plien, because the insertion is done according to the subcategory_id chosen in blade edit
AnnoncesController.php
public function store(Request $request)
{
$request->validate([
'category_id'=>['bail','required',],
'souscategory_id'=>['bail','required',],
'entreprise' =>['bail','exclude_unless:souscategory_id,1','required','string','min:2','max:255'],
'domaine' =>['bail','exclude_unless:souscategory_id,1','required','string'],
'contrat' =>['bail','exclude_unless:souscategory_id,1','required','string',],
'niveaua' =>['bail','exclude_unless:souscategory_id,1','required','string'],
'poste' =>['bail','exclude_unless:souscategory_id,2','required','string'],
'marquem' =>['bail','exclude_unless:souscategory_id,7','required','string'],
'marque' =>['bail','exclude_unless:souscategory_id,8','required','string'],
'modele' =>['bail','exclude_unless:souscategory_id,8','required','string'],
'annee' =>['bail','exclude_unless:souscategory_id,8','required','string'],
'km' =>['bail','exclude_unless:souscategory_id,8','required','numeric'],
'carburant' =>['bail','exclude_unless:souscategory_id,8','required','string','min:2','max:255'],
'puissance' =>['bail','exclude_unless:souscategory_id,8','required','string','min:2','max:255'],
'titre' =>['bail','exclude_unless:souscategory_id,1','required','string','min:2','max:255'],
'description' =>['bail','exclude_unless:souscategory_id,1','required','string','min:30','max:255'],
'prix' =>['bail','exclude_unless:souscategory_id,6','required','min:1','max:12'],
'image' =>['bail','exclude_unless:souscategory_id,1','required','mimes:jpeg,jpg,png,gif,svg','max:2048'],
'images.*' =>['bail','exclude_unless:souscategory_id,1','required','mimes:jpeg,jpg,png,gif,svg','max:2048'],
//2 emploi/
]);
if($request->hasFile('image'))
{
$path = $request->image->store('annonces');
$request->image = $path;
}
$request->user_id = Auth::user()->id;
$Annonce = new Annonce($request->all());
$Annonce->save();
return Redirect::to("annonces")
->withSuccess('Great! file has been successfully uploaded.');
}
to create a new element from $request in Laravel you need to do it as below and assign each value of $request to the each attribute of Announce object:
$request->user_id = Auth::user()->id;
$Annonce = new Annonce();
$Annonce->user_id = $request->user_id;
$Annonce->some_column = $request->some_column;
$Account->save();
You can try by this:
use App\Models\Announce;
............................
$data = $request->all();
if ($request->hasFile('image')) {
$path = $request->image->store('annonces');
$data['image'] = $path;
}
$data['user_id'] = Auth::user()->id;
Announce::create($data);
return Redirect::to("annonces")
->withSuccess('Great! file has been successfully uploaded.');
Make sure you imported the correct model namespace on your app.
although is not recommended to modify a request, you should do it in this way:
$request->request->add(['image' => $path, 'user_id' => Auth::user()->id]);
or by
$request = array_merge($request->all(), ['image' => $path, 'user_id' => Auth::user()->id]));

Upload CSV to SQL using laravel eloquent

I have this problem in uploading my csv to my database (SQL). I am using Maatwebsite... And here's is my controller:
class UploadCSV extends Controller
{
public function store(Request $request){
if($request->hasFile('import_file')){
$path = $request->file('import_file')->getRealPath();
$data = \Excel::load($path)->get();
if($data->count()){
foreach ($data as $key => $value) {
$arr[] = ['s_id' => $value->id,
'school_name' => $value->sname,
'region' => $value->reg,
'province' => $value->prov,
'municipality' => $value->mun,
'division' => $value->div,
'district' => $value->dis,
'enrollment_sy_2014_2015' => $value->enrolled,
'mooe_in_php_for_fy_2015' => $value->mooe,
'latitude' => $value->lat,
'longitude' => $value->lng
];
}
Map::insert($arr);
dd('Insert Record successfully.');
//return json_encode($arr);
}
}
dd('Request data does not have any files to import.');
}
Which gives me this endless error message:
The CSV contains only 200+ rows. Any help would be appreciated. Thanks in advance :))
Maybe try something like this, create new Model (assuming Map is the name of your Model and save():
<?php
class UploadCSV extends Controller
{
public function store(Request $request){
if($request->hasFile('import_file')){
$path = $request->file('import_file')->getRealPath();
$data = \Excel::load($path)->get();
if($data->count()){
foreach ($data as $key => $value) {
$entry = new Map;
$entry->s_id = $value->id;
$entry->school_name = $value->sname;
$entry->region = $value->reg;
$entry->province = $value->prov;
$entry->municipality = $value->mun;
$entry->division = $value->div;
$entry->district = $value->dis;
$entry->enrollment_sy_2014_2015 = $value->enrolled;
$entry->mooe_in_php_for_fy_2015 = $value->mooe;
$entry->latitude = $value->lat;
$entry->longitude = $value->lng;
$entry->save();
}
}
}
dd('Request data does not have any files to import.');
}
}

Codeigniter missing argument

Help guys.
This my error problem error:
I Think i follow right tutorial, but i don't know what the wrong from my code
there is my controllers
public function edit($id){
$where = array('id' => $id);
$data['barang'] = $this->model_barang->edit($where,'barang')->results();
$this->load->view('edit',$data);
}
public function update(){
$id = $this->input->post('id');
$jenis = $this->input->post('jenis');
$nama = $this->input->post('nama');
$harga = $this->input->post('harga');
$pemasok = $this->input->post('pemasok');
$data = array (
'jenis' => $jenis,
'nama'=> $nama,
'harga' => $harga,
'pemasok' => $pemasok
);
$where = array(
'id' => $id
);
$this->model_barang->update($where,$data,'barang');
redirect('barang/tampil');
}
and there is my model, i think my model is not showing any error
public function edit($where,$table) {
return $this->db->get($table,$where);
}
public function update($where,$data,$table){
$this->db->where($where);
$this->db->update($table,$data);
}
You need to pass the $id to the controller.
It should be something like this http://domain.com/barang/edit/1

Laravel 4.2 session::get() method not returning session data in controllers

Hi help me,
login code
public function store()
{
$credentials = array(
'u_email' => Input::get('email'),
'password' => Input::get('password'));
if (Auth::attempt($credentials) ) {
$user = Auth::user()->toArray();
$userrole = with(new User)->get_user_role($user['u_id']);
$userobj['u_id'] = $user['u_id'];
$userobj['u_shortcode'] = $user['u_shortcode'];
$userobj['utype'] = $user['utype'];
$userobj['u_title'] = $user['u_title'];
$userobj['u_fname'] = $user['u_fname'];
$userobj['u_lname'] = $user['u_lname'];
$userobj['u_email'] = $user['u_email'];
$userobj['u_role'] = $userrole;
$userobj['id'] = Session::getId();
Session::put('admin', $userobj);
$value = Session::get('admin');
return Response::json([
'user' => $userobj ],
202
);
}else{
return Response::json([
'flash2' => 'Authentication failed'],
202
);
}
}
and my second controller is:
public function get_sessionobj()
{
var_dump(Session::all());
$value = Session::get('admin');
print_r($value);
exit();
}
when i am calling second controller after login then session data not printed. in login controller Session::get('admin') function returning data. and i am using file driver for session storage. I have seen my session file there was some data like this:
a:5:{s:6:"_token";s:40:"XrUgs7QLPlXvjvyzFaTdmDpqGL0aSZRzkJS0il9f";s:38:"login_82e5d2c56bdd0811318f0cf078b78bfc";s:1:"1";s:5:"admin";a:9:{s:4:"u_id";s:1:"1";s:11:"u_shortcode";s:5:"u1001";s:5:"utype";s:1:"1";s:7:"u_title";s:3:"Mr.";s:7:"u_fname";s:6:"Aristo";s:7:"u_lname";s:5:"Singh";s:7:"u_email";s:24:"chandan.singh#jetwave.in";s:6:"u_role";a:3:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";}s:2:"id";s:40:"cd074f7f61fcc88b3d92c482e57e8a12dc888958";}s:9:"_sf2_meta";a:3:{s:1:"u";i:1410525787;s:1:"c";i:1410525787;s:1:"l";s:1:"0";}s:5:"flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}
Call a function get_sessionobj() in store function
Example:
public function store(){
$this->get_sessionobj();
}

Categories