Skip to content

Instantly share code, notes, and snippets.

@platinize
Last active March 23, 2018 16:08
Show Gist options
  • Save platinize/61bec9ed6e6c7b71d0a778fd858020ae to your computer and use it in GitHub Desktop.
Save platinize/61bec9ed6e6c7b71d0a778fd858020ae to your computer and use it in GitHub Desktop.
Laravel_5
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<link href="../css/news/news.css" rel="stylesheet">
</head>
<h1>Админка</h1>
<p><a href="{{route('home')}}" class="home">На главную</a>
<table cellspacing="0" border="1" cellpadding="10" class="adminTable">
<tr>
<td colspan="6" class="tableTitle"><span>Все новости</span></td>
<td colspan="2" class="tableTitle"><a href="{{route('addNews')}}">Добавить новость</a></td>
</tr>
<tr class="tableHead">
@foreach($colums as $entry)
<td>{{$entry}}</td>
@endforeach
<td class="func">Редактировать</td>
<td class="func">Удалить</td>
</tr>
@foreach($news as $entry)
<tr>
<td title="{{$entry->id}}">{{$entry->id}}</td>
<td title="{{$entry->title}}">{{str_limit($entry->title, 20)}}</td>
<td title="{{$entry->author}}">{{str_limit($entry->author, 20)}}</td>
<td title="{{$entry->content}}">{{str_limit($entry->content, 30)}}</td>
<td title="{{$entry->img}}" style="padding:0">@if($entry->img != '')<img src='../../{{$entry->img}}'>@endif</td>
<td title="{{$entry->created_at}}">{{str_limit($entry->created_at, 10)}}</td>
<td title="@if($entry->updated_at != NULL)Последнее редактирование {{$entry->updated_at}}@endif"><a href="{{route('updateNews', $entry->id)}}">Редактировать</a></td>
<td><a href="{{route('deleteNews', $entry->id)}}">Удалить</a></td>
</tr>
@endforeach
</table>
<div class="secondPag">{{$news->links()}}</div>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
@if(isset($news))<link href="../../../css/news/news.css" rel="stylesheet">@else<link href="../../css/news/news.css" rel="stylesheet">@endif
</head>
@if(isset($news))
<h1>Редактрирование новости "{{$news->title}}"(id{{$news->id}})</h1>
@else
<h1>Создание новости</h1>
@endif
<div class="newsForm">
<p><a href="{{route('admin')}}">Вернуться</a></p>
<form method="post" enctype="multipart/form-data">
<div><input type="hidden" name="id" value="@isset($news){{$news->id}}@endisset"></div>
<div>
<p>Название новсти</p>
<textarea rows="3" name="title" required>@isset($news){{$news->title}}@endisset</textarea>
</div>
<div>
<p>Автор</p>
<textarea rows="2" name="author" required>@isset($news){{$news->author}}@endisset</textarea>
</div>
<div>
<p>Содержание</p>
<textarea rows="20" spellcheck="true" name="content" required >@isset($news){{$news->content}}@endisset</textarea>
</div>
<div>
<p>Фото</p>
<p>@isset($news) @if($news->img != '') <img src="../../../../{{$news->img}}"> @endif @endisset</p>
<p><input type="file" name="img"></p>
</div>
{{ csrf_field() }}
<input type="submit" value="@if(isset($news)) Сохранить @else Добавить @endif">
</form>
</div>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
@isset($all) <link href= "../css/news/news.css" rel="stylesheet" > @else <link href= "css/news/news.css" rel="stylesheet" >@endisset
</head>
@isset($all)<h1>Все новости</h1><div><p><a href="{{route('home')}}" class="home">На главную</a></p>{{$news->links()}}
@else<h1>Главная</h1><div><p><a href="{{route('all')}}" class="home">Все новости</a><a href="{{route('admin')}}" class="home">Админка</a></p>@endisset
@foreach($news as $entry)
<div class="newsBlock">
<p class="newsTitle"><a href="{{route('moreInfo', $entry->id)}}">{{$entry->title}}</a></p>
<span>
@if($entry->img != '')<a href="{{route('moreInfo', $entry->id)}}"><img src="@isset($all)../@endisset../{{$entry->img}}"></a> @endif
@isset($all){{$entry->content}}@else{{str_limit($entry->content, 500)}}@endisset
</span>
<p class="info"><span class="auhor">Автор: {{$entry->author}}</span><span class="publicDate">{{$entry->created_at}}</span></p>
</div>
@endforeach
@isset($all)<div class="secondPag">{{$news->links()}}</div>@endisset
</div>
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class News extends Model
{
protected $guarded = [];
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\News;
class NewsController extends Controller
{
/**
* показать страницу с последними 5 новостями
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show(){
$news = News::orderBy('id', 'desc')
->take(5)
->get();
return view('news.index', ['news'=>$news]);
}
/**
* показать страницу со всеми новостями начиная с последне добавленной
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showAll(){
$news = News::orderBy('id', 'desc')
->paginate(5);
return view('news.index', ['news'=>$news, 'all'=>'']);
}
/**
* показать страницу администрирования
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function admin(){
$news = News::orderBy('id', 'desc')
->paginate(10);
return view('news.admin', ['news'=>$news, 'colums'=>['id','title','author','content','img','created_at']]);
}
/**
* удалить запись с $id и перенаправить на админку
* @param $id номер записи
* @return bool|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function delete($id){
if (!is_numeric($id)) return false;
News::destroy($id);
return redirect('news/admin');
}
/**
* вывести форму для редкатировния записи
* @param $id номер записи
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function formUpdate($id) {
$oneNews = News::where('id', $id)->first();
return view('news.form', ['news' => $oneNews]);
}
/**
* обновление записи значениями из формы
* @param Request $request массив значений из формы для обновления
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function update(Request $request){
$this->validate($request, [
'id' => 'required|numeric',
'title' => 'required|max:300',
'author' => 'required|max:100',
'content' => 'required|max:10000',
]);
$news = $request->all();
if (isset($request->img)){
$img = $request->img->store('images');
$img = 'storage/app/'.$img;
$news['img'] = $img;
}
unset($news['_token'],$news['id']);
News::find($request->id)->update($news);
return redirect('news/admin');
}
/**
* добавление новости значениями из формы
* @param Request $request значения из формы
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function addNews(Request $request){
$this->validate($request, [
'title' => 'required|max:300',
'author' => 'required|max:100',
'content' => 'required|max:10000',
]);
$news = $request->all();
if (isset($request->img)){
$img = $request->img->store('images');
$img = 'storage/app/'.$img;
$news['img'] = $img;
}
unset($news['_token'],$news['id']);
News::create($news);
return redirect('news/admin');
}
/**
* вывести форму для добавления новсоти
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function formAddNews(){
return view('news.form');
}
/**
* перенаправить на страницу полной новости с выбранным id
* @param $id номер новости
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function moreInfo($id){
if (!is_numeric($id)) return view('news.index');
$news = News::where('id', $id)->first();
return view('news.oneNews', ['news'=>$news]);
}
}
<head>
<style>
body {}
h1 {text-align: center;}
.newsBlock {display:flex;flex-direction:column;width:60%;margin:10px;background:#fafafa;padding:10px;float:left}
.newsBlock p {display: flex;padding: 0 10px;justify-content: space-between;}
.newsBlock p.info span {display: block;}
img {max-width: 800px;width:100%; float:left; margin: 15px auto; display:block}
.newsTitle a {font-size:20px;font-weight: 600;color:#245269}
a {text-decoration:none;}
a.home {margin:40px;display:block;color:#2b542c;font-size:25px;}
a.home:hover{color: #c7254e}
.info {font-size: 14px;color:#5e5e5e}
</style>
</head>
<h1>{{$news->title}}</h1>
<div class="newsBlock">
@if($news->img != '')<img src="../../{{$news->img}}"> @endif
{{$news->content}}
<p class="info"><span class="auhor">Автор: {{$news->author}}</span><span class="publicDate">{{$news->created_at}}</span></p>
</div>
<a href="{{route('home')}}" class="home">На главную</a></p>
<?php
Route::get('/', function () {
return view('welcome');
});
Route::any('news', 'NewsController@show')->name('home');
Route::get('news/all', 'NewsController@showAll')->name('all');
Route::any('news/admin', 'NewsController@admin')->name('admin');
Route::any('news/admin/update/{id}', 'NewsController@formUpdate')->name('updateNews');
Route::any('news/admin/delete/{id}', 'NewsController@delete')->name('deleteNews');
Route::post('news/admin/update/{id}', 'NewsController@update');
Route::get('news/admin/add', 'NewsController@formAddNews')->name('addNews');
Route::get('news/admin/add', 'NewsController@formAddNews')->name('addNews');
Route::post('news/admin/add', 'NewsController@addNews');
Route::get('news/{id}', 'NewsController@moreInfo')->name('moreInfo');
//Контроллер
https://gist.github.com/platinize/61bec9ed6e6c7b71d0a778fd858020ae#file-newscontroller-php
//Модели
https://gist.github.com/platinize/61bec9ed6e6c7b71d0a778fd858020ae#file-news-php
//vievs
https://gist.github.com/platinize/61bec9ed6e6c7b71d0a778fd858020ae#file-admin-blade-php
https://gist.github.com/platinize/61bec9ed6e6c7b71d0a778fd858020ae#file-form-blade-php
https://gist.github.com/platinize/61bec9ed6e6c7b71d0a778fd858020ae#file-index-blade-php
https://gist.github.com/platinize/61bec9ed6e6c7b71d0a778fd858020ae#file-onenews-blade-php
//роут
https://gist.github.com/platinize/61bec9ed6e6c7b71d0a778fd858020ae#file-web-php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment