Undefined variable $title

  Wed 02 / 08 / 2023

  Posted by: vosem46716

Category : Laravel

Tags : laravel , php laravel , web

Post Thumbnail
Description

Hey ! I am getting some errors i don't know why, I have built a simple crud. now i want to add Insert fields more, but i am getting errors. if i comment on new fields then the code works if I uncomment new fields it shows error undefined error. Can u please help me i am not able to find an error.


View Code
                        Admin Controller //
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;
use Illuminate\Support\Facades\Auth;



class AdminController extends Controller
{
    public function post_page(){
        return view('admin\post\postpage');
    }


    public function add_post(Request $request)
    {

        $user=Auth()->user();
        $userid = $user->id;
        $name = $user->name;
        $usertype = $user->usertype;



        $post=new Post;
        $post->title = $request->title;

        $post->description = $request->description;

        $post->post_status = 'active';
        $post->user_id = $userid;
        $post->title = $title;
        $post->description = $description;
        $post->seo_url = $seo_url;
        $post->meta_keywords = $meta_keywords;
        $post->meta_description = $meta_description;
        $post->usertype = $usertype;

        // Public folder image
        $image=$request->image;

        if($image)
        {
        $imagename=time().'.'.$image->getClientOriginalExtension();
        $request->image->move('postimage', $imagename);

        // to get image
        $post->image = $imagename;
    }

        $post->save();

        return redirect()->back()->with('message', 'Post Added Successfully!');
    }
    
    public function show_post()
    {

        $post = Post::all();
        return view('admin\post\show_post', compact('post'));
    }


    public function delete_post($id)
    {

        $post = Post::find($id);

        $post->delete();

        return redirect()->back()->with('message', 'Post Deleted Successfully');

    }


    public function edit_page($id)
    {

        $post=Post::find($id);

        return view('admin\post\edit_page', compact('post'));

    }


    public function update_post(Request $request,$id)
    {
        $data = Post::find($id);
        $data->title=$request->title;
        $data->description=$request->description;
        $image=$request->image;
       

        if($image)
        {
        $imagename=time().'.'.$image->getClientOriginalExtension();
        $request->image->move('postimage', $imagename);

        // to get image
        $data->image = $imagename;
        }

        $data->save();

        return redirect()->back()->with('message', 'Post Updated Successfully');
    }

}

/End Admin Controller




Table File 


<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title')->nullable();
            $table->longText('description')->nullable();
            $table->longText('seo_url')->nullable();
            $table->longText('meta_keywords')->nullable();
            $table->longText('meta_description')->nullable();
            $table->string('image')->nullable();
            $table->string('name')->nullable();
            $table->string('user_id')->nullable();
            $table->string('post_status')->nullable();
            $table->string('usertype')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('posts');
    }
};


// End Table File



Routes Web.php

<?php

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\AdminController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', [HomeController::class,'homepage']);

// Route::get('/home',[HomeController::class,'index']);



// Route::get('/dashboard', function () {
    //     return view('dashboard');
    // })->middleware(['auth', 'verified'])->name('dashboard');
    
Route::get('home', [HomeController::class, 'index'])->middleware('auth')->name('home');
// Route::get('post',[HomeController::class,'post'])->middleware(['auth', 'admin']);


Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__.'/auth.php';
Route::get('post_page', [AdminController::class, 'post_page']);

// too add data
Route::post('add_post', [AdminController::class, 'add_post']);

Route::get('show_post', [AdminController::class, 'show_post']);

Route::get('delete_post/{id}', [AdminController::class, 'delete_post']);

Route::get('edit_page/{id}', [AdminController::class, 'edit_page']);

Route::post('update_post/{id}', [AdminController::class, 'update_post']);

Route::get('post_details/{id}', [HomeController::class, 'post_details']);

// End Routes
                
  Answered by CDL

You are passing $title variable to $post object but it's not define at the top. so you need first store into the title variable and then pass it to the post object.

$title = $request->title;


$post->title = $title;


Then this will work, also make sure to pass the post title using html form as you are passing description.

Solved
  Comment   Share
0 Likes   0 Comments