我如何将图片上传到文章?
How i can upload image to the article?
请帮帮我,我正在想办法Laravel,还是不行。我已经创建了表格。建立了联系。创建控制器。它似乎是正确规定的。我不再明白如何做到在表格中你可以给这篇文章添加图片然后显示它。请帮助笨蛋。我需要我的函数为这篇文章上传一张图片,然后它才能显示。您最有可能需要以某种方式插入到 "create" 函数中。请帮忙。
创建文章表
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug');
$table->text('description_short')->nullable();
$table->text('description')->nullable();
$table->string('meta_title')->nullable();
$table->string('meta_description')->nullable();
$table->string('meta_keyword')->nullable();
$table->boolean('published')->nullable();
$table->integer('viewed')->nullable();
$table->integer('created_by')->nullable();
$table->integer('modified_by')->nullable();
$table->string('image_path'); //There image path
$table->timestamps();
});
}
ArticleController.php
class ArticleController extends Controller
{
public function store(Request $request)
{
$article = Article::create($request->all());
if($request->input('categories')) :
$article->categories()->attach($request->input('categories'));
endif;
return redirect()->route('admin.article.index');
}
public function index()
{
return view('admin.articles.index',[
'articles' => Article::orderBy('created_at', 'desc')->paginate(10)
]);
}
public function create()
{
return view('admin.articles.create',[
'article' => null,
'categories' => Category::with('children')->where('parent_id',0)->get(),
'delimiter' => ''
]);
}
function upload(Request $request)
{
$this->validate($request, [
'select_file' => 'required|image|mimes:jpg,png,gif|max:2048'
]);
$image = $request->file('select_file');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
return back()->with('success', 'Image Uploaded Successfully')->with('path', $new_name);
}
form.blade.php
<label for="published">Status</label>
<select id="published" class="form-control" name="published">
@if (isset($article) && isset($article->id))
<option value="0" {{ ($article->published == 0) ? 'selected' : '' }}>NO</option>
<option value="1" {{ ($article->published == 1) ? 'selected' : '' }}>OK</option>
@else
<option value="0" selected disabled>OK</option>
<option value="1" disabled>NO</option>
@endif
</select>
<label for="title">Title</label>
<input id="title" type="text" class="form-control" name="title" placeholder="" value="{{ $article ? $article->title : '' }}" required>
<label for="slug">Slug </label>
<input id="slug" class="form-control" type="text" name="slug" placeholder="" value="{{ $article ? $article->slug : '' }}" readonly />
<label for="parent_id">Parent cat</label>
<select id="parent_id" class="form-control" name="categories[]" multiple="">
@include('admin.categories.partials.categories',
['categories' => $categories,
'current' => $article,
'delimiter' => $delimiter])
</select>
<label for="">Short desc</label>
<textarea name="description_short" class="form-control" id="description_short">{{$article->description_short or ""}}</textarea>
<label for="">Descr</label>
<textarea name="description" class="form-control" id="description_short">{{$article->description or ""}}</textarea>
<hr />
<label for="">Meta title</label>
<input type="text" class="form-control" name="meta_title" placeholder="Meta title" value="{{ $article ? $article->meta_title : '' }}" required>
<label for="">Meta descrip</label>
<input type="text" class="form-control" name="meta_description" placeholder="" value="{{ $article ? $article->meta_description : '' }}" required>
<label for="">Key</label>
<input type="text" class="form-control" name="meta_keyword" placeholder="" value="{{ $article ? $article->meta_keyword : '' }}" required>
<div class="form-group">
<table class="table">
<tr>
<td width="40%" align="right"><label>Select File for Upload</label></td>
<td width="30"><input type="file" name="select_file" /></td>
</tr>
<tr>
<td width="40%" align="right"></td>
<td width="30"><span class="text-muted">jpg, png, gif</span></td>
<td width="30%" align="left"></td>
</tr>
create.blade.php
@extends('admin.layouts.app_admin')
@section('content')
<div class="container">
@component('admin.components.breadcrumb')
@slot('title') Create new @endslot
@slot('parent') Main @endslot
@slot('active') News @endslot
@endcomponent
<hr />
<form class="form-horizontal" action="{{route('admin.article.store')}}" method="post">
{{ csrf_field() }}
{{-- Form include --}}
@include('admin.articles.partials.form')
<input type="hidden" name="created_by" value="{{Auth::id()}}">
</form>
</div>
@endsection
article.blade.php
@extends('layouts.app')
@section('title', $article->meta_title)
@section('meta_keyword', $article->meta_keyword)
@section('meta_description', $article->meta_description)
@section('content')
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1>{{$article->title}}</h1>
<p>{!!$article->description!!}</p>
</div>
</div>
</div>
@endsection
filesystem.php
'public' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
'url' => env('APP_URL').'/public',
'visibility' => 'public',
],
Route::get('/blog/article/{slug?}', 'BlogController@article')->name('article');
通过enctype
属性指定表单可以post个文件
<form class="form-horizontal" action="{{route('admin.article.store')}}"
method="post" enctype="multipart/form-data">
并利用控制器中的upload
方法
public function store(Request $request)
{
$new_name = $this->upload($request);
$article = Article::create([
'title' => $request->title,
'slug' => $request->slug,
'description_short' => $request->description_short,
'description' => $request->description,
'meta_title' => $request->meta_title,
'meta_description' => $request->meta_description,
'meta_keyword' => $request->meta_keyword,
'published' => $request->published,
'viewed' => 0,
'created_by' => auth()->id(),
'modified_by' => auth()->id(),
'image_path' => $new_name
]);
if ($request->input('categories')) :
$article->categories()->attach($request->input('categories'));
endif;
return redirect()->route('admin.article.index')->with('success', 'Image Uploaded Successfully')->with('path', $new_name);
}
function upload(Request $request)
{
$this->validate($request, [
'select_file' => 'required|image|mimes:jpg,png,gif|max:2048'
]);
$image = $request->file('select_file');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
return $new_name;
}
希望对您有所帮助
请帮帮我,我正在想办法Laravel,还是不行。我已经创建了表格。建立了联系。创建控制器。它似乎是正确规定的。我不再明白如何做到在表格中你可以给这篇文章添加图片然后显示它。请帮助笨蛋。我需要我的函数为这篇文章上传一张图片,然后它才能显示。您最有可能需要以某种方式插入到 "create" 函数中。请帮忙。
创建文章表
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug');
$table->text('description_short')->nullable();
$table->text('description')->nullable();
$table->string('meta_title')->nullable();
$table->string('meta_description')->nullable();
$table->string('meta_keyword')->nullable();
$table->boolean('published')->nullable();
$table->integer('viewed')->nullable();
$table->integer('created_by')->nullable();
$table->integer('modified_by')->nullable();
$table->string('image_path'); //There image path
$table->timestamps();
});
}
ArticleController.php
class ArticleController extends Controller
{
public function store(Request $request)
{
$article = Article::create($request->all());
if($request->input('categories')) :
$article->categories()->attach($request->input('categories'));
endif;
return redirect()->route('admin.article.index');
}
public function index()
{
return view('admin.articles.index',[
'articles' => Article::orderBy('created_at', 'desc')->paginate(10)
]);
}
public function create()
{
return view('admin.articles.create',[
'article' => null,
'categories' => Category::with('children')->where('parent_id',0)->get(),
'delimiter' => ''
]);
}
function upload(Request $request)
{
$this->validate($request, [
'select_file' => 'required|image|mimes:jpg,png,gif|max:2048'
]);
$image = $request->file('select_file');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
return back()->with('success', 'Image Uploaded Successfully')->with('path', $new_name);
}
form.blade.php
<label for="published">Status</label>
<select id="published" class="form-control" name="published">
@if (isset($article) && isset($article->id))
<option value="0" {{ ($article->published == 0) ? 'selected' : '' }}>NO</option>
<option value="1" {{ ($article->published == 1) ? 'selected' : '' }}>OK</option>
@else
<option value="0" selected disabled>OK</option>
<option value="1" disabled>NO</option>
@endif
</select>
<label for="title">Title</label>
<input id="title" type="text" class="form-control" name="title" placeholder="" value="{{ $article ? $article->title : '' }}" required>
<label for="slug">Slug </label>
<input id="slug" class="form-control" type="text" name="slug" placeholder="" value="{{ $article ? $article->slug : '' }}" readonly />
<label for="parent_id">Parent cat</label>
<select id="parent_id" class="form-control" name="categories[]" multiple="">
@include('admin.categories.partials.categories',
['categories' => $categories,
'current' => $article,
'delimiter' => $delimiter])
</select>
<label for="">Short desc</label>
<textarea name="description_short" class="form-control" id="description_short">{{$article->description_short or ""}}</textarea>
<label for="">Descr</label>
<textarea name="description" class="form-control" id="description_short">{{$article->description or ""}}</textarea>
<hr />
<label for="">Meta title</label>
<input type="text" class="form-control" name="meta_title" placeholder="Meta title" value="{{ $article ? $article->meta_title : '' }}" required>
<label for="">Meta descrip</label>
<input type="text" class="form-control" name="meta_description" placeholder="" value="{{ $article ? $article->meta_description : '' }}" required>
<label for="">Key</label>
<input type="text" class="form-control" name="meta_keyword" placeholder="" value="{{ $article ? $article->meta_keyword : '' }}" required>
<div class="form-group">
<table class="table">
<tr>
<td width="40%" align="right"><label>Select File for Upload</label></td>
<td width="30"><input type="file" name="select_file" /></td>
</tr>
<tr>
<td width="40%" align="right"></td>
<td width="30"><span class="text-muted">jpg, png, gif</span></td>
<td width="30%" align="left"></td>
</tr>
create.blade.php
@extends('admin.layouts.app_admin')
@section('content')
<div class="container">
@component('admin.components.breadcrumb')
@slot('title') Create new @endslot
@slot('parent') Main @endslot
@slot('active') News @endslot
@endcomponent
<hr />
<form class="form-horizontal" action="{{route('admin.article.store')}}" method="post">
{{ csrf_field() }}
{{-- Form include --}}
@include('admin.articles.partials.form')
<input type="hidden" name="created_by" value="{{Auth::id()}}">
</form>
</div>
@endsection
article.blade.php
@extends('layouts.app')
@section('title', $article->meta_title)
@section('meta_keyword', $article->meta_keyword)
@section('meta_description', $article->meta_description)
@section('content')
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1>{{$article->title}}</h1>
<p>{!!$article->description!!}</p>
</div>
</div>
</div>
@endsection
filesystem.php
'public' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
'url' => env('APP_URL').'/public',
'visibility' => 'public',
],
Route::get('/blog/article/{slug?}', 'BlogController@article')->name('article');
通过enctype
属性指定表单可以post个文件
<form class="form-horizontal" action="{{route('admin.article.store')}}"
method="post" enctype="multipart/form-data">
并利用控制器中的upload
方法
public function store(Request $request)
{
$new_name = $this->upload($request);
$article = Article::create([
'title' => $request->title,
'slug' => $request->slug,
'description_short' => $request->description_short,
'description' => $request->description,
'meta_title' => $request->meta_title,
'meta_description' => $request->meta_description,
'meta_keyword' => $request->meta_keyword,
'published' => $request->published,
'viewed' => 0,
'created_by' => auth()->id(),
'modified_by' => auth()->id(),
'image_path' => $new_name
]);
if ($request->input('categories')) :
$article->categories()->attach($request->input('categories'));
endif;
return redirect()->route('admin.article.index')->with('success', 'Image Uploaded Successfully')->with('path', $new_name);
}
function upload(Request $request)
{
$this->validate($request, [
'select_file' => 'required|image|mimes:jpg,png,gif|max:2048'
]);
$image = $request->file('select_file');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
return $new_name;
}
希望对您有所帮助