MEMBUAT CRUD LARAVEL ITU MUDAH
Mumpung hari libur, jadi saya bisa nulis tutorial lagi nih tutorial
CRUD Laravel
Ooiya, untuk instalasi Laravel nya saya skip saja karena sudah pernah saya
bahas di tutor sebelum-sebelumnya buat yang belum tau bisa di cari post nya,
dan sekarang kembali ke project.
setelah project nya jadi,lalu kita tidur melakukan
routing
buat controller
php
artisan make:controller testing --resource
testing adalah nama controller nya ,lalu copy script controller di bawah
ini
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests;
use App\curd;
class testing extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$user = curd::all();
return view(‘read’, compact(‘user’));
}
class testing extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$user = curd::all();
return view(‘read’, compact(‘user’));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view(‘create’);
}
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view(‘create’);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
curd::create(Request::all());
return redirect(‘index’);
}
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
curd::create(Request::all());
return redirect(‘index’);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$user=curd:: find($id);
return view(‘show’, compact(‘user’));
}
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$user=curd:: find($id);
return view(‘show’, compact(‘user’));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$user=curd::find($id);
return view(‘update’, compact(‘user’));
}
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$user=curd::find($id);
return view(‘update’, compact(‘user’));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$user=curd::find($id);
$user->update(Request::all());
return redirect(‘index’);
}
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$user=curd::find($id);
$user->update(Request::all());
return redirect(‘index’);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$user=curd::find($id);
$user->delete();
return redirect(‘index’);
}
}
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$user=curd::find($id);
$user->delete();
return redirect(‘index’);
}
}
setelah itu sambungkan project nya dengan data base nya
disini saya menamakan project nya dengan nama ‘tibmass’
lalu buat model dengan
php
artisan make:model curd
Kalo buat model nya berhasil, akan keluar command line Model
created successfully.lalu kita
php
artisan make:migration create_user_table --create=crud
Kita mau buat table dengan nama users_table, paste commandline ke cmd nya ya :
php
artisan make:migration create_users_table --create=curd
Selanjutnya buka App/curd.php, ikuti seperti ini :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class curd
extends Model
{
protected $fillable = [‘nama’, ’email’, ‘mobile’, ‘password’];
}
{
protected $fillable = [‘nama’, ’email’, ‘mobile’, ‘password’];
}
Lalu ke database/migrations, kita ke users_table ,copy script
dibawah ini:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(‘users’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(‘name’);
$table->string(’email’)->unique();
$table->String(‘avatar’)->default(‘default.jpg’);
$table->string(‘password’);
$table->rememberToken();
$table->timestamps();
});
}
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(‘users’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(‘name’);
$table->string(’email’)->unique();
$table->String(‘avatar’)->default(‘default.jpg’);
$table->string(‘password’);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop(‘users’);
}
}
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop(‘users’);
}
}
jika sudah mari kita migrate sob
php artisan
migrate
jika sudah kita buat view di ‘resources/view dengan nama ‘create.blade.php’,lalu copy
script di bawah ini
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>CREATE</title>
</head>
<body>
<fieldset style=”width:30%”><legend align=”center”>Create User</legend>
<br>
<form action=”store” method=”post”>
<label for=”nama”>nama</label>
<input type=”text” name=”nama”><br/>
<label for=”email”>email</label>
<input type=”text” name=”email”><br/>
<label for=”mobile”>mobile</label>
<input type=”text” name=”mobile”><br/>
<label for=”password”>password</label>
<input type=”password” name=”password”><br/>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>CREATE</title>
</head>
<body>
<fieldset style=”width:30%”><legend align=”center”>Create User</legend>
<br>
<form action=”store” method=”post”>
<label for=”nama”>nama</label>
<input type=”text” name=”nama”><br/>
<label for=”email”>email</label>
<input type=”text” name=”email”><br/>
<label for=”mobile”>mobile</label>
<input type=”text” name=”mobile”><br/>
<label for=”password”>password</label>
<input type=”password” name=”password”><br/>
<input type=”hidden” name=”_token” value=”{{csrf_token()}}”>
<label for=””></label>
<input type=”submit” name=”Submit”>
<a href=”home”>Cancel</a>
</form>
<label for=””></label>
<input type=”submit” name=”Submit”>
<a href=”home”>Cancel</a>
</form>
</body>
</html>
</html>
lalu buat view di resources/view dengan nama read.blade.php copy
script di bawah ini
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>READ</title>
<style>
table,thead,th,tr,td{
border-collapse: collapse;
border: 1px solid black;
padding-left: 1.5em;
}
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>READ</title>
<style>
table,thead,th,tr,td{
border-collapse: collapse;
border: 1px solid black;
padding-left: 1.5em;
}
</style>
</head>
<body>
</head>
<body>
<br><br>
<hr>
<table style=”width:100%”>
<thead>
<th>Id</th>
<th>Nama</th>
<th>Email</th>
<th>Mobile</th>
<th>Password</th>
<th>Show</th>
<th>Update</th>
<th>Delete</th>
<table style=”width:100%”>
<thead>
<th>Id</th>
<th>Nama</th>
<th>Email</th>
<th>Mobile</th>
<th>Password</th>
<th>Show</th>
<th>Update</th>
<th>Delete</th>
</thead>
@foreach($user as$users)
<tr>
<td>{{$users->id}}</td>
<td>{{$users->nama}}</td>
<td>{{$users->email}}</td>
<td>{{$users->mobile}}</td>
<td>{{$users->password}}</td>
<td><a href=”show/{{$users->id}}”>show</a></td>
<td><a href=”edit/{{$users->id}}”>Update</a></td>
<td><a href=”delete/{{$users->id}}”>Delete</a></td>
</tr>
@endforeach
</table>
</body>
</html>
@endsection
@foreach($user as$users)
<tr>
<td>{{$users->id}}</td>
<td>{{$users->nama}}</td>
<td>{{$users->email}}</td>
<td>{{$users->mobile}}</td>
<td>{{$users->password}}</td>
<td><a href=”show/{{$users->id}}”>show</a></td>
<td><a href=”edit/{{$users->id}}”>Update</a></td>
<td><a href=”delete/{{$users->id}}”>Delete</a></td>
</tr>
@endforeach
</table>
</body>
</html>
@endsection
terakhir nih gan ,kita buat view updatenya , di resources/view dengan
nama update.blade.php copy script di bawah ini
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>UPDATE</title>
</head>
<body>
<fieldset style=”width:30%”><legend align=”center”>Update User</legend>
<br>
{{Form::model($user, [‘method’ => ‘patch’, ‘action’ =>[‘testing@update’, $user->id]])}}
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>UPDATE</title>
</head>
<body>
<fieldset style=”width:30%”><legend align=”center”>Update User</legend>
<br>
{{Form::model($user, [‘method’ => ‘patch’, ‘action’ =>[‘testing@update’, $user->id]])}}
{{Form::label(‘name’, ‘name’)}}
{{Form::text(‘name’)}}<br/>
{{Form::text(‘name’)}}<br/>
{{Form::label(’email’, ’email’)}}
{{Form::text(’email’)}}<br/>
{{Form::text(’email’)}}<br/>
{{Form::label(‘mobile’, ‘mobile’)}}
{{Form::text(‘mobile’)}}<br/>
{{Form::text(‘mobile’)}}<br/>
{{Form::label(‘password’, ‘password’)}}
{{Form::text(‘password’)}}<br/>
{{Form::text(‘password’)}}<br/>
{{Form::submit(‘Update’)}}
{{Form::close()}}
</body>
</html>
{{Form::close()}}
</body>
</html>
oke coba sekarang kita running
Sekian dari saya semoga bermanfaat :)







