问题
I was created post create is it work. I try to adding for can edit post and delete, but i have error undefined variable.
namespace App\Http\Controllers;
use App\Models\Category;
use App\Models\Post;
use App\Models\PostImage;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$data['categories'] = Category::all();
return view('post', $data);
}
public function savePOst(Request $request)
{
// $imageName = time() . '.' . $request->file2->extension();
// $rs = $request->file2->move(public_path('img/portfolio'), $imageName);
//
// dd($rs);
// die;
$post = new Post();
$post->title = $request->title;
$post->desc = $request->desc;
$post->category_id = $request->category;
if ($post->save()) {
$postId = $post->id;
if ($request->hasFile('file1')) {
$imageName = uniqid().time() . '.' . $request->file1->extension();
$request->file1->move(public_path('img/portfolio'), $imageName);
$postImage = new PostImage();
$postImage->image = $imageName;
$postImage->main = 1;
$postImage->post_id = $postId;
$postImage->save();
}
if ($request->hasFile('file2')) {
$imageName = uniqid().time() . '.' . $request->file2->extension();
$request->file2->move(public_path('img/portfolio'), $imageName);
$postImage = new PostImage();
$postImage->image = $imageName;
$postImage->main = 0;
$postImage->post_id = $postId;
$postImage->save();
}
if ($request->hasFile('file3')) {
$imageName = uniqid().time() . '.' . $request->file3->extension();
$request->file3->move(public_path('img/portfolio'), $imageName);
$postImage = new PostImage();
$postImage->image = $imageName;
$postImage->main = 0;
$postImage->post_id = $postId;
$postImage->save();
}
if ($request->hasFile('file4')) {
$imageName = uniqid().time() . '.' . $request->file4->extension();
$request->file4->move(public_path('img/portfolio'), $imageName);
$postImage = new PostImage();
$postImage->image = $imageName;
$postImage->main = 0;
$postImage->post_id = $postId;
$postImage->save();
}
if ($request->hasFile('file5')) {
$imageName = uniqid().time() . '.' . $request->file5->extension();
$request->file5->move(public_path('img/portfolio'), $imageName);
$postImage = new PostImage();
$postImage->image = $imageName;
$postImage->main = 0;
$postImage->post_id = $postId;
$postImage->save();
}
return back()->with('status', 'Post inserted');
} else {
return back()->with('status', 'Error');
}
}
public function editPost(Request $requeest){
DB::table('posts')
->where('id', $requeest->id)
->update(['post' => $requeest->post]);
return back()->with('status','Posts been updated');
}
public function deletePost(Request $request){
DB::table('posts')->where('id', '=', $request->id)->delete();
echo json_encode(200);
}
}
/**
* Created by PhpStorm.
* User: abhi
* Date: 10/10/2020
* Time: 10:12 PM
*/ ?>
@extends('layouts.app')
@section('content')
<!-- CSS only -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<section style="padding-top:60px;">
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<div class="card">
<div class="card-header">
Add New Post
</div>
<div class="card-body">
@if(Session::has('status'))
<div class="alert alert-success" role="alert">
{{Session::get('status')}}
</div>
@endif
<form method="POST" action="{{url('addpost')}}" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label for="name">Title</label>
<input type="text" name="title" class="form-control"/>
<label for="desc">Description</label>
<textarea name="desc" class="form-control">
</textarea>
<label for="category">Post Category</label>
<select class="form-control" id="category" name="category">
<option selected disabled>Select Your Post Category</option>
@if($categories != null)
@foreach($categories as $category)
<option value="{{$category->id}}">{{$category->category}}</option>
@endforeach
@endif
</select>
<br>
<label for="file">Choose Post Main Image</label>
<input type="file" name="file1" class="form-control" onchange="previewFile1(this)"/>
<img id="previewImg1" alt="Main image" style="max-width:330px;margin-top:20px;">
<label for="file">Choose Post Image Two</label>
<input type="file" name="file2" class="form-control" onchange="previewFile2(this)"/>
<img id="previewImg2" alt="First image" style="max-width:330px;margin-top:20px;">
<label for="file">Choose Post Image Three</label>
<input type="file" name="file3" class="form-control" onchange="previewFile3(this)"/>
<img id="previewImg3" alt="Second image" style="max-width:330px;margin-top:20px;">
<label for="file">Choose Post Image Four</label>
<input type="file" name="file4" class="form-control" onchange="previewFile4(this)"/>
<img id="previewImg4" alt="Fourth image" style="max-width:330px;margin-top:20px;">
<label for="file">Choose Post Image Five</label>
<input type="file" name="file5" class="form-control" onchange="previewFile5(this)"/>
<img id="previewImg5" alt="Fifth image" style="max-width:330px;margin-top:20px;">
</div>
<button type="submit" class="btn btn-primary">Add New Post</button>
<a href="/category" class="btn btn-success">Add New Category</a>
</form>
</div>
<form method="POST" action="{{url('editpost')}}" enctype="multipart/form-data">
<div class="row mt-5">
<div class="col-md-6 offset-md-3">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Category</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@if($post != null)
@foreach($post as $posts)
<tr>
<th scope="row">{{$posts->id}}</th>
<td>{{$posts->posts}}</td>
<td>
<button class="btn btn-primary btn-sm mr-2"
onclick="edit('{{$posts->id}}','{{$posts->posts}}');">Edit
</button>
<button class="btn btn-danger btn-sm" onclick="del('{{$posts->id}}')">Delete</button>
</td>
</tr>
@endforeach
@endif
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"
integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"
integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV"
crossorigin="anonymous"></script>
<script>
function previewFile1(input) {
var file1 = $(input).get(0).files[0];
if (file1) {
var reader = new FileReader();
reader.onload = function () {
$('#previewImg1').attr("src", reader.result);
}
reader.readAsDataURL(file1);
}
}
function previewFile2(input) {
var file2 = $(input).get(0).files[0];
if (file2) {
var reader = new FileReader();
reader.onload = function () {
$('#previewImg2').attr("src", reader.result);
}
reader.readAsDataURL(file2);
}
}
function previewFile3(input) {
var file3 = $(input).get(0).files[0];
if (file3) {
var reader = new FileReader();
reader.onload = function () {
$('#previewImg3').attr("src", reader.result);
}
reader.readAsDataURL(file3);
}
}
function previewFile4(input) {
var file4 = $(input).get(0).files[0];
if (file4) {
var reader = new FileReader();
reader.onload = function () {
$('#previewImg4').attr("src", reader.result);
}
reader.readAsDataURL(file4);
}
}
function previewFile5(input) {
var file5 = $(input).get(0).files[0];
if (file5) {
var reader = new FileReader();
reader.onload = function () {
$('#previewImg5').attr("src", reader.result);
}
reader.readAsDataURL(file5);
}
}
</script>
@endsection
I created this project in laravel 8, it's for school i try to learn laravel and symfony i try to understand this systeme someone can help with this error and explain if possible. How I can resolve this error for I can edit and deleted posts, in the same page
回答1:
$data['categories'] = Category::all();return view('post', $data); to $data['categories'] = Category::all(); $data['post] = Post::all(); return view('post', $data);
- need add @csrf
来源:https://stackoverflow.com/questions/64315672/undefined-variable-post-view-home-e-images-123-test1-resources-views-post-bl