簡體   English   中英

Laravel 為什么我的圖片發布功能無法正常工作?

[英]Laravel Why my image post function doesn't work correctly?

基本信息

我正在使用 Laravel6.0 開發一個簡單的 Web 應用程序。
我做了一個圖片發布功能,但它不能正常工作。
沒有錯誤消息。

問題

我無法發布圖像。
沒有改變。
它只是過渡到同一頁面。
create2.blade.php是發布圖像的形式,我想在index2.blade.php上查看此圖像

代碼

路線/ web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('login');

Route::group(['middleweare' => 'auth'], function () {
    Route::get('/', 'StoriesController@index');
    Route::post('/', 'StoriesController@index');
    Route::post('/stories/create', 'StoriesController@upload');
    Route::get('/stories/create', 'StoriesController@add');
    Route::post('/stories/create', 'StoriesController@add');
});

Route::group(['middleweare' => 'auth','name'=>'profile'], function () {
    Route::get('/profile/edit', 'ProfileController@edit');
    Route::get('/profile/create', 'ProfileController@add');
    Route::post('/profile/create', 'ProfileController@add');
    Route::post('/profile/create', 'ProfileController@store');
    Route::post('/profile/create', 'ProfileController@upload');
});

Route::get('/home', 'HomeController@index')->name('home');

Auth::routes();


app/Http/Controllers/StoriesController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Story;
use Auth;
use App\Posts;
use App\History;
use App\Attachment;
use Carbon\Carbon;
use Storage;

class StoriesController extends Controller
{

  public function __construct()
    {
      $this->middleware('auth');
    }

  public function index(Request $request)
      {
        $images = Attachment::all();

        return view('stories.index2', compact('images'));
      }

  public function add()
    {
      return view('stories.create2');
    }


  public function store(Request $request)
  {

    $d = new \DateTime();
    $d->setTimeZone(new \DateTimeZone('Asia/Tokyo'));
    $dir = $d->format('Y/m');
    $path = sprintf('public/images/%s', $dir);


    $data = $request->except('_token');

    foreach ($data['images'] as $k => $v) {

      $filename = '';


      $attachments = Attachment::take(1)->orderBy('id', 'desc')->get();

      foreach ($attachments as $attachment) {

        $filename = $attachment->id + 1 . '_' . $v->getClientOriginalName();
      }
      unset($attachment);

      if ($filename == false) {
        $filename = 1 . '_' . $v->getClientOriginalName();
      }

      $v->storeAs($path, $filename);

      $attachment_data = [
        'path' => sprintf('images/%s/', $dir),
        'name' => $filename
      ];

      $a = new Attachment();
      $a->fill($attachment_data)->save();
    }

    unset($k, $v);

    return redirect('/');
  }


  public function upload(Request $request)
    {
      dd($request->all());
      $this->validate($request, [
        'file' => [
          'required',
          'file',
          'image',
          'mimes:jpeg,png',
        ]
      ]);

      if ($request->file('file')->isValid([])) {
        $path = $request->file->store('public');
        return view('stories.index2')->with('filename', basename($path));
      } else {
        return redirect('/')
          ->back()
          ->withInput()
          ->withErrors();
      }
    }
}

資源/視圖/故事/create2.blade.php

@extends('layouts.front2')
@section('title','StoryCreate')

@section('content')
<link href="{{ asset('/css/main22.css' )}}" rel="stylesheet">

<div class="poststory">
    <h1>Post Story</h1>
</div>
@if ($errors->any())
<ul>
    @foreach($errors->all() as $error)
    <li>{{ $error }}</li>
    @endforeach
</ul>
@endif
<form action="{{ url('/') }}" method="POST" enctype="multipart/form-data">

    <div class="form">
        <label for="photo" class="file">Choose Image or Video</label>
        <div class="post">
            <input type="file" class="here" name="images[]">

        </div>
    </div>
    <br>
    </div>

    {{ csrf_field() }}
    <div class="post">
        <div class="btn postbtn">
            <input type="submit" value="post" />
        </div>
    </div>
</form>

@endsection

資源/視圖/故事/index2.blade.php

@extends('layouts.front2')
@section('title','mainpage')

@section('content')
<div class="profile">

    <div class="profileimg">
        @foreach ($images as $image)
        <img src="/storage/{{ $image->path . $image->name }}" style="height: 210px; width: 210px; border-radius: 50%;">
        @endforeach
    </div>

    <div class="name">
        @guest
        <a class="nav-link2" href="{{ route('register')}}">{{ __('Create Accout!')}}</a>
        @else
        <a id="navbarDropdown" class="nav-link2" href="#" role="button">
            {{Auth::user()->name}}<span class="caret"></span></a>



        <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
            @csrf
        </form>
    </div>
    @endguest


    <div class="aboutme">
        You can write your profile here!You can write your profile here!You can write your profile here!
        You can write your profile here!You can write your profile here!You can write your profile here!
        You can write your profile here!You can write your profile here!You can write your profile here!
        You can write your profile here!You can write your profile here!You can write your profile here!
        You can write your profile here!You can write your profile here!You can write your profile here!
    </div>

</div>

<div class="new">

    <div class="newtitle">
        <h1>New</h1>
    </div>

    <div class="container1">

        @foreach ($images as $image)
        <img src="/storage/{{ $image->path . $image->name }}" class="images" style="height: 150px; width: 150px; border-radius: 50%;">
        @endforeach
        <div class="more">
            more...
        </div>
    </div>

</div>

<div class="stories">

    <div class="titlestories">
        <h1>Stories</h1>
    </div>

    <div class="container2">

        <div class="titleclose">
            <h2>#CloseFriends</h2>
        </div>

        @foreach ($images as $image)
        <img src="/storage/{{ $image->path . $image->name }}" class="images" style="height: 150px; width: 150px; border-radius: 50%;">
        @endforeach

        <div class="titlefollow">
            <h2>#Follows</h2>
        </div>


    </div>
</div>

{{ csrf_field() }}
@endsection

對不起我糟糕的英語,但是,我需要幫助。
等待您的評論和答復!

您的路由文件中有兩個POST /stories/create定義。

   Route::post('/stories/create', 'StoriesController@upload');
   ...
   Route::post('/stories/create', 'StoriesController@add');

處理的最后一個將接收對此路由的請求。 您應該能夠通過運行php artisan route:list並查看哪些路由可用以及它們映射到哪些控制器來在命令行上驗證這一點。 在這種情況下,您可能會點擊添加方法而不是上傳方法。

作為立即修復,從路由文件中刪除第二個定義,重新運行php artisan route:list以查找更改,並查看網頁的行為是否有所不同。

看起來您將面臨類似的問題:

  Route::post('/profile/create', 'ProfileController@add');
  Route::post('/profile/create', 'ProfileController@store');
  Route::post('/profile/create', 'ProfileController@upload');

足智多謀的路由可以幫助標准化路由,並避免很多像這樣的頭痛和困惑。 https://laravel.com/docs/6.x/controllers#resource-controllers

特別是,如果您按照https://laravel.com/docs/6.x/controllers#restful-partial-resource-routes 中的描述定義路由,則可以使用php artisan route:list來驗證每個路由是哪些方法映射到,然后更新您的控制器以匹配。

Route::resource('stories', 'StoriesController')->only([
    'index', 
    'create', 
    'store',
]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM