有没有一种方法可以在Laravel中的函数中调用函数

时间:2019-08-09 23:05:10

标签: laravel-5

我正在尝试在发出请求时在控制器中调用函数。

我添加了使用use App \ Traits \ UploadTrait;在控制器的顶部,试图将函数作为静态调用,但是不起作用。

我尝试了那些: -Laravel: Controller does not exist -Laravel cannot find a Controller Method (does not exist) -Class App\Http\Controllers\AdminController does not exist

路线

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

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

Route::post('/home', 'HomeController@NewOffer');

HomeController检查NewOffer

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Quotation;
use App\Traits\UploadTrait;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        $offers = DB::table('offers')->get();

        return view('home', ['offers' => $offers]);

    }

    public function NewOffer(Request $request)
    {
        $request->validate([
            'offer_title'=>'required',
            'offer_category'=>'required',
            'offer_image.*'=>'required|mimes:jpeg,bmp,png,jpg',
        ]);

        if ($request->has('offer_image')) {
            // Get image file
            $image = $request->file('offer_image');
            // Make a image name based on user name and current timestamp
            $name = str_slug($request->input('offer_title')).'_'.time();
            // Define folder path
            $folder = '/public/img/offers';
            // Make a file path where image will be stored [ folder path + file name + file extension]
            $filePath = $folder . $name. '.' . $image->getClientOriginalExtension();
            // Upload image
            $this->uploadOne($image, $folder, 'public', $name);
            // Set user profile image path in database to filePath
            $offers->offer_image = $filePath;
        }

        $offers->save();

        return redirect()->back()->with(['status' => 'Offre ajoutée avec succès !']);
    }

}

UploadTrait

<?php

namespace App\Traits;

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

Trait UploadTrait
{
    public function uploadOne(UploadedFile $uploadedFile, $folder = null, $disk = 'public', $filename = null)
    {
        $name = !is_null($filename) ? $filename : str_random(25);

        $file = $uploadedFile->storeAs($folder, $name.'.'.$uploadedFile->getClientOriginalExtension(), $disk);

        return $file;
    }
}

我得到的错误是

BadMethodCallException 方法App \ Http \ Controllers \ HomeController :: uploadOne不存在。

Laravel不喜欢这个$this->uploadOne($image, $folder, 'public', $name);

1 个答案:

答案 0 :(得分:0)

我需要在类中放置App\Traits\UploadTrait;,以便可以访问。