Laravel Model and Database migration এর ব্যবহার

Author: Al-mamun Sarkar Date: 2020-06-17 11:26:13

এই লেসনে আমরা ডাটাবেস কনফিগারেশন করা এবং Model and Migration এর বেসিক ব্যবহার শিখবো। Model ব্যবহার করে Database এ Insert, Update, Delete করা শিখবো 

 

Source Code:

Artisan Commands:

php artisan serve
php artisan key:generate
php artisan migrate

 

Routes:

web.php

Route::get( 'about', 'HomeController@index');
Route::get( 'contact', 'ContactController@contact');

Route::get( 'users', 'UserController@index');
Route::get( 'create-user', 'UserController@create');
Route::get( 'users/{id}', 'UserController@show');
Route::get( 'users/{id}/delete', 'UserController@delete');
Route::get( 'users/{id}/edit', 'UserController@edit');

 

Database Section of .env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_course
DB_USERNAME=root
DB_PASSWORD=

 

HomeController.php:

namespace App\Http\Controllers;


class HomeController extends Controller
{
	
	public function index()
	{
		$this->data['name'] = 'Mamun Sarkar';
		$this->data['roll'] = 234;
		$this->data['subject'] = ['Math', 'Eng', 'Bangla'];

		return view( 'home.index', $this->data ); 
	}	

}

 

ContactController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ContactController extends Controller
{
    public function contact()
	{
		return view('contact.contact');
	}
}

 

UserController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
	
	public function index()
	{
		$users = User::all();

		return $users;
	}


	public function show( $id )
	{	
		$user = User::find($id);

		return $user;
	}


    public function create()
    {
    	// Create a user
    	$data = [
    		'name' => 'Jone Doe',
    		'email' => 'jone@gmail.com',
    		'password' => '123456'
    	];

    	User::create($data);

    	return 'Created Successfully';
    }


    public function edit($id)
    {
    	$user = User::findOrFail($id);
    	$user->name = 'New name';
    	$user->email = 'new_email@gmail.com';
    	$user->save();

    	return 'Edited';
    }


    public function delete($id)
    {
    	$user = User::findOrFail($id);
    	$user->delete();

    	return 'Deleted';
    }

}