Use Illuminate\Http\Request;
public function registerAStudent(Request $request)
{
Student::Create($request->all());
}
I want to upload a row to the database table.
Form data is stored in the request variable and using Student model.
but can't update the database I'm getting following error:
ErrorException (E_RECOVERABLE_ERROR) HELP Argument 1 passed to
AdminController::registerAStudent() must be an instance of
Illuminate\Http\Request, none given
please give me a proper way to do it.
Related
It's very strange for me. Because in codeigniter 4 one of my function is working in controller but not working in view. I am getting my data's from database in controller, but when I am trying to get same data from view it showing mysqli_sql_exception #1064.
Example codes:
In Controller (MyData.php)
function get_data($status){
return $this->Data->get_data($status);
}
function view_data(){
return view('user/view-data',['data'=>$this])
}
In Model (MyData.php)
function get_data($status="approved"){
return $this->select()->where('status',$status)->get()->getResultArray();
}
In View (MyData.php)
$datas = $data->get_data('approved');
Error
mysqli_sql_exception #1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*, *, *
I believe your view_data function should be
function view_data(){
return view('user/view-data',['data'=>$this->get_data('approved')])
}
Then in view just loop through the $data array
Views are places that you MUST NOT need to execute sql queries. You need to pass only data (objects, arrays, string, int, etc.) to views.
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.
I'm trying to automatically create a profile for a user when a user is created.
I'm using the created event and overriding the boot() method. But I when call the create() method on user->profile->create(), it says create was called on null. I checked and profile is null in this.
Here's the code:
static::created(function ($user) {
// it returns profile as null, thus create() can't be used on null.
$user->profile->create(['title' => $user->username,]);
});
Can anyone help me understand this? It's working in my tutor's code, and he is using Laravel 5.8 but I have version 7.1.
$user->profile returns the related model if any exists. You have to do $user->profile() which returns a query builder to query the relation. Try to do it like so:
$user->profile()->create(['title' => $user->username,]);
I am trying to save image on database to be used as background for the website. The problem is, I am getting Call to a member function fill() on null
Here is what I did
https://paste.laravel.io/ZoWBM
This simply means there is no any record in SiteSettings model with name_setting = $key.
You should always do a check like this:
if (is_null($siteSettingsUpdate)) {
// There is no such record in DB.
}
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)