Vous êtes sur la page 1sur 9

Related Things Laravel

Connecting to the Database


Step 1: Open .env file

Step 2: Edit the following lines:

DB_CONNECTION=mysql DB_CONNECTION=mysql

DB_HOST=127.0.0.1 DB_HOST=127.0.0.1

DB_PORT=3306 DB_PORT=3306

DB_DATABASE=homestead DB_DATABASE=haranahtours

DB_USERNAME=homestead DB_USERNAME=root

DB_PASSWORD=secret DB_PASSWORD=null
Step 3: Go to config  database.php

Step 4: Edit the following lines:


Querying from the Database (Reference: https://laravel.com/docs/5.5/queries)
Query ALL
Equivalent: SELECT * FROM ‘tablename’

Code: $variablename = DB::table(‘tablename’)->get();

foreach($variablename as $newname) {
#code
}

Example:

Column Name

Query Select
Equivalent: SELECT columnone, columntwo FROM ‘tablename’

Code:

$variablename = DB::table(‘tablename’)
->select(‘columnone’, ‘columntwo’)->get();

Query Where
Equivalent: SELECT * FROM ‘tablename’ WHERE column_name = ‘condition’

Code:

$variablename = DB::table(‘tablename’)->where(‘column_name’, ‘condition’);

Query multiple Where


Equivalent: SELECT * FROM ‘tablename’ WHERE column_one = ‘condition’ AND
column_two > ‘value’
Code:

$variablename = DB::table(‘tablename’)->where([
[‘column_one’, ‘=’, ‘condition’],
[‘column_two’, ‘>’, ‘value’]
]);

Query with other methods


Equivalent: SELECT DISTINCT column_name FROM ‘tablename’ WHERE column_one =
‘condition’ GROUP BY column_name ORDER BY column_name ASC LIMIT 10, 5
Code:

$variablename = DB::table(‘tablename’)
->select(‘column_name’)
->distinct()
->where(‘column_one’, ‘condition’)
->groupBy(‘column_name’)
->orderBy(‘column_name’, ‘asc’)
->skip(10)
->take(5)
->get();

Query Join
Equivalent: SELECT * FROM ‘table_one’ JOIN ‘table_tow’ USING (‘column_id’)

Code:

$variablename = DB::table(‘table_one’)
->join(‘table_two’, ‘table_one.id’, ‘=’, ‘table_two.id’)
->get();
Query Insert
Equivalent: ISNERT INTO tablename (column_one, column_two) VALUES (‘value_one’,
‘value_two’)
Code:
DB::table(‘tablename’)->insert([
[‘column_one’ => ’value_one’, ‘column_two’ => ‘value_two’]
]);

Query Update

Equivalent: UPDATE tablename SET column_name = ‘new_value’ WHERE column_name


= ‘condition’
Code:
DB::table(‘tablename’)
->where(‘column_name’, ‘condition’)
->update([‘column_name’ => ‘new_value’]);
Views (Reference: https://laravel.com/docs/5.5/views)
WHAT IS A VIEW?

A “View” is a page in the website. This is where you construct your website.

WHERE TO FIND THE VIEW?

Go to haranahtours  resources  views

They are typically named as pagename.blade.php

HOW TO CREATE A VIEW?

When saving, name the page as pagename.blade.php

HOW TO ACCESS THE VIEW IN THE BROWSER?

You can access it with the use of ROUTING (explained on next page)
Routing (Reference: https://laravel.com/docs/5.5/routing)
WHAT IS ROUTING?

Routing is used to redirect pages. You may also use it for you HTTP methods

WHERE CAN I FIND IT?

Go to haranahtours  routes

HOW DO I USE IT?

web.php

SIMPLY VIEWING THE PAGE

You can redirect your pages to your views.

STEP 1: Open the web.php in a text editor

STEP 2: Add the code

Route::view(‘/uri_path’, ‘folder.page_name.blade.php’,
[‘variablename’ => ‘variabe_value’]);
Example:

I have a management.blade.php page

To access the page, edit the web.php

Then go to the URL in the browser: http://localhost/haranahtours/public/management


GET METHOD

web.php

management.blade.php

http://localhost/haranahtours/public/management/2

Vous aimerez peut-être aussi