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]));
public function getleaveType(Request $request){
$leave_types = leaveType::all();
//create leave-type
if($request->isMethod('POST')){
$leave_Type = new leaveType;
$leave_Type->leave_type = $request->input('leaveType');
$leave_Type->staff_type = $request->input('staffType');
$leave_Type->leave_days = $request->input('leaveDays');
$leave_Type->leave_description = $request->input('leaveDescription');
$leave_Type->save();
}
return redirect('leave/leaveType', ['leave_t' => $leave_types]);
}
Looks like you are passing data $leave_types to route and not to view.
It should be something like this
return view('view_name', ['leave_t' => $leave_types]);
OR
return view('view_name')->with('leave_t',$leave_types);
Edit:
Try this
public function getleaveType(Request $request){
//create leave-type
if($request->isMethod('POST')){
$leave_Type = new leaveType;
$leave_Type->leave_type = $request->input('leaveType');
$leave_Type->staff_type = $request->input('staffType');
$leave_Type->leave_days = $request->input('leaveDays');
$leave_Type->leave_description = $request->input('leaveDescription');
$leave_Type->save();
}
$leave_types = leaveType::all();
return view('view_name')->with('leave_t',$leave_types);
}
return redirect()->route('leave/leaveType')->with( ['leave_t' => $leave_types]);
Try to use redirect like above and encapsulate the values you want to pass using with function
I am coding in Laravel, How can I pass variable to one function to another function in Controller,
In controller file I have 2 functions like this
public function hiringEmployee(Request $request)
{
$hireEmployee = new EmployeeHire();
$hireEmployee->candidateName = $request->get('candidateName');
$file = $request->file('file');
$name = $file->getClientOriginalName();
$file->move('uploads/cv', $name);
$hireEmployee->file = $name;
$hireEmployee->save();
return redirect('list-candidate');
}
public function assignInterview(Request $request, $id)
{
$assignInterview = EmployeeHire::find($id);
$interview = $request->get('interview');
$assignto = $request->get('assignto');
$dateTime = $request->get('dateTime');
$note = $request->get('note');
$interviewDetails = ([
'interview' => $interview,
'assign_to' => $assignto,
'date_time' => $dateTime,
'note' => $note,
]);
$assignInterview->interview_details = $interviewDetails;
$assignInterview->save();
Mail::send('emails.hireemployee', ['candidateName' => $candidateName], function ($message) use ($assignto, $name) {
$message->subject('Interview For New Candidate!');
$message->from('hrm#wcg.com', 'HRM');
$message->to($mail);
$message->attach('uploads/cv/'.$name);
});
return redirect('list-candidate');
}
I want to use $candidateName and $name in assignInterview() function from hiringEmployee() function.
How can I do it?
You won't be able to use the $name and $candidateName directly from the other function as they look like they are for two different requests, however, it looks like you're saving that data to database when you're creating a new EmployeeHire in your hiringEmployee() method so you should already have access to that information in your assignInterview() method:
$assignInterview = EmployeeHire::find($id); // this is where you loading the model
$candidateName = $assignInterview->candidateName;
$name = $assignInterview->file;
In your situation , you can use two approach:
#1
Use Session Variable as below:
Session::put('candidateName', $candidateName);
Then:
$value = Session::get('candidateName');
#2
Use class attribute:
class acontroller extends Controller
{
private $classCandidateName;
}
You can try something like this:
public function hiringEmployee(Request $request)
{
$hireEmployee = new EmployeeHire();
$hireEmployee->candidateName = $request->get('candidateName');
$file = $request->file('file');
$name = $file->getClientOriginalName();
$file->move('uploads/cv', $name);
$hireEmployee->file = $name;
$hireEmployee->save();
return redirect('list-candidate');
}
public function assignInterview(Request $request, $id)
{
$assignInterview = EmployeeHire::find($id);
if(is_null($assignInterview)){
return redirect()->back()->withErrors(['Error message here']);
}
$interviewDetails = ([
'interview' => $request->get('interview'),
'assign_to' => $request->get('assignto'),
'date_time' => $request->get('dateTime'),
'note' => $request->get('note'),
]);
$assignInterview->interview_details = $interviewDetails;
$assignInterview->save();
Mail::send('emails.hireemployee', ['candidateName' => $assignInterview->candidateName], function ($message) use ($assignto, $assignInterview->file) {
$message->subject('Interview For New Candidate!');
$message->from('hrm#wcg.com', 'HRM');
$message->to($mail);
$message->attach('uploads/cv/'.$assignInterview->file);
});
return redirect('list-candidate');
}
Please, you should to be careful with find($id). If it is a null, you will get an error.
Have fun!
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.');
}
}
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