Call to undefined method My_model::remove_user() Codeigniter - codeigniter

So I am trying to create a function that allows a user to delete their account: hence I want the function to delete all data in the database related to their profile, etc.
In view I have this button:
Delete Account
Which lead to my controller "Profiles" which has the following code:
function remove_user()
{
$this->load->model("profiles_model");
$email = $this->session->userdata('email');
$this->profiles_model->remove_user($email);
$this->load->view('templates/header');
$this->load->view('pages/homepage');
$this->load->view('templates/footer');
}
Which calls my model Profiles_model which has the following code:
function remove_user($email)
{
$this->db->where('email', $email);
$this->db->delete('profiles');
}
The table where the data is stored is "profiles".
I am also using sessions to pull the user's email.
When I execute, I get this error message: "Message: Call to undefined method Profiles_model::remove_user()"
I dont understand, as I use almost identical code to "Update" user data, and it works perfectly.

So I figured it out...my closing "}" tag on my model was ABOVE my function I was trying to call!!!!!!!!! There is 4 days wasted I will never get back again.

Related

How can I get data from multiple controllers into a Laravel Blade view?

I'm new with Laravel (and programming) and I'm a bit confused.
Using the Eloquent ORM I store in the database a list of pets and their info in a pets table with a Pet model and a Pet controller.
I also have a users table with User model.
Then I have an owner_pet table that contains foreing keys to user_id and pet_id, with an OwnerPet model and controller.
I have a view called 'pets' and I want there to be a form and a list of the user pets, only accesible by authenticated users.
I have this route:
Route::get('/pets', [App\Http\Controllers\HomeController::class, 'index'])->name('pets');
There, using the auth middleware it checks the user is logged in and returns the view 'pets', that contains a form to add pets.
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('pets');
}
This works fine.
Then I have a pet controller with an add pet function, I use it with a form in the 'pets' view that calls this route:
Route::post('/add', 'PetController#addPet')->name('add');
When the pet is added I return the 'pets' view with a success message.
return view('pets')->with('success', $success);
This works fine.
Then I have a ownerPets index function where I get all the info I need and store it as $pets and call the 'list' view:
public function index()
{
$ownerPets = OwnerPet::leftjoin('pets', 'pets.id', '=', 'owner_pets.pet_id')
->where('user_id', Auth::user()->id)->paginate(20);
return view('list')->with('pets', $pets);
}
I call the function with this route:
Route::get('/list', 'OwnerPetController#index')->name('list');
This works fine, I can see the list of the user's pets.
But I need to have everything in the same view, and I can only call a single function from the route.
Do I need to make another controller and call all the functions from there and then send all the data to the view? I was told calling a controller from another controller was a bad practice, but I can't think of another way to do this.
Can I maybe make a view that contains the other 2 views? But I don't know how would I get the functions to pass the data to them.
Please I need some advice. Thanks.
This mean that you want a page which have add form at the right side and list of pet with owner information at the left (for example).
so, you just
This Route::get('/list', 'OwnerPetController#index')->name('list'); will return list pet and add from for the same page.
Create another route like Route::post('/add', 'PetController#addPet')->name('add'); (in the same controller or other controller, it up to you) for adding pet.
after you fill up the form and click submit, addPet function must return view('pets');
Hope martch your question.
Thanks

Issue using View()->Composer() in AppServiceProvider

I am trying to pass data to a view using the boot method in the AppServiceProvider, and have followed the steps outlined in one of the Laracast fundamentals videos. However, I am getting an error in my controller when trying to use this variable.
In my AppServiceProvider:
public function boot()
{
view()->composer('home', function($view){
$view->with('current', Post::orderBy('created_at', 'desc')->first());
});
}
So this should pass the data to my home view as far as I understand, so that I don't need to keep referencing it in all the methods I have in the controller for that view. I then have a homeController with the following method:
public function index()
{
//Get user
$user = Auth::user();
//OMDB API setup - Get Movie name, remove spaces, add into API request
$movie_one = str_replace(" ", "+", $current->movie_one);
$movie_two = str_replace(" ", "+", $current->movie_two);
return view('home', compact(array('user')));
}
Previously I was adding a variable called current in this method to get an entry from my Post table, but wanted to add it to the AppServiceProvider as I will have to re-declare it in other methods as well. The issue I have is that I try to use the $current object again in this method, but it isn't available to it? I get the following error:
ErrorException in homeController.php line 30:
Undefined variable: current
What can I do here? Can I pass that data from to a controller as well as the home view? Or is that not possible?
the code you written inside the boot method, will only be accessible inside home view, if you want to access the Post object inside controller too, you have to follow this, through this you can access your Post object in controller, as will as view.
app()->singleton('current',function($app){
return Post::orderBy('created_at', 'desc')->first();
});
So in your code, you can access the Post object like this,
$post_obj = App::make('current');
$post_obj->movie_two;
or directly
App::make('current')->movie_two;

Why can't this method access the session?

I have a session containing some data that I access throughout several methods in my controller with no trouble. But one method in particular has problems.
I have made a little booking system. On completion of a booking, I call a function to send an e-mail to myself and the customer:
public function complete(Request $request)
{
$details = Session::get('details');
// send emails: call sendConfirmationEmail(to address, using this view)
$this->sendConfirmationEmail(env('EMAIL'), 'emails.ourconfirmation');
$this->sendConfirmationEmail($details->booker_email, 'emails.bookerconfirmation');
return view('booking/complete');
}
private function sendConfirmationEmail($to,$view)
{
$from = env('OFFICE_EMAIL');
$to_address = $to;
$details = Session::get('details');
$instance = Session::get('instance');
Mail::queue($view, compact(['details','instance']), function($message) use ($to){
$message->from(env('OFFICE_EMAIL'))
->to($to)
->subject('Thanks for booking');
});
}
First I got an error accessing a non-object in my e-mail view. So to test it I just set the sendConfirmationEmail function to return $instance - blank page. Then I tested it by commenting out the function call in complete() and returning $instance there. No problem, there's a nice shiny session full of data. Then I tried passing $instance from complete() to sendConfirmationEmail() and returning it: again, blank page. Why can't sendConfirmationEmail 'see' my session?!
To my knowledge, Sessions are for using across HTTP requests, not across functions in PHP. You are trying the use Sessions as global variables.
You can simply capture details and instance session data in the complete function, and then pass them along as parameters for the sendConfirmationEmail() function.

Trying to get property of non-object error occurs in my controller

Was trying to call orders made by a particular user
from my database but get error from my controller reporting from controller..
Public function getYourOrders {
$order = Order::where('user_id','=',Auth::user()->id)->get();
return View::make('orders.show')->with('order',$order);
}
This is where the errorexception is reporting from
If you don't have a logged in user, Auth::user()->id would give you that error.
If you work with framework version 4.1.26+ don't use Auth::user()->id to retrieve user's id.
Instead use id method:
Auth::id();
it will take care of returning null if there's no logged in user and it will not execute unnecessary DB query (when using DB driver)

View Same user_id

//Anyone can help to create a view data with same id? it is a multiple viewing.
this is my Controller. i dont khow apply in Model and View
function Get_Pitch($id){
$this->load->model('users_model');
$data['query'] = $id;
$this->load->view('view_pitch', $data);
}
Example this is my url "http://localhost/SMS_System/home/sample/102"
in my database is
id=1 name=erwin user_id=102
id=2 name=flores user_id=102
id=3 name=sample user_id=202
how to view the same user_id?
First of all with what you've supplied your URL won't work, you aren't following the normal conventions for CI so it won't know where to look. I am assuming your controller is called sample then you need to tell the application which function you're calling in that controller, finally URL names should be lower case so I changed that, so your URL should read:
"http://localhost/SMS_System/home/sample/get_pitch/102"
Also you need to get your data from a model, you loaded the model then didn't use it. The line after loading the model calls a function from that model and passes it the id you got from your url. Notice the if not isset on the id, this ensures that if someone goes to that page without the id segment there are no errors thrown from the model having a missing parameter, it will just return nothing, that is handled in the view.
Controller:
function get_pitch($id){
//the following line gets the id based on the segment it's in in the URL
$id=$this->uri_segment(3);
if(!isset($id))
{
$id = 0;
}
$this->load->model('users_model');
$data['query'] = $this->users_model->getUserData($id);
$this->load->view('view_pitch', $data);
}
Your model takes the id passed from the controller and uses that to retrieve the data from the database. I normally create the array I am going to return as an empty array and handle that in the view, this makes sure you get no errors if the query fails. The data then returns to the controller in the last line and is passed to the view in your load view call.
Model:
function getUserData($id)
{
$this->db->where('id',$id);
$result = $this->db->get('users') //assuming the table is named users
$data = array(); //create empty array so we aren't returning nothing if the query fails
if ($result->num_rows()==1) //only return data if we get only one result
{
$data = $result->result_array();
}
return $data;
}
Your view then takes the data it received from the model via the controller and displays it if present, if the data is not present it displays an error stating the user does not exist.
View:
if(isset($query['id']))
{
echo $query['id']; //the variable is the array we created inside the $data variable in the controller.
echo $query['name'];
echo $query['user_id'];
} else {
echo 'That user does not exist';
}

Resources