提问者:小点点

Laravel 8传递多个参数以查看


我试图传递多个参数到同一个视图,laravel抛给我一个错误,说未定义变量$博客。我已经将变量$all_cards传递给同一个视图,但从HomeController.php的另一个控制器传递给index()。问题是Laravel正在读取$all_cards,但没有第二个var$blog。Laravel总是抛出相同的错误。

博客控制器。php

class BlogController extends Controller
{
    public function index()
    {
        $blogs = Blog::all();
        return view('home.home',['blogs'=>$blogs]);
    }
}

网状物php

use Illuminate\Support\Facades\Route;

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

Auth::routes();

Route::get('/home', [App\Http\Controllers\BlogController::class, 'index'])->name('home');

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Route::get('/home/add-deposit', [App\Http\Controllers\HomeController::class, 'addDeposit'])->name('home.addDeposit');
Route::post('/home/add-deposit', [App\Http\Controllers\HomeController::class, 'updateDeposit'])->name('home.addDeposit');

Route::get('/home/show-card-form', [App\Http\Controllers\HomeController::class, 'showCardForm'])->name('home.showCardForm');
Route::post('/home/save-card', [App\Http\Controllers\HomeController::class, 'saveCard'])->name('home.saveCard');
Route::get('/home/delete-card/{id}' , [App\Http\Controllers\HomeController::class, 'deleteCard'])->name('home.deleteCard');
Route::get('/home/edit-card-view/{id}', [App\Http\Controllers\HomeController::class, 'editCardView'])->name('home.editCardView');
Route::put('/home/update-card/{id}', [App\Http\Controllers\HomeController::class, 'updateCard'])->name('home.updateCard'); //dovrsi

Route::get('/home/buy-food/{id}' , [App\Http\Controllers\HomeController::class, 'buyFood'])->name('home.buyFood');

Route::get('/home/show-chef-form', [App\Http\Controllers\HomeController::class, 'chefForm'])->name('home.chefForm');
Route::post('/home/show-chef-form', [App\Http\Controllers\HomeController::class, 'saveChef'])->name('home.saveChef');

家刀身php

@foreach ($blogs as $blog)
<div class="col-xl-6 col-12 blog-left">
    <div class="post-item">
        <div class="post-inner">
            <div class="post-thumb">
                <a href="#">
                    <img src="/chef_images/{{ $blog->image }}" alt="petuk-blog">
                </a>
            </div>
            <div class="post-content">
                <h5><a href="#">{{ $blog->title }}</a></h5>
                <a href="#" class="date">{{ $blog->created_at }}</a>
                <p>{{ $blog->description }}</p>
                <a href="#" class="food-btn"><span>Read More</span></a>
            </div>
        </div>
    </div>
</div>
</div>
</div>
@endforeach

家庭控制器。php

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(Request $request)
    {
        $search = $request->get('search');
        $all_cards = Post::all();
        $all_cards = DB::table('posts')->where('name','like','%'.$search.'%')->paginate(5);
        $chefs = HomeChef::all();
        return view('home.home',compact('all_cards','chefs'));
    }

    public function addDeposit()
    {
        return view('home.addDeposit');
    }

    public function updateDeposit(Request $request)
    {
        $request->validate([
            'deposit'=>'required'
        ]);
        $user = Auth::user();
        $user->deposit = $user->deposit + $request->deposit;
        $user->save();

        return redirect(route('home'));
    }

    public function showCardForm()
    {   
        return view('home.showCardForm');
    }

    public function saveCard(Request $request)
    {
        $request->validate([
            'price'=>'required',
            'image1'=>'mimes:png,jpg,jpeg',
            'image2'=>'mimes:png,jpg,jpeg',
            'name'=>'required',
            'foodname'=>'required'
        ]);

        if($request->hasFile('image1')){
            $image1 = $request->file('image1');
            $image1_name = time().'1.'.$image1->extension();
            $image1->move(public_path('card_images'),$image1_name);
        }

        if($request->hasFile('image2')){
            $image2 = $request->file('image2');
            $image2_name = time().'2.'.$image2->extension();
            $image2->move(public_path('card_images'),$image2_name);
        }

        Post::create([
            'price'=>$request->price,
            'image1'=> (isset($image1_name)) ? $image1_name : null,
            'image2'=> (isset($image2_name)) ? $image2_name : null,
            'name'=> $request->name,
            'foodname'=>$request->foodname
        ]);

        return redirect(route('home'));
    }

    public function deleteCard($id)
    {
        $card = Post::find($id);
        $card->delete();

        return redirect(route('home'));
    }

    public function editCardView($id)
    {
        $card = Post::find($id);
        return view('home.editCardView',compact('card'));
    }

    public function buyFood($id)
    {
        $user = Auth::user();
        $card = Post::find($id);
        $price = $card->price;

        $user->deposit = $user->deposit - $price;
        $user->save();

        return redirect(route('home'));
    }
    public function chefForm()
    {
        return view('home.chefForm');
    }
    
    public function saveChef()
    {
        $request->validate([
            'image1'=>'mimes:png,jpg,jpeg',
            'image2'=>'mimes:png,jpg,jpeg',
            'name'=>'required',
            'position'=>'required'
        ]);

        if($request->hasFile('image1')){
            $image1 = $request->file('image1');
            $image1_name = time().'1.'.$image1->extension();
            $image1->move(public_path('chef_images'),$image1_name);
        }

        if($request->hasFile('image2')){
            $image2 = $request->file('image2');
            $image2_name = time().'2.'.$image2->extension();
            $image2->move(public_path('chef_images'),$image2_name);
        }

        HomeChef::create([
            'image1'=> (isset($image1_name)) ? $image1_name : null,
            'image2'=> (isset($image2_name)) ? $image2_name : null,
            'name'=> $request->name,
            'position'=>$request->position
        ]);

        return redirect(route('home'));

    }

}

共1个答案

匿名用户

=>if you display all record using foreach in view and blogs variable compact in your compact. ['blogs'=>$blogs] you can not using this variable on your controller that is not support if you pass multiple variable so compact.  

->return your page and blogs that is your list of data. that is working try. 

return view('home.home',compact('blogs'));