public function search(Request $request){
$telephone=Souscription::get ( 'q' );
$email=Souscription::get ( 'q1' );
if(isset($telephone) AND isset($email)){
$fouilles = Souscription::where('telephone', 'LIKE','%'. $search.'%')->where('email', 'LIKE','%'.$search.'%')->get(); }
return view('rechercher', compact('souscription',compact('telephone','email')));
}
you can do something like this
public function search(Request $request){
$input = $request->all();
$telephone = $input['q'];
$email = $input['q1'];
$searchdb = Souscription::select(['col1','col2'])->orderBy('id','desc');
if(isset($telephone) AND isset($email)) {
$searchdb->where('telephone',$input['telephone']);
$searchdb->where('email',$input['email']);
}
$fouilles = $searchdb->get();
return view('rechercher', compact('souscription',compact('telephone','email', 'fouilles ')));
}
Related
I created a multistep form but I didn't use Ajax in it because I have no idea about Ajax. I have had to code a lot in my controller and routes for the way I created it which is probably not optimized. I'm new to Laravel so I can't find any solution.
How can I do this with ajax?
Controller
public function InitialCreateReport(Request $request){
$request->session()->forget('report');
$data['fiscalyear'] = FiscalYear::all();
$data['month'] = Months::all();
$data['report_type'] = ReportType::all();
$data['report'] = $request->session()->get('report');
return view('adc.reports.create-report', $data);
}
public function PostInitialCreateReport(Request $request){
$validatedData = $request->validate([
'fiscalyear' => 'required',
'month' => 'required',
'report_type' => 'required',
]);
if (empty($request->session()->get('report'))) {
$report = new Report();
$report->fill($validatedData);
$request->session()->put('report', $report);
} else {
$report = $request->session()->get('report');
$report->fill($validatedData);
$report->session()->put('report', $report);
}
return redirect()->route('adc.create.rent.certificate');
}
public function CreateRentCertificateReport(Request $request)
{
$data['report'] = $request->session()->get('report');
$data['reports'] = Report::distric()->status(1)->get();
return view('adc.reports.start-create-report', $data);
}
public function PostCreateRentCertificateReport(Request $request)
{
$report = $request->session()->get('report');
$request->session()->put('report', $report);
return redirect()->route('adc.preview.rent.certificate.report');
}
public function PreviewRentCertificateReport(Request $request){
$report = $request->session()->get('report');
return view('adc.reports.preview-rent-certificate-report', compact('report', $report));
}
public function PostPreviewRentCertificateReport(Request $request){
$report = $request->session()->get('report');
return redirect()->route('adc.save.rent.certificate.report');
}
public function SaveRentCertificateReport(Request $request){
$report = $request->session()->get('report');
return view('adc.reports.save-rent-certificate-report', compact('report', $report));
}
public function PostSaveRentCertificateReport(Request $request)
{
$report = $request->session()->get('report');
$reports = new Report;
$reports->column_one = $report->sum('column_one');
$reports->column_two = $report->sum('column_two');
$reports->fiscal_year = $report->fiscalyear;
$reports->month = $report->month;
$reports->report_type = $report->report_type;
$reports->save();
$notification = array(
'message' => 'Report Created Successfully',
'alert-type' => 'success'
);
return redirect()->route('adc.pending.report')->with($notification);
}
Route
Route::get('/initial/create/report', [AdcController::class,'InitialCreateReport'])->name('inital.create.report');
Route::post('/initial/create/report', [AdcController::class,'PostInitialCreateReport'])->name('inital.create.report.post');
Route::get('/create/rent/certificate/report', [AdcController::class, 'CreateRentCertificateReport'])->name('create.rent.certificate');
Route::post('/create/rent/certificate/report', [AdcController::class, 'PostCreateRentCertificateReport'])->name('create.rent.certificate.report.post');
Route::get('/preview/rent/certificate/report', [AdcController::class, 'PreviewRentCertificateReport'])->name('preview.rent.certificate.report');
Route::post('/preview/rent/certificate/report', [AdcController::class, 'PostPreviewRentCertificateReport'])->name('preview.rent.certificate.report.post');
Route::get('/save/rent/certificate/report', [AdcController::class, 'SaveRentCertificateReport'])->name('save.rent.certificate.report');
Route::post('/save/rent/certificate/report', [AdcController::class, 'PostSaveRentCertificateReport'])->name('save.rent.certificate.report.post');
I'm setting up a CRUD, similar to an ecommerce
I uploaded the image, it is stored in the public folder. The name of the image is replaced by the value of an input that the user places. This same value is saved in the database with the image extension.
I was also able to delete the image, but I don't know how to update it. Can someone please give me a light?
project controller:
public function store(Request $request)
{
$nameFile = $request->input('imageName', '');
if($request->file('imageFile')->isValid()){
$nameFile .= '.' . $request->file('imageFile')->getClientOriginalExtension();
$request->file('imageFile')->storeAs('projects', $nameFile);
}
$project = new Project();
$project->name = $request->input('name');
$project->price = $request->input('price');
$project->imageName = $nameFile;
$project->save();
return redirect()->route('ProjectControllerCreate');
}
public function destroy($id)
{
$project = Project::find($id);
// Image
$filePathName = public_path().'/storage/projects/'. $project->imageName;
if( file_exists($filePathName) ){
unlink($filePathName);
}
// Data
if(!$project)
return redirect()->back();
$project->delete();
return redirect()->route('ProjectControllerCreate');
}
public function update(Request $request, $id)
{
$project = Project::find($id);
if(!$project)
return redirect()->back();
$project->update($request->all());
return redirect()->route('ProjectControllerCreate');
}
This is how you can update :
use Illuminate\Support\Facades\Storage;
public function update(Request $request, $id)
{
$project = Project::find($id);
if(!$project)
return redirect()->back();
// Update new image
if($request->file('imageFile')->isValid()){
// Delete old image
$old_image = $project->imageName;
Storage::delete($old_image);
$nameFile = $request->input('imageName', '');
$nameFile .= '.' . $request->file('imageFile')->getClientOriginalExtension();
$request->file('imageFile')->storeAs('projects', $nameFile);
$project->imageName = $nameFile;
}
$project->name = $request->input('name');
$project->price = $request->input('price');
$project->save();
return redirect()->route('ProjectControllerCreate');
}
public function update(Request $request, $id)
{
$house = House::find($id);
$house->name = $request->name;
$house->code = $request->code;
$house->description = $request->description;
$house->address = $request->address;
$house->owner_name = $request->owner_name;
$house->manager_name = $request->manager_name;
if($house->update()){
return redirect()->route('houses')
->with('alert.status', 'success')
->with('alert.message', 'Successfully Updated.');
}
else{
return redirect()->route('houses')
->with('alert.status', 'danger')
->with('alert.message', 'Try Again.');
}
}
Here i want to update all my fields except code when the code already exists in the database.How can i do that please give some solution.Thank you in advance.
You can use the null coalescing operator, available in php version 7.
In short, you can do this:
public function update(Request $request, $id)
{
$house = House::find($id);
$house->code = $house->code ?? $request->code;
$house->save();
}
If $house->code is different from null, this will be used if it will not use $request->code.
https://www.tutorialspoint.com/php7/php7_coalescing_operator.htm
do this way with ternary condition :
public function update(Request $request, $id)
{
$house = House::find($id);
$house->code = !empty($house->code) && $house->code!= NULL ? $house->code : $request->code;
}
Use a if :
public function update(Request $request, $id)
{
$house = House::find($id);
$house->name = $request->name;
if($house->code == NULL)
$house->code = $request->code;
$house->description = $request->description;
$house->address = $request->address;
$house->owner_name = $request->owner_name;
$house->manager_name = $request->manager_name;
$house->save();
if($house->update()){
return redirect()->route('houses')
->with('alert.status', 'success')
->with('alert.message', 'Successfully Updated.');
}
else{
return redirect()->route('houses')
->with('alert.status', 'danger')
->with('alert.message', 'Try Again.');
}
}
You should use the save() method after you made all this changes.
You can check for code existence on database by Eloquent: Mutators like below on your House Model
class House extends Model
{
//your codes
public function setCodeAtrribute($value)
{
if ($this->attributes["code"] != null) $this->attributes["code"] = $value
}
}
You can also check here for more information
When trying to filter with only username or email code works fine.But when i am trying to filter with both email and username it returns empty.what i am missing
User model
public function scopeEmail($query, $email)
{
$query->where('email','=', $email);
}
public function scopeUsername($query, $username)
{
$query->where('username','=', $username);
}
Controller:
public function filter(Request $request)
{
$q = User::query();
$email = $request->input('email');
$username= $request->input('username');
if (isset($email))
{
// simple where here or another scope, whatever you like
$q->Email($request->input('email'));
}
if (isset($username))
{
$q->Username($request->input('username'));
}
//execute
$results = $q->get();
return response()->json(['issError'=>0, 'errorCode'=>0,'message'=>$results],200);
}
Use When to make filter easy:
public function filter(Request $request)
{
$q = User::query();
$email = $request->input('email');
$username= $request->input('username');
$q->when($email,function ($query){
$query->where('email',$email);
});
$q->when($username,function ($query){
$query->where('username',$username);
});
$results = $q->get();
return response()->json(['issError'=>0, 'errorCode'=>0,'message'=>$results],200);
}
Use simple where in if statements
public function filter(Request $request)
{
$q = User::query();
$email = $request->input('email');
$username= $request->input('username');
if (isset($email))
{
// simple where here or another scope, whatever you like
$q = $q->where('email', $request->input('email'));
}
if (isset($username))
{
$q = $q->where('username', $request->input('username'));
}
//execute
$results = $q->get();
return response()->json(['issError'=>0, 'errorCode'=>0,'message'=>$results],200);
}
Try this
public function filter(Request $request)
{
$q = User::query();
$email = $request->input('email');
$username= $request->input('username');
if (!is_null($email))
{
$q = $q->where('email', $email);
}
if (!is_null($username))
{
$q = $q->where('username', $username));
}
$results = $q->get();
return response()->json(['issError'=>0, 'errorCode'=>0,'message'=>$results],200);
}
Ok I want to pass two variables from a controller to a model but I get some kind of error. Am I passing variables on right way? My syntax is:
Controller:
public function add_tag(){
if(isset($_POST['id_slike']) && isset($_POST['id_taga'])){
$slika = $_POST['id_slike'];
$tag = $_POST['id_taga'];
$this->load->model("Member_model");
$res = $this->Member_model->add_tags($slike, $tag);
foreach ($res->result() as $r){
echo $r->name;
}
}
else{
echo "";
}
}
Model:
public function add_tags(){
$data = array(
'tags_id' => $tag ,
'photos_id' => $slika
);
$check = $this->db->query("SELECT tags_id,photos_id FROM bridge WHERE bridge.tags_id='{$tag}' AND bridge.photos_id={$slika} ");
if($check->num_rows()==0){
$this->db->insert('bridge',$data);
$res = $this->db->query("SELECT name FROM tags where `tags`.`id`='{$tag}' ");
return $res;
}
}
you are passing variables correctly, but do not get them correctly in the model, which should look like this:
public function add_tags($slike, $tag){
//your other code
}
The following code write on the controller file:-
$data = array();
$this->load->model('dbmodel');
$data['item'] = $this->dbmodel->getData('*','catagory',array('cat_id'=>21));
$this->load->view('listing_view', $data);
The following code write on the dbmodel file:-
public function getData($cols, $table, $where=array()){
$this->db->select($cols);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;}