i have this code and when i try to access laravel.dev/cats/create a page appears:
Whoops, looks like something went wrong.
this is my route.php code(important parts):
Route::model('cat', 'Cat');
Route::get('cats/create', function() {
$cat = new Cat;
return View::make('cats.edit')
->with('cat', $cat)
->with('method', 'post');
});
Route::post('cats', function(){
$cat = Cat::create(Input::all());
return Redirect::to('cats/' . $cat->id)
->with('message', 'Successfully created page!');
});
Route::get('cats/{id}', function($id) {
$cat = Cat::find($id);
return View::make('cats.single')
->with('cat', $cat);
});
and this is my cats/edit.blade.php code:
#extends('master')
#section('header')
← Cancel
<h2>
#if($method == 'post')
Add a new cat
#elseif($method == 'delete')
Delete {{$cat->name}}?
#else
Edit {{$cat->name}}
#endif
</h2>
#stop
#section('content')
{{Form::model($cat, array('method' => $method, 'url'=>
'cats/'.$cat->id))}}
#unless($method == 'delete')
<div class="form-group">
{{Form::label('Name')}}
{{Form::text('name')}}
</div>
<div class="form-group">
{{Form::label('Date of birth')}}
{{Form::text('date_of_birth')}}
</div>
<div class="form-group">
{{Form::label('Breed')}}
{{Form::select('breed_id', $breed_options)}}
</div>
{{Form::submit("Save", array("class"=>"btn btn-default"))}}
#else
{{Form::submit("Delete", array("class"=>"btn btn-default"))}}
#endif
{{Form::close()}}
#stop
i dont now where the problem , i have the same code for edit and delete but those guys work correctly but this one no!
this is the error that debugger reports:
Trying to get property of non-object (View: D:\Xampp\htdocs\laravel\app\views\cats\single.blade.php)
<?php echo e($cat->name); ?>
error mention that line.
Thanks for Your helps:))))
Edit:
and this is single.blade.php:
#extends('master')
#section('header')
<?php// dd($cat); ?>
Back to overview
<h2>
{{{$cat->name}}}
</h2>
<a href="{{url('cats/'.$cat->id.'/edit')}}">
<span class="glyphicon glyphicon-edit"></span> Edit
</a>
<a href="{{url('cats/'.$cat->id.'/delete')}}">
<span class="glyphicon glyphicon-trash"></span> Delete
</a>
Last edited: {{$cat->updated_at}}
#stop
#section('content')
<p>Date of Birth: {{$cat->date_of_birth}} </p>
<p>
#if($cat->breed)
Breed:
{{link_to('cats/breeds/' . $cat->breed->name,
$cat->breed->name)}}
#endif
</p>
#stop
i don't know why when i was go to cats/create i passes to single.blade.php i think this is where i'm going wrong but no idea where in code i do that
The problem is in:
Route::get('cats/{id}', function($id) {
$cat = Cat::find($id);
return View::make('cats.single')
->with('cat', $cat);
});
It seems that there is no record found with $id, so $cat is null here and it won't work in Blade.
You should check what's the current url (what's the $id value) and make sure that there is a record with such $id in table).
Related
It works really good, but when I've created new page something goes wrong and now my blog view doesn't show data, but blogs view still correctly show data . I am trying to show detailed data of each blog when user click on button "Details"
MainController:
public function blog(Blogs $blog)
{
return view('blog', compact('blog'));
}
public function blogs()
{
return view('blogs',['blogs' => Blogs::all(),]);
}
blogs.blade.php:
#extends('layouts.master')
#section('title', __('main.blogs'))
#section('content')
<div class="row">
#foreach($blogs as $blog)
#include('layouts.cardBlog', compact('blog'))
#endforeach
</div>
#endsection
and cardBlog.blade.php:
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="{{($blog->image) }}">
<div class="caption">
<h3>{{ $blog->title }}</h3>
<p>{{ $blog->body }}</p>
<p>
<a href="{{route('blog', $blog->id) }}"
class="btn btn-default"
role="button">#lang('main.more')</a>
#csrf
</p>
</div>
</div>
</div>
blog.blade.php
#extends('layouts.master')
#section('title', __('main.blogs'))
#section('content')
<h1>{{ $blog->title}}</h1>
<img src="{{$blog->image }}">
<p>{{ $blog->body }}</p>
#endsection
web.php:
Route::get('/', 'MainController#index')->name('index');
Route::get('/categories', 'MainController#categories')->name('categories');
Route::get('/about', 'MainController#aboutus')->name('about');
Route::get('/contact-us', 'ContactUSController#contactUS')->name('contact-us');
Route::post('contactus', ['as'=>'contactus.store','uses'=>'ContactUSController#contactSaveData']);
Route::get('/contacts', 'MainController#contacts')->name('contacts');
Route::get('/blogs', 'MainController#blogs')->name('blogs');
Route::get('/blog/{id}', 'MainController#blog')->name('blog');
Route::get('/intership', 'MainController#intership')->name('intership');
Route::get('/{category}', 'MainController#category')->name('category');
Route::get('/{category}/{product}/{skus}', 'MainController#sku')->name('sku');
Route::post('subscription/{skus}', 'MainController#subscribe')->name('subscription');
What's is the error that it shown?
I suggest use "compact" in this line code
return view('blogs',['blogs' => Blogs::all(),]);
Something like this
public function blogs()
{
$blogs = Blogs::all();
return view('blogs',compact('blogs'));
}
Try this
public function blog($id)
{
$blog = Blogs::findOrFail($id)
return view('blog', compact('blog'));
}
public function saveUserToGroup(Request $request, $username){
if (!$this->secure($username)) return redirect('/404');
$member = new UserHobby();
$person = $request->input('person');
$group = $this->group;
$member->user_id = $person->id;
$member->hobby_id = $group->id;
if ($member->save()) {
$request->session()->flash('alert-success', 'Your friend has been added to the group!');
}else{
$request->session()->flash('alert-danger', 'Something went wrong!');
}
return Redirect::back()->withErrors(['msg', 'Success']);
}
<form action="/social/{username}/save/group" method="POST">
#csrf
<table id="modal-table">
#foreach($user_list->get() as $f)
<tr>
<td>
<div class="image">
#if($f->follower->avatar_location)
<img src="{{asset('storage/'.$f->follower->avatar_location)}}" class="img-circle" style="max-height:50px;" />
#else
<img src="{{url('/')}}/assets/media/icons/socialbuttons/user.png" class="img-circle" style="max-height:50px;"/>
#endif
</div>
<div class="detail">
#if($f->follower->verified == 1)
{{ $f->follower->name }}<img id="verified_img_sm_mess_list" src="{{url('/')}}/assets/media/icons/socialbuttons/octagonal_star.png" alt="Recensioni">
#else
<h3>{{ $f->follower->name }}</h3>
#endif
<small id="mess_list_uname">{{ '#'.$f->follower->username }}</small>
</div>
<button type="submit" name="person" value="person" class="btn-link">Go</button>
</td>
</tr>
#endforeach
</table>
</form>
Route::post('/social/{username}/save/group', [GroupController::class, 'saveUserToGroup'])->name('socialprofile.save.user.group');
Hey guys, I'm new to all this, and not sure if I'm calling the route incorrectly or wrong application of my controller function. Obviously I'm trying to add a user from a list into a group. Please Help! I've been a little lost with this and looking for some help to understand where my errors are. Many thanks!
EDIT**
now log reading..
"'hobby_id' cannot be null"..
I have tried to change the variable to take the group id. and that reads error "trying to get id of non-object"
but am I not assigning hobby_id as the group_id?
The problem is that I want to add into 'hobby_id' the 'id' of the current group which this function is being called.
I'm working with Laravel 5.1
I have a template for a page that was previously working fine. I could hit my route and the page would be rendered as expected. At some point the page started just displaying the text contained in the blade template rather than rendering my page. To my knowledge nothing had changed in my routes file, controller or blade. Is there anything that is known to cause this to happen? I've tried updating permissions, using a different method to call the view and creating a new view file all together.
The route:
Route::group(['prefix' => '/admin'], function() {
Route::group(['prefix' => '/reactor-rules'], function() {
Route::get('/visual-rules/{track_id?}', [
'as' => 'broker_visual_reactor_rules',
'uses' => 'ReactorRulesController#visualRules'
]);
});
});
ReactorRulesController#visualRules:
public function visualRules(IReactorTrackDAO $reactorTrackDAO, $id = 0)
{
$export = new ReactorExport();
$tracks = $export->getReactorTracks();
if ($id == 0) {
$eventsArray = [];
}
else {
$eventsArray = $export->getArrayForTrack($id);
}
return $this->renderView('enterpriseBroker::reactor-rules.visual-rules', compact('eventsArray','tracks','reactorTrackDAO'));
}
My blade:
#extends($layout)
{{ SEO::setPageTitle(trans('enterpriseBroker::reactorRules.title'), false) }}
#section('content')
<br>
<br>
#if(empty($eventsArray))
<p>Please select a track below to view/export rules.</p>
#foreach($tracks as $track)
{{$track->name}}<br>
#endforeach
#else
#foreach($eventsArray as $track => $events)
<h4>{{$track}}</h4>
<a href="{{url().'/admin/visual-rules/'.$reactorTrackDAO->findByName($track)->id.'/download'}}" class="btn btn-primary" download>Export</a><br>
#foreach($events as $id => $event)
#if (count($event['rules']) > 0)
<div id="{{$track.'-'.str_replace(' ','_',$event['name'])}}">
<div class="col-lg-12">
<hr/>
<div class="col-lg-2">
<h4>{{$event['name']}}</h4>
</div>
<div class="col-lg-10 border border-primary">
#foreach($event['rules'] as $ruleId => $rule)
<h5>{{$rule['name']}}</h5>
<div class="col-lg-6">
<h6>Conditions to be met:</h6>
#foreach($rule['conditions'] as $condition)
<p>{{$condition}}</p>
#endforeach
</div>
<div class="col-lg-6">
<h6>Actions to run:</h6>
#foreach($rule['actions'] as $action)
<p>{!!$action!!}</p>
#endforeach
</div>
#endforeach
</div>
</div>
</div>
#endif
#endforeach
#endforeach
#endif
#endsection
#section('scripts')
#endsection
This is how it originally appeared:
This is how it currently appears:
It seems to be error with $layout
Try to test it using a hard coded value as below:
#extends('layouts.app')
and run below command
php artisan view:clear
I resolved the issue by creating a new blade file, pasting the contents of the old one in and pointing my controller to the new view. I'm not sure how this resolved the issue.
I have 2 questions and I need your help.First of all, I have made a delete route for comments, but other users logged can also delete comments from direct link...link.com/deleteComment/id. How to make this available only for owner of the comment?The owner id is saved in database and can be accessed with {{ $comment->user_id }}.
Second problem...In my view, when I click on a photo which have no comments, I'm receiving undefined variable comment, but I dont know why because on photos with comments, I have no problem with that.Can I make something like if comments != empty, dont show it or something like that?
CommentsController:
public function store(Request $request, $post_id)
{
$this->validate($request, array(
'comment' => 'required|min:5|max:2000',
));
$post = Post::find($post_id);
$comment = new Comment();
$comment->username = Auth::user()->username;
$comment->email = Auth::user()->email;
$comment->user_id = Auth::user()->id;
$comment->comment = $request->comment;
$comment->approved = true;
$comment->post()->associate($post);
$comment->save();
Session::flash('message', "Message posted successfully!");
return Redirect::back();
}
PostsController:
public function delete($id){
DB::table('posts')->where('id',$id)->delete();
return redirect('/profile/' . auth()->user()->id);
}
My view
#foreach($post->comments as $comment)
<div class="comment d-flex ">
<p><strong><a class="text-dark" href="/profile/{{ $comment->user_id }}">{{ $comment->username}}</a>: </strong> {{ $comment->comment}}</p>
#can('update', $post->user->profile)
<div class="dropdown col-md-6">
<button type="button" class="btn btn-primary dropdown-toggle btn-sm" style="background-color: #ffffff00;border: 1px solid #555;color: black;padding: 0 5px" data-toggle="dropdown">
Select
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Edit comment</a>
<a class="dropdown-item" title="Options" style="text-decoration: none;" href="/deleteComment/{{$comment->id}}">Delete comment</a>
</div>
</div>
</div>
#endcan
#endforeach
My route
Route::post('comments/{post_id}', ['uses' => 'CommentsController#store', 'as' => 'comments.store']);
Route::get('/deleteComment/{id}', 'CommentsController#delete');
Try this to restrict the deletion only for the authenticated user who owns the comment:
/**
* Comments Controller Method Delete
*/
public function delete($id){
if(!DB::table('comments')->where('id',$id)->where('user_id',auth()->user()->id)->delete()){
Session::flash('remove', "You do not have permission to delete the comment!");
}else{
Session::flash('remove', "Message removed successfully!");
}
return Redirect::back();
}
For your second question, I think that what happens is that by not having comments you are using the variable without results.
You can try to enclose the statement where you use the variable $comments using this.
For Controller or others files php
if (!$comment->isEmpty()) {
//your code
}
if ($comment->count()) {
//your code
}
if (count($comment)) {
//your code
}
For Blade
#if(!$comment->isEmpty())
//your code
#endif
#if($comment->count())
//your code
#endif
#if(count($comment))
//your code
#endif
I hope I could help you, if not, please attach more code where they appear exactly what he says, since comments and delete the picture, since I have not seen in the code you have attached. Thanks and good luck.
References
$comment->isEmpty
$comment->count() and count($comment)
Updated
<div class="row">
<div class="col-md-12">
#if(!$post->comments->isEmpty()) //****Added
#if($post->comments->count() > 0)
#foreach($post->comments as $comment)
<div class="comment d-flex ">
<p><strong><a class="text-dark"
href="/profile/{{ $comment->user_id }}">{{ $comment->username}}</a>:
</strong> {{ isset($comment->comment) ? $comment->comment : "--" }}</p>
#can('update', $post->user->profile)
<div class="dropdown col-md-6">
<button type="button" class="btn btn-primary dropdown-toggle btn-sm"
style="background-color: #ffffff00;border: 1px solid #555;color: black;padding: 0 5px"
data-toggle="dropdown">
Select
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Edit comment</a>
<a class="dropdown-item" title="Options" style="text-decoration: none;"
href="/deleteComment/{{$comment->id}}">Delete comment</a>
</div>
</div>
</div>
#endcan
#endforeach
#endif
#endif //****Added
</div>
</div>
Updated for delete if admin or no
/**
* Comments Controller Method Delete
*/
public function delete($id)
{
$comment = DB::table('comments')->where('id', $id):
if(!auth()->user()->admin){
$comment->where('user_id', auth()->user()->id);
}
if (!$comment->delete()) {
Session::flash('remove', "You do not have permission to delete the comment!");
} else {
Session::flash('remove', "Message removed successfully!");
}
return Redirect::back();
}
The first question can be done easily. In your destroy() function, just check comment owner:
// Check comment owner
if($comment->user_id != \Auth::id()){
return abort(401);
}
// Do logic code to delete comment.
The second question, you can check exist comment like this :
if(! $comments->isEmpty()) {
// Do logic code to show comment
}
I'm trying register in my app but this Error happened and I don't know why.
and my post.blade.php file has this code:
<div class="blog-post">
<h2 class="blog-post-title">
<a href="/posts/{{$post->id}}">
{{$post->title}}
</a>
</h2>
<p class="blog-post-meta">
{{$post->user->name }}
{{$post->created_at->toFormattedDateString()}}
</p>
{{$post->body}}
postcontroller has this code:
public function index()
{
$posts=Post::latest()->get();
return view('posts.index',compact('posts'));
}
and index file is:
#foreach($posts as $post)
#include('posts.post')
#endforeach
<nav class="blog-pagination">
<a class="btn btn-outline-primary" href="#">Older</a>
<a class="btn btn-outline-secondary disabled" href="#">Newer</a>
</nav>
</div><!-- /.blog-main -->
I think your code need to update like:
public function index()
{
$posts=Post::with('users')->latest()->get();
return view('posts.index',compact('posts'));
}
<p class="blog-post-meta">
{{$post->users->name }}
{{$post->created_at->toFormattedDateString()}}
</p>
Hope this work for you!
assume that you are including post.blade.php in index.blade.php file.
In above case you are not passing $post to post.blade.php. Your foreach loop will be like :
#foreach($posts as $post)
#include('posts.post', ['post' => $post])
#endforeach
I assume that you are including post.blade.php in index.blade.php file.
In above case you are not passing $post to post.blade.php. Your foreach loop will be like :
#foreach($posts as $post)
#include('posts.post', ['post' => $post])
#endforeach