0
votes

hello l have problem I try to create search bar in laravel 5.8.

welcome.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    @foreach ($posts as $post)
        <div class="card" style="width: 18rem;">
            <img class="card-img-top" src="..." alt="Card image cap">
            <div class="card-body">
                <h5 class="card-title">Card title</h5>
                <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                <a href="#" class="btn btn-primary">Go somewhere</a>
            </div>
        </div>
    @endforeach
</div>
@endsection

PostController.php

<?php

namespace App\Http\Controllers;

use App\Http\Requests\Poststore;
use App\Post;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\DB;
class PostController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $posts = DB::table('posts')->orderBy('created_at', 'DESC')->paginate(10);

        return view('welcome', compact('posts'));
    }

    public function create()
    {
        return view('posts.create');
    }

    public function store()
    {
        $data = request()->validate([
            'title' => ['required', 'string'],
            'image' => ['required', 'image'],
            'price' => ['required', 'integer'],
            'descriptionpost' => ['required', 'string']
        ]);

        $imagePath = request('image')->store('uploads', 'public');

        $image = Image::make(public_path("/storage/{$imagePath}"))->fit(1200, 1200);
        $image->save();


        auth()->user()->posts()->create([
            'title' => $data['title'],
            'descriptionpost' => $data['descriptionpost'],
            'price' => $data['price'],
            'image' => $imagePath
        ]);

        return redirect()->route('profile.show', ['user' => auth()->user() ]);
    }

    public function show(Post $post)
    {
        return view('posts.show', compact('post'));
    }
}

route

Route::get('/welcome', 'PostController@index')->name('posts.index');

but i have this error :

Undefined variable: posts (View: /home/savinov/annonces/resources/views/welcome.blade.php)

dont understand why its undefined variable if i define him in controller, someone can helping to me if its possible?

1
have you checked, $posts have data or not in controller? - Rahul
try dd($posts) in your controller to see what you're getting. You can also just pass it in as return view('welcome', $posts); seeing as you're only passing in one variable - party-ring
dont work my friend ... - Den Kot
@DenKot What doesn't work? Can you be more specific? He asked you to do more than 1 thing. What's dd($posts) returning to you? - César Escudero

1 Answers

1
votes

You try like below options

option 1

public function index()
{
   $posts = DB::table('posts')->orderBy('created_at', 'DESC')->paginate(10);
   return view('welcome', ['posts' => $posts]);
}

option2

public function index()
{
    $posts = Post::orderBy('created_at', 'DESC')->paginate(10);
    return view('welcome',compact('posts'));
     or 
   return view('welcome', ['posts' => $posts]);
}