×
Clear all filters including search bar
Valeri Tandilashvili's Laravel Notes
Route::resource('posts', 'PostsController');
These routes are:
GET
at posts
to list all the posts
POST
at posts
to store new post
GET
at posts/create
to show form for creating new post
GET
at posts/{post}
to show the post
PUT
at posts/{post}
to update the post
DELETE
at posts/{post}
to delete the post
GET
at posts/{post}/edit
to show edit formPosts
php artisan make:controller PostsController
With the following basic content<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class Postscontroller extends Controller
{
//
}
Creates controller and empty methods in it (with appropriate comments)php artisan make:controller PostsController --resource
The methods are:
index()
- Displays a list of the resource.
create()
- Shows the form for creating a new resource
store(Request $request)
- Stores a newly created resource in storage
show($id)
- Displays the specified resource
edit($id)
- Shows the form for editing the specified resource
update(Request $request, $id)
- Updates the specified resource in storage
destroy($id)
- Removes the specified resource from storagewith
methodpublic function about() {
$param1 = 'this is a parameter of about us page';
return view('pages/about')->with('title', $param1);
}
Content of the blade template@extends('layouts.app')
@section('cntnt')
<h3>abt us {{$title}}</h3>
@endsection
public function services() {
$data = [
'title' => 'langs',
'languages' => ['PHP', 'Java', 'Python']
];
return view('pages/services')->with($data);
}
Receive the passed array@extends('layouts.app')
@section('cntnt')
<h3>{{$title}}</h3>
@if(count($languages))
@foreach($languages as $language)
<li>{{$language}}</li>
@endforeach
@endif
@endsection
Post
php artisan make:model Post
With the following basic content<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//
}
Creates Post
model and also migrations to create table for the modelphp artisan make:model Post -m
The migration file location isdatabase/migrations/2020_09_02_041219_create_posts_table.php
public function contact() {
return 'Under Construction';
}
Returns HTMLpublic function about() {
return '<h1>About the site</h1>details...';
}
Returns view located at resources/views/pages/services.blade.php
public function services() {
return view('pages/services');
}
multiple
attribute and append []
to input name<input type="file" multiple="multiple" name="files[]" placeholder="Attach file"/>
To validate show errors on client side we have to append input name with .*
Complete example of the form-group
<div class="form-group">
<label for="files">Attach File</label>
<i id="add_new_file" class="ki ki-plus text-success pointer" title="Add New File"></i>
<input type="file" class="form-control mb-2 {{ $errors->has('files.*') ? 'is-invalid' : '' }}" multiple="multiple" name="files[]" placeholder="Attach file"/>
@if($errors->has('files.*'))
<div class="invalid-feedback">
<strong>{{ $errors->first('files.*') }}</strong>
</div>
@endif
</div>
Server side validation of mime typespublic function rules()
{
return [
'comment' => 'sometimes|nullable|min:2|max:255',
'files.*' => 'sometimes|nullable|mimes:pdf,docx,xlsx,pptx,rar,zip,png,jpg',
];
}
It's necessary the validator key files
to be appended with the same symbols .*
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('dashboard')->with('posts', $user->posts);
public function roles()
{
return $this->belongsToMany(Role::class, 'model_has_roles', 'model_id', 'role_id');
}
$errors->has('accountable_id')
did not work @if($errors->has('accountable_id'))
<div class="invalid-feedback">
<strong>{{ $errors->first('accountable_id') }}</strong>
</div>
@endif
Until is-invalid
class was specifiedclass="form-control {{ $errors->has('accountable_id') ? 'is-invalid' : '' }}"
The complete example<div class="form-group">
<label for="exampleSelect2">Accountable <span class="text-danger">*</span></label>
<select class="form-control {{ $errors->has('accountable_id') ? 'is-invalid' : '' }}" id="accountable_id" name="accountable_id">
<option value="">Select Accountable</option>
@foreach($users as $accountable)
<option value="{{ $accountable->id }}" {{ $accountable_id == $accountable->id ? 'selected' : '' }}>{{ $accountable->name }}</option>
@endforeach
</select>
@if($errors->has('accountable_id'))
<div class="invalid-feedback">
<strong>{{ $errors->first('accountable_id') }}</strong>
</div>
@endif
</div>