Eloquent
Let’s first
of all have a look at Eloquent.
Eloquent is used to interact with the database. We have a corresponding model for every table. It allows you to insert data in your table as well as apply any query to that. Before this you must make sure that you have configured your database explained in migration article.
Eloquent is used to interact with the database. We have a corresponding model for every table. It allows you to insert data in your table as well as apply any query to that. Before this you must make sure that you have configured your database explained in migration article.
By default
the models are located in app folder. If you to go to the app folder you will
see a User.php file, this is an already created model. Migration of user and
model for that is already created because user is a part of many projects, you
can just delete it if you don’t need it and make your own model.
Let’s create
our first model, for that go to command prompt then go to the location of your
project e.g. D:\wamp\www\bitspedia and type
php artisan make:model Users
php artisan make:model Users
It will create a model named
as Users in app directory. Now if you open it you will see that a code has been
generated.
You have to tell the model that what
attributes can be changed by user. Like you don’t want your “id” to be changed
by user as it is auto incremented primary key so you will write the all other
attribute except id as written below in the code sample.
Eloquent assume that you have “created_at” and “updated_at” columns in your
table. If you don’t have them set the time-stamp false.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Users extends Model
{
protected $fillable = [
'name', 'subjects','gender', 'date_of_birth', 'phone_no',
];//attributes user can change
namespace App;
use Illuminate\Database\Eloquent\Model;
class Users extends Model
{
protected $fillable = [
'name', 'subjects','gender', 'date_of_birth', 'phone_no',
];//attributes user can change
public $timestamps = false; //table don’t includes “created_at” and
“updated_at”
}
Remember that subjects, gender, date_of_birth and phone_no are the
attributes of my table
users.
users.
Route
Let’s set a
route so that when form is submitted it gets to our method where we will get
and store this data.
Route::post('storeUser','controllerUsers@store');
Controller
When you have created a form and model now you need to get that information from the form and save it in the database for that you have to create a function in your controller
so let’s create a controller named as controllerUsers
When you have created a form and model now you need to get that information from the form and save it in the database for that you have to create a function in your controller
so let’s create a controller named as controllerUsers
Go to
controller folder located in Http folder further located in app folder e.g.
D:\wamp\www\bitspedia\app\Http
Now create a
new php file named as controllerUsers now you must add basics things for
controller i.e.
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Users;
use Request;
use DB;
}
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Users;
use Request;
use DB;
}
class controllerBasics extends
Controller
{
{
public function store()
{
$user = Request::all(); //getting all the data from form
Users::create($user); //storing the data in database
{
$user = Request::all(); //getting all the data from form
Users::create($user); //storing the data in database
$file = Request::file('image'); //temporary path
$destinationPath = 'D:\wamp\www\bitspedia\public\uploads'; // path where want to store
$destinationPath = 'D:\wamp\www\bitspedia\public\uploads'; // path where want to store
$fileName =
$file->getClientOriginalName(); //getting the orginal name
$path=Request::file('image')->move($destinationPath,$fileName);
//moving the image and giving it a name
$lastUser = Users::orderBy('id', 'desc')->first(); //getting the last added user Users::where('id','=', $lastUser->id)->update(['image_path' => $path]);
//update the image path of that user } }
All controllers are stored in App\Http\Controllers and we used DB because we will contact with
database(use DB),
Request will be used to get data from form(use Request) and Users is
a model that we created earlier and it is located in app folder(use
App\Users).
All controllers must extend the base controller.
I have also
created a store method. Its responsibility will be to get data from form and
store it in database. First we received data from form. We fetched all the data from form and stored it in some variable “$user”
Then we
saved the data in our database, here you saw
the benefits of Eloquent. To save the data which we have received and saved in a
variable $user we just used a single line .i.e Users::create($user); this will create a row in database.
Then we received the
temporary path of image. Then
we gave a path where we want our image to be stored. Then we got the original
file name. Then we moved the file to our desired folder
The destination saved in
our database was be temporary location so we felt the need to update it so we
did it by getting the last user added in database and then updating its image
path.
To fetch a
specific data let’s say we want to fetch only name from the form
$user= Request::get('name');
Remember that name is a textfield in my form.
$user= Request::get('name');
Remember that name is a textfield in my form.
You can also use except to get data of all attributes except some of
the attributes
$user = Request::except(['name']);
$user = Request::except(['name']);
Queries
You can also apply any query on it as I mentioned above, let’s apply
some queries
To get all the data from table
$user = Users::get();
$user = Users::get();
To get all the rows where name is equal to junaid
$user = Users::where('name','=', 'junaid')->get();
$user = Users::where('name','=', 'junaid')->get();
To delete all the rows where name is equal to junaid
$user = Users::where('name','=', 'junaid')->delete();
$user = Users::where('name','=', 'junaid')->delete();
To get the first row from table where name is equal to junaid
$user = Users::where('name','=', 'junaid')->first();
$user = Users::where('name','=', 'junaid')->first();
To apply a double where clause
$user = Users::where('first_name', '=', 'junaid')->where('class','=', 'fsc')->get();
To update the row where name is equal to junaid
$user = Users::where('name', 'junaid')->update(['name' => 'asif']);
The name will be updated to asif
$user = Users::where('name', 'junaid')->update(['name' => 'asif']);
The name will be updated to asif
You can fetch data by any order too
$user = Users::orderBy('id', 'desc')->first();
This will fetch data in descending order by id, similarly you can fetch data by ascending too.
$user = Users::orderBy('id', 'desc')->first();
This will fetch data in descending order by id, similarly you can fetch data by ascending too.
