How to pass variable from one method to another in Laravel - php

Please, I need help, I know that this is basic oop but I'm still learning...
public function confirm()
{
$q = Input::get('track');
$confirm = Tracking::where(function ($query) use ($q) {
$query->where('tracking_no', 'like', $q );
})->first();
if ($confirm) {
return Redirect::to('progress');
} else if(!$confirm){
return Redirect::to('invalid-tracking-number');
}
}
public function invalid($q)
{
$data = $this->confirm($q);
return view('frontend.invalid');
}
I'm to pass the input field from confirm() to invalid() but is not working... I need the $q inside my invalid()
so that I pass it to view...

I have fixed it....I reference another method using
if($confirm->isNotEmpty())
{
return (a valid page)
} else {
return $this->invalid();
}
with is, I'm able to return all the input data to the view of invalid

Related

Call to a member function get() on null - LARAVEL

Seeking for your help again. Just wondering for any idea's to return something if the column is NULL. Please see below codes: Thank you!
Application Controller
public function index()
{
$user_id = Auth::user()->id;
$applications = application::where('user_id', '=', $user_id)->first()->get();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}
If user is not authenticated then you will get unexpected error. So first check the auth as Auth::check()
Try this :
use Illuminate\Support\Facades\Auth;
public function index()
{
if(!Auth::check()) {
return view('login');
}
$applications = Application::where('user_id', $Auth::user()->id)->first();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}

How to use Get function as object

Im making a login page , so i need to compare the passoword form DB and the on form form , whats wrong with my code , it says Trying to get property of non-object
Using laravel
public function index()
{
// $mahasiswa = DB::table('mahasiswa')-> get();
return view('index');
}
public function cek(Request $request)
{
$user = DB::table('mahasiswa')->where('npm', $request->npm)->first();
if ($user->Password == $request->password) {
redirect('welcome');
} else {
return ('eror');
}
}
I expect its all right to write like that
you can easily use
Auth::attempt
to check user credentials and this is the correct way to do what you want
or
use this code
if (Hash::check($request->password, $user->password)) {
// true...
}
and the error Trying to get property of non-object
as you write in your code
if($user->Password == $request->password){
redirect('welcome');
}
the problem with Password you write p as a capital not small
I hope my answer help you!
try this, if the error persist, check your database password field if its getting the field. $user->password;
public function index()
{
// $mahasiswa = DB::table('mahasiswa')-> get();
return view('index');
}
public function cek(Request $request)
{
$user = DB::table('mahasiswa')->where('npm', $request->input('npm'))->first();
if ($user->Password == $request->input('password')) {
redirect('welcome');
} else {
return ('erorr');
}
}

Error:You must use the "set" method to update an entry

I am using Codeigniter as my PHP framework, and I keep getting this error when I submit my data to my database.
I am already using set in the controller. I am not sure why I have this error:
You must use the “set” method to update an entry
My model:
public function update_reserve($data,$book_id)
{
$this->db->where('book_id',$book_id);
$this->db->update('books',$data);
return TRUE;
}
My controller:
public function out()
{
$book_id = $this->input->post('book_id');
if(isset($_POST['btn_reserve'])) {
$data = array(
'pickup' =>$this->input->post('pickup'),
'return' =>$this->input->post('return'),);
if(!empty($data)) {
$this->user_model->update_reserve($book_id,$data);
}
}
}
In model, You can use "set" method:
public function update_reserve($data,$book_id)
{
$this->db->where('book_id',$book_id);
$this->db->set($data);
$this->db->update('books');
return TRUE;
}
I think you should call your method like this
$this->user_model->update_reserve($data,$book_id);
to see result

What will the best solution for this multiple optional filter?

These are all optional fields, so will I have to write multiple queries with conditions or is there any way to handle this using Laravel? What will be the query looks like?
Thanks
It depends a bit on how the filters are submitted, but you can do one of the following two things (and probably a gazillion more...):
public function listCars(Request $request)
{
$cars = Car::when($request->get('make'), function ($query, $make) {
$query->where('make', $make);
})
->when($request->get('model'), function ($query, $model) {
$query->where('model', $model);
})
->...
->get();
// do what you have to do
}
So you are basically wrapping your query builder calls in when($value, $callback), which will only execute $callback if $value evaluates to true. When you retrieve a not set parameter with $request->get('parameter'), it will return null and the callback is not executed. But be careful, if $value is 0 it will also not execute the callback. So be sure you don't have this as an index.
As alternative to this, you can also do the same thing but with a bit less eloquent expressions...
public function listCars(Request $request)
{
$query = Car::query();
if($request->filled('make')) {
$query->where('make', $request->get('make'));
}
if($request->filled('model')) {
$query->where('model', $request->get('model'));
}
// some more filtering, sorting, ... here
$cars = $query->get();
// do what you have to do
}
Here is a working example of something similar query i have in my app.
$filters = $vehicle->newQuery();
if (!empty($request->make)) {
$filters->where('make_id', $request->make);
}
if (!empty($request->carmodel)) {
$filters->where('carmodel_di', $request->carmodel);
}
if (!empty($request->year)) {
$filters->where('year_id', $request->year);
}
if (!empty($request->engine)) {
$filters->where('engine_id', $request->engine);
}
if (!empty($request->price)) {
$filters->where('price_id', $request->price);
}
$cars = $filters->latest()->paginate(50);
and now push the $cars variable to view. I hope this works for you or atleast gives you an idea on how to proceed
here is a simple way, you can also make the joins conditional inside the ->when() condition, if you are in Laravel version > 5.4, use $request>filled() instead of $request->has()
public function listCars(Request $request)
{
$cars = Car::when($request->has('make'), function ($query)use($request) {
$query->join('maker','car.makerId','=','maker.id')
->where('make', $request->input('make'));
})
->when($request->has('model'), function ($query)use($request) {
$query->where('model', $request->input('model'));
})
->...
->get();
// you can even make the join conditionaly,
}
$fiterItem = ['make','model','year','engine','price'];
$filters = $vehicle->newQuery();
foreach ($filter as $item) {
if ($r->filled($item)) {
$list->where($item, $r->query($item));
}
}
$list = $filters->paginate(20);

Calling function to save and redirect route: Laravel 5.2

I have following functions in Controller.
public function UpdateCountry(\App\Http\Requests\CountryRequest $request) {
$this->SaveChanges($request);
}
private function SaveChanges($request) {
if($request['CountryID'] == 0) {
$Country = new \App\Models\CountryModel();
}
else {
$Country = \App\Models\CountryModel
::where('CountryID', $request['CountryID'])->first();
}
$Country->Country = $request['Country'];
$Country->CountryCode = $request['CountryCode'];
$Country->save();
return redirect()->route('AllCountries');
}
public function AllCountries() {
$Countries = \App\Models\CountryModel::all();
return view('Country.List', array('Countries' => $Countries));
}
Issue is in below line: When I call function SaveChanges, I am not able to see List of countries page and when I write the code directly in UpdateCountry function, it redirect route successfully.
return redirect()->route('AllCountries');
Anybody faced this issue before ?
Your route is being handled by the UpdateCountry function. Laravel will take action based on the returned value from this function. However, you're not returning anything from this function.
You call SaveChanges, which returns a Redirect object, but then you don't return anything from your UpdateCountry function. You need that Redirect object from the UpdateCountry function in order for Laravel to actually return the redirect to the client.
Update your UpdateCountry function to this:
// added the return statement, so this function will return the redirect
// to the route handler.
public function UpdateCountry(\App\Http\Requests\CountryRequest $request) {
return $this->SaveChanges($request);
}
Maybe you missed a return in $this->SaveChanges($request). It has to be:
public function UpdateCountry(\App\Http\Requests\CountryRequest $request) {
return $this->SaveChanges($request);
}
I hope it works fine for you.

Categories