How does Laravel protects itself from html injection? - php

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

Related

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.

add Product size to cart (laravel shoppingcart)

I have followed a tutorial to create a laravel shopping cart https://www.youtube.com/watch?v=Jzi6aLKVw-A&list=PLEhEHUEU3x5oPTli631ZX9cxl6cU_sDaR&index=3 and composer https://github.com/hardevine/LaravelShoppingcart
I want make button select size product
CartController.php
public function store(Request $request)
{
$duplicates = Cart::search(function ($cartItem, $rowId) use ($request) {
return $cartItem->id === $request->id;
});
if ($duplicates->isNotEmpty()) {
return redirect()->route('cart.index')->with('success_message', 'Item is already in your cart!');
}
Cart::add($request->id, $request->name, 1, $request->price, $request->data)->associate('App\Product');
return redirect()->route('cart.index')->with('success_message', 'Item was added to your cart!');
}
Product.blade.php
<h1 class="topic">{{ $product->name}}</h1>
<h4>{{ $product->price }}</h4>
<div class="size-b">
<div class="title">
<h5>Trousers</h5>
</div>
<form action="{{ route('cart.store') }}" method="POST">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{ $product->id }}" >
<input type="hidden" name="name" value="{{ $product->name }}" >
<input type="hidden" name="price" value="{{ $product->price }}" >
<div class="btn-group" role="group" aria-label="Basic">
<button> <input type="checkbox" name="data" value="S" class="btn btn-secondary"><span>S</span></button>
<button><input type="checkbox" name="data" value="M" class="btn btn-secondary">M</button>
<button><input type="checkbox" name="data" value="L" class="btn btn-secondary">L</button>
<button> <input type="checkbox" name="data" value="XL" class="btn btn-secondary">XL</button>
</div>
<button type="submit" class="btn btn-secondary">ซื้อสินค้า</button>
</form>
cart.blade.php
#if (Cart::count() >0 )
<h4>{{ Cart::count() }}Item(s) in cart</h4>
<div class="table-cart-destop">
#foreach (Cart::content() as $item)
<div class="row">
<div class="col-lg">
<div class="remove">
<form action="{{ route('cart.destroy', $item->rowId) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-secondary">X</button>
</form>
</div>
</div>
<div class="col-lg-2">
<div class="image-border">
<img src="{{ $item->model->photo1}}" class="img-cart" alt="product-cart">
</div>
</div>
<div class="col-lg-5">
<div class="content">
<h4>{{ $item->model->name}}</h4>
<p>{{ $item->model->details}}</p>
</div>
</div>
<div class="col-lg">
<p class="size">size {{ $item->model->data }} </p>
</div>
<div class="col-lg">
<div class="quality">
<form action="">
<div class="form-row align-items-center">
<div class="col-auto my-1">
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect">
<option selected>1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="3">5</option>
</select>
</div>
</div>
</form>
</div>
</div>
<div class="col-lg">
<p class="size">{{ $item->data}}฿</p>
</div>
</div>
#endforeach
<div class="tatal">
<p>ยอดชำระเงินทั้งหมด</p>
<p class="price-tatal">{{ Cart::subtotal() }}฿</p>
</div>
<div class="buy-now">
<a href="{{ route('checkout.index') }}">
<button type="submit" class=" btn btn-secondary">สั่งซื้อสินค้า</button>
</a>
</div>
I try dd($request->all());
run php artisan serve --> size product (don't show)

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();

Passing dates from form into blade view

I have a shopping cart that has a pick-up and return date option that needs to be selected. I cannot get either of them to be echoed into the cart view when they are selected. I am using the Laravel Shopping Cart library to build the cart, which has an array for the extra options. I have passed the values into there to pass into the view, but it doesn't seem to work.
Here is the form mark:
<form action="{{ route('cart.store') }} " method="POST">
{{ csrf_field() }}
<fieldset>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="pickupDate" style="float:left;">Pick Up Date</label>
<input type="date" name="pickupDate" id="pickupDate" class="pickupDate"/>
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="returnDate" style="float:left;">Return Date</label>
<input type="date" name="returnDate" id="returnDate" class="return" />
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1of2" style="float: left;">
<label class="label" for="location" style="float:left;">Pick Up Location</label>
<select name="location" id="location" class="location">
<option>please choose</option>
<option value="bakersfield">Bakersfield</option>
<option value="chico">Chico</option>
<option value="fresno">Fresno</option>
<option value="hayward">Hayward</option>
<option value="manteca">Manteca</option>
<option value="oakley">Oakley</option>
<option value="redwood_city">Redwood City</option>
<option value="sacramento">Sacramento</option>
<option value="salinas">Salinas</option>
<option value="san_jose">San Jose</option>
<option value="san_jose_fusion">San Jose Fusion</option>
<option value="santa_rosa">Santa Rosa</option>
</select>
</div>
</div>
</fieldset>
<input type="hidden" name="id" value="{{ $rental->id }}">
<input type="hidden" name="title" value="{{ $rental->title }}">
<input type="hidden" name="pickupDate" value="{{ $rental->pickupDate }}">
<input type="hidden" name="returnDate" value="returnDate">
<div class="buttons">
<div class="back">
<button class="primary button" type="submit">Add to Cart</button>
</div>
</div>
</form>
Here is the cart view:
<article>
#if(session()->has('success_message'))
<div class="alert alert-success">
{{ session()->get('success_message') }}
</div>
#endif
<h1>Shopping Cart</h1>
#if(count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#if (Cart::count() > 0)
<h2>{{ Cart::count() }} item(s) in Shopping Cart</h2>
<div>
<div>
#foreach (Cart::content() as $item)
<fieldset>
<article class="js-cart-product">
<p class="prod-title">Name: {{$item->model->name}} </p>
<p class="pu-date">Pick up date: {{ ($item->options->has('pickupDate') ? $row->options->pickup : '') }} </p>
<p class="rtn-date">Return Date: {{ ($item->options->has('returnDate') ? $row->options->pickup : '') }}</p>
<p class="loc">Location: {{$item->location}}</p>
<form action="{{ route('cart.destroy', $item->rowId)}}" method="POST">
{{csrf_field()}}
{{method_field('DELETE')}}
<div class="buttons">
<div class="back">
<button class="primary button" type="submit">Delete Item</button>
</div>
</div>
<!-- <div class="cart__footer">
<p class="cart__text">
<a class="button" href="#" title="Buy products">
Check Out
</a>
</p>
</div> -->
</form>
</article>
</fieldset>
#endforeach
</div>
</div>
#else
<h3>No items in Cart!</h3>
Return to Rental Equipment
#endif
</div>
</article>
And here is the create item part of the controller:
public function store(Request $request)
{
$duplicates = Cart::search(function ($cartItem, $rowId) use ($request) {
return $cartItem->id === $request->id;
});
if ($duplicates->isNotEmpty()) {
return redirect()->route('cart.index')->with('success_message', 'Item is already in your cart!');
}
$this->validate($request, array(
'location'=>'required',
));
Cart::add($request->id, $request->title, 1, $request->location, $options = ['pickup' => 'pickupDate', 'returnDate' => 'returnDate'])
->associate('App\Rental');
Session::flash('success', 'The item was successfully save!');
return redirect()->route('cart.index');
}
From your form blade
<form action="{{ route('cart.store') }} " method="POST">
{{ csrf_field() }}
<fieldset>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="pickupDate" style="float:left;">
Pick Up Date
</label>
<input type="date" name="pickupDate" id="pickupDate" class="pickupDate"/>
</div>
</div>
<div class="formrow" style="margin-right: -10;">
<div class="formitem col1" style="margin-right: -10">
<label class="label req" for="returnDate" style="float:left;">Return Date</label>
<input type="date" name="returnDate" id="returnDate" class="return" />
</div>
</div>
then in the bottom somewhere of your master/app blade you put
<script>
$(function() {
$("#pickupDate" ).datepicker({dateFormat: 'dd/mm/yyyy'});
});
</script>
<script type="text/javascript">
$(function() {
$("#returnDate").datepicker({dateFormat: 'dd/mm/yyyy'});
});
</script>
be sure to include these necessary files jquery.min.js, and if you use bootstrap then you need bootstrap.min.js, bootstrap-datepicker.min.js
Please let me know if it works

error while retrieving data from the database

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!!}

Categories