bahdcoder / Laravel-5.3-and-Vue-js-2.0-social-network

Social network built with Laravel 5.3 and Vue js 2.0
134 stars 91 forks source link

$info is an undefined variable? #6

Closed rtd62 closed 7 years ago

rtd62 commented 7 years ago

Hi! I just watched Social network in Laravel 5.3 and Vuejs 2.0 #5 - Updating user's profile and I reached an error at the end of the video. I am working in Laravel 5.3.31. I followed the instructions explicitly and followed along as much as I could. I think it has something to do with {{ $info->location }} and {{ $info->about }} in edit.blade.php, which would tell me that $info is an undefined variable, am I right? Any help would be much appreciated! 🍻

edit.blade.php threw this error at me:

ErrorException in 2a1e7b367cf4a444165b54ce29f9e8cab797dfa4.php line 12:
Undefined variable: info (View: /var/www/html/site-name/resources/views/profiles/edit.blade.php
)

Here are the files below:

edit.blade.php

@extends('layouts.sessions')

@section('title', 'Edit Profile')

@section('content')
    <div class="container">
        <div class="col l6 center">
            <div class="card">
                <form action="{{ route('profile.update') }}" method="POST" enctype="multipart/form-data">
                    {{ csrf_field() }}
                    <div class="form-group">
                        <label for="location">Location</label>
                        <input type="text" name="location" value="{{ $info->location }}" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <label for="about">About Me</label>
                        <textarea name="about" id="about" cols="30" rows="10" class="form-control" required>{{ $info->about }}</textarea>
                    </div>
                    <div class="form-group">
                        <p class="center">
                            <button class="btn btn-round blue" type="submit">Save</button>
                        </p>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection

ProfileController.php

<?php

namespace App\Http\Controllers;

use Auth;
use Session;
use App\User;
use Illuminate\Http\Request;

class ProfileController extends Controller
{
    public function index($slug)
    {
        $user = User::where('slug', $slug)->first();

        return view('profiles.profile')
            ->with('user', $user);
    }

    public function edit()
    {       
        return view('profiles.edit')->with('info', Auth::user()->profile);
    }

    public function update(Request $request)
    {
        $this->validate($request, [
            'location' => 'required',
            'about' => 'required|max:255'
        ]);

        Auth::user()->profile()->update([
            'location' => $request->location,
            'about' => $request->about
        ]);

        Session::flash('success', 'Profile updated!');

        return redirect()->back;
    }
}

web.php

...
Route::group(['middleware' => 'auth'], function(){
    Route::get('/profile/{slug}', [
        'uses' => 'ProfileController@index',
        'as' => 'profile'
    ]);
    Route::get('/profile/edit/profile', [
        'uses' => 'ProfileController@edit',
        'as' => 'profile.edit'
    ]);
    Route::post('/profile/update/profile', [
        'uses' => 'ProfileController@update',
        'as' => 'profile.update'
    ]);
});
...
astritzeqiri commented 7 years ago

@rtd62 You can try clearing your cache views by running php artisan view:clear

rtd62 commented 7 years ago

@astritzeqiri Clearing overall app cache and using view:clear had no effect.

rtd62 commented 7 years ago

So it turns out there's something monumentally screwed up in my mysql database. I ran this on a fresh install using sqlite and it worked fine.