I am trying to create a page where an user can put some data and save or post it. But when I try to visit my place adding url I get an error "ErrorException in PlaceController.php line 101: Trying to get property of non-object"
My Route:
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/','FrontView@index');
Route::get('/placesbydiv','FrontView@placesByDiv');
Route::get('/placesbydist','FrontView@placesByDist');
Route::get('/blog',['as' => 'blog', 'uses' => 'PostController@blog']);
//authentication
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
// check for logged in user
Route::group(['middleware' => ['auth']], function()
{
// show new post form
Route::get('new-post','PostController@create');
// save new post
Route::post('new-post','PostController@store');
// edit post form
Route::get('edit/{slug}','PostController@edit');
// update post
Route::post('update','PostController@update');
// delete post
Route::get('delete/{id}','PostController@destroy');
// display user's all posts
Route::get('my-all-posts','UserController@user_posts_all');
// display user's drafts
Route::get('my-drafts','UserController@user_posts_draft');
// add comment
Route::post('comment/add','CommentController@store');
// delete comment
Route::post('comment/delete/{id}','CommentController@distroy');
});
//users profile
Route::get('user/{id}','UserController@profile')->where('id', '[0-9]+');
// display list of posts
Route::get('user/{id}/posts','UserController@user_posts')->where('id', '[0-9]+');
// display list of posts by category
Route::get('category/{id}/posts','UserController@category_posts')->where('id', '[0-9]+');
// display single post
Route::get('/{slug}',['as' => 'post', 'uses' => 'PostController@show'])->where('slug', '[A-Za-z0-9-_]+');
//Places
Route::get('/place',['as' => 'place', 'uses' => 'PlaceController@place']);
// check for logged in user
Route::group(['middleware' => ['auth']], function()
{
// show new post form
Route::get('new-place','PlaceController@create');
// save new post
Route::post('new-place','PlaceController@store');
// edit post form
Route::get('edit/{slug}','PlaceController@edit');
// update post
Route::post('update','PlaceController@update');
// delete post
Route::get('delete/{id}','PlaceController@destroy');
// display user's all posts
Route::get('my-all-places','UserController@user_places_all');
// display user's drafts
Route::get('my-places-drafts','UserController@user_places_draft');
// add comment
//Route::post('comment/add','CommentController@store');
// delete comment
//Route::post('comment/delete/{id}','CommentController@distroy');
});
//users profile
//Route::get('user/{id}','UserController@profile')->where('id', '[0-9]+');
// display list of posts
Route::get('user/{id}/places','UserController@user_places')->where('id', '[0-9]+');
// display single post
Route::get('/{slug}',['as' => 'place', 'uses' => 'PlaceController@show'])->where('slug', '[A-Za-z0-9-_]+');
My Place controller:
<?php
namespace App\Http\Controllers;
use App\Places;
use App\User;
use App\Div;
use Redirect;
use App\Http\Controllers\Controller;
use App\Http\Requests\PlaceFormRequest;
use Illuminate\Http\Request;
class PlaceController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('home', array('page' => 'home'));
}
public function place(Request $request){
//fetch 5 posts from database which are active and latest
$places = Places::where('active',1)->orderBy('created_at','desc');
//page heading
$title = 'দর্শনীয় স্থান';
$search_key = $request->input('body');
if(!empty('$search_key')){
$places->where('body','LIKE','%'.$search_key.'%');
}
$places = $places->paginate(5);
//return home.blade.php template from resources/views folder
return view('place')->withPlaces($places)->withTitle($title);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create(Request $request)
{
// if user can post i.e. user is admin or author
if($request->user()->can_post())
{
$divs = \DB::table('divs')->lists('div', 'id');
return view('places.create')->with('divs', $divs);
}
else
{
return redirect('/')->withErrors('You have not sufficient permissions for writing post');
}
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(PlaceFormRequest $request)
{
$place = new Places();
$place->title = $request->get('title');
$place->url = $request->get('url');
$place->location = $request->get('location');
$place->gmap_location = $request->get('gmap_location');
$place->slug = $place->url;
$place->body = $request->get('body');
//$post->slug = str_slug($post->title);
// $place->author_id = $request->user()->id;
$place->div_id = $request->get('div');
$place->source_url = $request->get('source_url');
if($request->has('save'))
{
$place->active = 0;
$message = 'Place saved successfully';
}
else
{
$place->active = 1;
$message = 'Place added successfully';
}
$place->save();
return redirect('edit/'.$place->slug)->withMessage($message);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($slug)
{
$place = Places::where('slug',$slug)->first();
$div = \DB::table('divs')->where('id',$place->div_id)->first(); **//THIS IS THE ERROR LINE**
if(!$place)
{
return redirect('/')->withErrors('requested page not found');
}
// $comments = $post->comments;
return view('places.show')->withPlace($place)->withDiv($div);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Request $request,$slug)
{
$place = Places::where('slug',$slug)->first();
if($place && ($request->user()->can_post() || $request->user()->is_admin()))
return view('places.edit')->with('place',$place);
return redirect('/')->withErrors('you have not sufficient permissions');
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request)
{
$place_id = $request->input('place_id');
$place = Places::find($place_id);
if($place && ($request->user()->can_post() || $request->user()->is_admin()))
{
$title = $request->input('title');
$slug = str_slug($title);
$duplicate = Places::where('slug',$slug)->first();
if($duplicate)
{
if($duplicate->id != $place_id)
{
return redirect('edit/'.$place->slug)->withErrors('Title already exists.')->withInput();
}
else
{
$place->slug = $slug;
}
}
$place->title = $title;
$place->location = $request->get('location');
$place->gmap_location = $request->get('gmap_location');
$place->body = $request->input('body');
$place->source_url = $request->get('source_url');
if($request->has('save'))
{
$place->active = 0;
$message = 'Place saved successfully';
$landing = 'edit/'.$place->slug;
}
else {
$place->active = 1;
$message = 'Place updated successfully';
$landing = $place->slug;
}
$place->save();
return redirect($landing)->withMessage($message);
}
else
{
return redirect('/')->withErrors('you have not sufficient permissions');
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request, $id)
{
$place = Places::find($id);
if($place && ($request->user()->is_admin()))
{
$place->delete();
$data['message'] = 'Place deleted Successfully';
}
else
{
$data['errors'] = 'Invalid Operation. You have not sufficient permissions';
}
return redirect('/')->with($data);
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire