error while retrieving data from the database - php

i was trying to display news with its comments, but one of the headlines is giving me an error,but others are working, i have a table of news and i have a table of news_comments, some of the news are being retrieved perfectly with their comments but this one is giving me an error.
#extends('layouts.app')
#section('content')
<div class="container">
{{$news->title}}<br/>
#foreach($news->news_pictures as $news_picture)
<img src="{{asset($news_picture->pictures)}}" width="200"><br/>
#endforeach
{!! $news->body !!} <br/>
<small>written on{{$news->created_at}} by {{$news->user->name}} </small>
</div>
#foreach($news->news_comments as $news_comment)
#if(!Auth::guest())
<div class="well">
{!! $news_comment->user->name!!}
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
#else
<div class="well">
{{$news_comment->commentor}}<br/>
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
#endif
#endforeach
#if(!Auth::guest())
<form action="{{action('NewsController#AddComments',[$news->id])}}" method="post">
{{csrf_field()}}
<div class="container">
<textarea type="text" class="form-control" name="comments" placeholder="your comment"></textarea>
<button class="btn btn-primary" >post</button>
</div>
</form>
#else
<form action="{{action('NewsController#AddComments',[$news->id])}}" method="post">
{{csrf_field()}}
<div class="container">
<input type="text" class="form-control" placeholder="your name" name="commentor">
<textarea type="text" class="form-control" name="comments" placeholder="your comment"></textarea>
<button class="btn btn-primary" >post</button>
</div>
</form>
#endif
#endsection

This error occur when the property you want to access that doesn't exist so you should check property isset or not
Here is the example
#if (!is_null($news_comment->user))
<div class="well">
{!! $news_comment->user->name!!}
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
#endif
OR
#if (isset($news_comment->user->id) && isset($news_comment->user->name))
<div class="well">
{!! $news_comment->user->name!!}
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
#endif

Try to use: with () on user
{!! $news_comment->user()->name!!}
Instead of:
{!! $news_comment->user->name!!}

Related

No response after clicking on Create Gallery button

Route::group(['middleware' =>'auth'], function(){
Route::prefix('/user/')->group(function(){
Route::get('galleries/create', [GalleryController::class, 'gallerycreate'])->name('galleryCreate');
Route::post('galleries/store', [GalleryController::class, 'gallerystore'])->name('galleryStore');
});
<div class="card-header">Create New Gallery</div>
<div class="card-body">
<form action=" {{ route('galleryStore') }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
#csrf
<div class="row">
<div class="col-md-12">
<label for="description">Gallery Description</label>
<textarea name="description" rows="3" class="form-control"></textarea>
</div>
</div>
<br>
<button class="btn btn-primary" type="submit">Create Gallery</button>
</form>
</div>
From what I can see it looks like you have included two csrf fields. {{ csrf_field() }} and #csrf This shouldn't be a big issue but I would expect this to throw an error.
Secondly the GalleryStore action in the GalleryController does not return anything. The action should include something like return view('some.view')->with(['data' => $data]);

Laravel post form is not submitting?

I want to make a form where you select what exercises you have done and afterwards add them to the database, but the problem is that I can't even submit my post form. The button does nothing when it is pressed.
This is my view file:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-body">
<h2>Excercise - {{$excercises[0]->hnro}}</h2>
<hr>
<div class="col-md-10">
#if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
#endif
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
<form class="form-horizontal" name="form" method="POST" action="{{ route('storeScores') }}">
<div class="form-group row">
<div class="col-md-12">
<input type="url" name="url" id="url" class="form-control" placeholder="Copy the URL here" required autofocus>
</div>
</div>
</div>
#foreach($excercises as $excercise)
<div class="row">
<div class="form-group col-md-3 col-md-offset-1">
<select class="form-control" id="select{{$excercise->tnro}}" name="teht[]" required>
<option value="">Excercise {{$excercise->tnro}}</option>
<optgroup label="Choose your points">
#for ($i=0; $i<=$excercise->maxpist;$i++)
<option value="{{$i}}">{{$i}} points</option>
#endfor
</optgroup>
</select>
</div>
</div>
#endforeach
<div class="form-group row">
<div class="col-md-5 col-md-offset-1">
<button type="submit" name="save" class="btn btn-
primary">Tallenna</button>
</div>
</div>
<input type="hidden" name="hnro" value="">
<input type="hidden" name="student_id" value="">
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
This is my route:
Route::get('/addscoreform','HarkkaController#showExcercises');
Route::post('/addscoreform','HarkkaController#storeScores')->name('storeScores');
And this is my controller:
public function storeScores(Request $request)
{
return redirect()->back()->with('success','Form has been sent...');
}
My problem is that I can't even display the success message after submit! It's like the button doesn't work!
It seems like you have tag nesting issues.
<form ...>
<div class="form-group row">
<div class="col-md-12">
<input type="url" name="url" id="url" class="form-control" ...>
</div>
</div>
</div>
...
</form>
What is the third closing? Ensure that the opening and closing tags are nested properly.
A few things which might help organize this better and make it easier to maintain are to eliminate any tags you can, and to break things down into smaller subviews and include statements as much as possible. Subviews are described at https://laravel.com/docs/master/blade#including-subviews.

Keep dropdown menu selected laravelcollective/html

I have a dropdown menu like this:
{!! Form::open(['method'=>'get']) !!}
<div class="row">
<div class="col-sm-4 form-group">
{!! Form::select('sort',[''=>'Choose Sort','asc'=>'Ascending','desc'=>'Descending'],null,['class'=>'form-control','onChange'=>'form.submit()']) !!}
</div>
<div class="col-sm-5 form-group">
<div class="input-group">
<input class="form-control" id="search"
value="{{ request('search') }}"
placeholder="Search name" name="search"
type="text" id="search"/>
<div class="input-group-btn">
<button type="submit" class="btn btn-warning">Search</button>
</div>
</div>
</div>
</div>
{!! Form::close() !!}
When I choose a item like "Ascending", my page reloaded and the dropdown menu return into "Choose Sort". I want to keep my selected value on dropdown menu.
How can I do that? Thank you very much!
Its simple just need to pass variable
Change Your route as per your requirement
In Your View
{!! Form::open(['method'=>'get','route' => 'document.sort']) !!}
<div class="row">
<div class="col-sm-4 form-group">
{!! Form::select('sort',[''=>'Choose Sort','asc'=>'Ascending','desc'=>'Descending'],isset($sortvalues) ? $sortvalues : '',['class'=>'form-control','onChange'=>'form.submit()']) !!}
</div>
<div class="col-sm-5 form-group">
<div class="input-group">
<input class="form-control" id="search"
value="{{ request('search') }}"
placeholder="Search name" name="search" value="{{ isset($searchvalues) ? $searchvalues : ''}}"
type="text" id="search"/>
<div class="input-group-btn">
<button type="submit" class="btn btn-warning">Search</button>
</div>
</div>
</div>
</div>
{!! Form::close() !!}
And my route
Route::any('/sort', 'DocDocumentController#sorDocument')->name('document.sort');
And finnaly in Controller
public function sorDocument(Request $request)
{
$docDocuments = DocDocument::latest()->paginate(20,['*'],'documentPage');
$searchvalues = $request->search;
$sortvalues = $request->sort;
$viewShare = array_keys(get_defined_vars());
return view('docdocuments.index', compact($viewShare));
}

Trying to get property 'serino' of non-object

I don't want to use "id" to edit the product, I want to use the "serial no" column. But I get the following error:
Trying to get property 'serino' of non-object
Product.php
public function edit($serino)
{
$product= DB::select('select * from products where serino=?',[$serino]);
return view("products/edit",compact("product"));
}
index.blade.php
<a href="{{route('products.edit',$product->serino)}}">
<button class="btn btn-info btn-sm"><i class="fas fa-edit"></i> Edit</button>
</a>
edit.blade.php
<form class="form-horizontal" action="{{route('products.update',$product->serino)}}" method="post">
{!! csrf_field() !!}
{!! method_field('put') !!}
<div class="form-group">
<div class="col-sm-12">
<input type="text" class="form-control" value="{{$product->name}}" name="name" placeholder="Product Name">
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="text" class="form-control" value="{{$product->category}}" name="category" placeholder="Product Category">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-12">
<button type="submit" class="btn btn-success">Save</button>
</div>
</div>
</form>
Good Work..
Your query will return you collection/array of result, no just one Product.
You can use sometihing like this:
$product = DB::table('products')->where('serino', $serino)->first();

How does Laravel protects itself from html injection?

Hello I'm new to laravel and I tried to add HTML to a <h6> field like below image .
and it appears in the page source like this
My question is why it doesn't render in the browser ? What has Laravel done here ?
My Blade code :
#extends('layouts')
<div class="row">
<div class='col-md-6 offset-md-3' >
#section('content')
<h1>{{$card->title}}</h1>
{{$card->created_at}}
<br>
<ul class="list-group">
#foreach($card->notes as $note)
<li class="list-group-item">
<h6>{{$note->body}}</h6> <button class="btn btn-info btn-sm" onclick="togglediv('divform{{$note->id}}')" >Edit</button>
<div class="col-md-6 offset-md-3" id="divform{{$note->id}}" style="display: none;">
<hr>
<form action="/notes/{{$note->id}}/edit" method="POST">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="exampleInputPassword1"><h6>Edit the note</h6></label>
<input type="text-area" name="body" class="form-control" id="body" placeholder="{{$note->body}}">
</div>
<button type="submit" class="btn btn-success btn-sm">Done</button>
</form>
</div>
</li>
#endforeach
<script type="text/javascript">function togglediv(id) {
var div = document.getElementById(id);
div.style.display = div.style.display == "none" ? "block" : "none";
}</script>
</ul>
</div>
</div>
#endsection
#section('footer')
<br>
<div class="col-md-6 offset-md-3">
<hr>
<form action="/cards/{{$card->id}}/notes" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="exampleInputPassword1"><h3>Add a new note</h3></label>
<input type="text-area" name="body" class="form-control" id="body" placeholder="Body">
</div>
<button type="submit" class="btn btn-primary">Add Note</button>
</form>
</div>
#endsection
Your html is escaped ,if you don't want that use the following syntax to output the html
{!!$note->body!!}
more info

Categories