Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cameronmd's avatar

Adding two classes with the same name

So in my ItemsController I need both:

use Request;
use Illuminate\Http\Request;

Obviously this doesn't work since they have the same name. How do I go about fixing this? Thanks in advance.

Heres the rest of my ItemsController for the context they're needed.

<?php
namespace App\Http\Controllers;

 use App\Item;
 // use Request;
 use Illuminate\Http\Request;
 use App\Http\Controllers\Controller;
 use App\Http\Requests\ItemRequest;



 class ItemsController extends Controller
 {

 public function __construct()
 {
    $this->middleware('auth', ['except' => ['index', 'show']]);
 }

public function index()
{
    $items = Item::all();
    // $items = Item::latest('created_at')->get();

    return view('items.index', compact('items'));
}

public function show(Item $item)
{
    return view('items.show', compact('item'));
}

public function create()
{
    //Potentially use something like this for charging for ad
    // $cost = str_word_count(desc);

    return view('items.create');
}

public function store(ItemRequest $request)
{
    $input = Request::all();
    Item::create($input);

    return redirect('items');
}

public function addPhoto(Request $request)
{

    // $this->validate($request, [
    //     'photo' => 'require|mimes:jpg,jpeg,png,bmp'
    // ]);

    $file = $request->file('photo');

    $name = time().$file->getClientOriginalName();

    $file->move('public/items_add/photos', $name);

    //needs to change from first() to 'current()'
    $item = Item::first();
    // $item = Item::find($item->id);
    
    //Add $item into function arguments and test with line below (actually try using $id not $item)
    // $item = Item::findOrFail($item);

    $item->photos()->create(['path'=>"public/items_add/photos/{$name}"]);

    return 'Done';
}
}
0 likes
2 replies
thomaskim's avatar
Level 41

Request and Illuminate\Http\Request are the same thing.

You can replace:

$input = Request::all();

with this:

$input = $request->all();

Also, for future reference, if you do have two classes with the same name, you can alias it. For example:

use Request as RequestFacade;
use Illuminate\Http\Request;

Then, you can do:

RequestFacade::all();

Finally, as proof that the two are the same, you can check this:

RequestFacade::instance() === $request->instance(); // will return true
2 likes
willvincent's avatar

alias one or both of them...

use Request as Something
use Illuminate\Http\Request as SomethingElse

Then within your class refer to them by the alias you gave them.

Easy peasy.

3 likes

Please or to participate in this conversation.