https://thewebtier.com/laravel/understanding-roles-permissions-laravel/
Элементы форм
Модель
Модель = сущность . за счет этого мы отходим от конкрентой БД. Нужна для работы с физическими данными.
Методы работы с данными
# use Illuminate\Support\Facades\DB;
$posts = DB::table('posts')->orderBy('id', 'desc')->get() ;
#
Post::orderBy('id', 'desc')->get() ;
#save
$p = new Post($request->all());
$p->save();
#update
$post->update($request->all());
#delete
$post->delete();
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//
protected $fillable = ["text"];
protected $guarded = ["id"];
}
Миграции
Нужны чтобы создать таблицы физически на компьютере.
# .env надо настроить сначала
php artisan migrate
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string("title");
$table->string("slug");
$table->text("body")->nullable();
$table->integer("category_id")->nullable();
$table->timestamps();
});
}
Контроллер
#
return view("posts.create", array("entity"=> $post ) ) ;
#
return redirect()->route("post.index")
<?php
namespace App\Http\Controllers;
use App\Post;
use App\Category;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function index()
{
$posts = Post::all();
return view('admin.posts.index', compact('posts'));
}
public function create()
{
$categories = Category::all();
return view('admin.posts.form', compact('categories'));
}
public function store(Request $request)
{
$this->validate($request, [
'slug' => 'required',
'title' => 'required',
'category_id' => 'required|integer'
]);
Post::create($request->all());
return redirect()->route('posts.index');
}
public function show(Post $post)
{
//
}
public function edit(Post $post)
{
$entity = $post;
$categories = Category::all();
return view('admin.posts.form', compact('categories', 'entity'));
}
public function update(Request $request, Post $post)
{
$this->validate($request, [
'slug' => 'required',
'title' => 'required',
'category_id' => 'required|integer'
]);
$post->update($request->all());
return redirect()->route('posts.index');
}
public function destroy(Post $post)
{
$post->delete();
return redirect()->route('posts.index');
}
}