Pass data from form submit to controller via get Method - laravel

can anyone tell, what is wrong with the following way to pass the data from view->route->controller. Currently I get Missing required parameters for [Route: show.exclusion] [URI: exclusion/create/{id}]
This is my view part:
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">In welcher Gruppe möchten Sie User einladen?</div>
<div class="panel-body">
{!! Form::open(array('route'=>'show.exclusion', 'method'=>'get', 'id'=>'$group->idgroup')) !!}
<div class="form-group">
{{Form::label('choosegroup', 'Wähle eine Gruppe')}}
<select class="form-control m-bot15" name="idgroup">
#foreach($groups as $group)
<option id="{{ $group->idgroup }}">{{ $group->groupname }}</option>
#endforeach
</select>
<!--{{ csrf_field() }}-->
</div>
<div>
{{Form::submit('Search',['class' => 'btn btn-primary'])}}
<a class="btn btn-default btn-close" href="{{ route('home') }}">Cancel</a>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
This is my route...
Route::get('exclusion/create/{id}', 'ExclusionController#show')->name('show.exclusion');
And here you can see my controller function...
public function show($id)
{
$groupid = $id;
$members = V_Exclusion::where([['idgroup', $groupid],['groupadmin', Auth::id()]])->get();
$groups = V_Exclusion::where('groupadmin', Auth::id())->get();
return view('exclusions.createexclusion')
->with('members', $members)
->with('groups', $groups);
}

Try this.
In Route
Route::get('exclusion/create/{id}', 'ExclusionController#show');
{!! Form::open(array('route'=>['exclusion/create',$group->idgroup], 'method'=>'get', 'id'=>'$group->idgroup')) !!}

Related

Laravel search from multi select dropdown

I am trying to create a search filter using dropdown selections. The code only shows results when all dropdowns (make and model) are selected. But it should work even single dropdown (either make or model)is selected. At the moment, if I select a single dropdown, the result page shows "No Matching Data found". this is what I have done -
Controller
class CarFrontController extends Controller
{
public function index_classiccars(Request $request)
{
$filterMakes = DB::table('cars')->select('make')->distinct()->get()->pluck('make');
$filterModels = DB::table('cars')->select('model')->distinct()->get()->pluck('model');
$classiccar = Car::query();
if ($request->has('make')) {
$classiccar->where('make', $request->make);
}
if ($request->has('model')) {
$classiccar->where('model', $request->model);
}
return view(
'layouts.frontend.cars.classiccars_index',
[
'filterMakes' => $filterMakes, 'filterModels' => $filterModels,
'classiccars' => $classiccar->get()
]
);
}
public function store(Request $request)
{
return $request->all();
return view('layouts.frontend.cars.classiccars_index');
}
}
data blade
#forelse ($classiccars as $car)
<div class="row">
<div class="col-lg-5 col-md-5 col-pad">
<div class="car-thumbnail">
<a href="car-details.html" class="car-img">
<div class="price-box">
<span>£{{ $car->price }}</span>
</div>
#php $i=1; #endphp
#foreach ($car->join_caralbum as $image)
#if ($i>0)
<img class="d-block w-100" src="{{asset($image->image_location)}}" alt="car" height="225px" >
#endif
#php $i--; #endphp
#endforeach
</a>
<div class="carbox-overlap-wrapper">
<div class="overlap-box">
<div class="overlap-btns-area">
<a class="overlap-btn" href="{{ url('car_show/'.$car->id) }}">
<i class="fa fa-eye-slash"></i>
</a>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-7 col-md-7 col-pad align-self-center">
<div class="detail">
<h3 class="title">
{{ $car->make }}
</h3>
<ul class="custom-list">
<li>
{{ $car->model }} /
</li>
......
</ul>
<ul class="facilities-list clearfix">
.....
.....
....
</ul>
</div>
</div>
</div>
</div>
#empty
No matching data found
#endforelse
filter blade
{!! Form::open(['action'=>'CarFrontController#index_classiccars','method'=>'GET']) !!}
<select class="form-control" name="make" id="make">
<option> Make(All)</option>
#foreach ($filterMakes as $make)
<option value="{{ $make }}">{{ $make }}</option>
#endforeach
</select><br>
<select class="form-control" name="model" id="model">
<option> Model(All)</option>
#foreach ($filterModels as $model)
<option value="{{ $model }}">{{ $model }}</option>
#endforeach
</select><br>
{{ Form::submit('Update Search',['class'=>'btn btn-primary']) }}
Your query results empty because even if you don't select any of your options, you actually do. If options don't have a value, the value is the text you give it to the option.
In your case, if you select just a model, actually you are selected 'Make(All)' option too. You can see this simply by putting dd($request->all()) on your index_classiccars method.
For checking selects, you can give empty value to first(default) option and control it with request()->filled('input').

How do I change the route depending on selection in laravel

I'm trying to create a page that lets the user select a product and then when they press "add to cart" that product then gets sent to the cart page.
On my product page I can have the same item that has 2 or more codes attached to it, so I've created a selection that has the codes and the user then has to pick one of them
and then press "add to cart" button to go to the cart page.
So the way I have it at the moment is that the form grabs the id of the product page and not of the selection box.
What I would like to know is how would I go about changing the route depending on what I've selected.
Here is my code
<form action="{{ route('product.addToCart', ['id' => $product->id]) }}" method="POST">
{{ csrf_field() }}
<div class="row">
<div class="col-lg-12 pl-0">
<select class="form-control mb-2" id="supplier_code" name="supplier_code">
#foreach($parent_product as $parent)
<option value="{{ $parent->supplier_code }}">{{ $parent->supplier_code }}</option>
#if(count($parent->parent))
#foreach($parent->parent as $child)
<option value="{{ $child->supplier_code }}">{{ $child->supplier_code }}</option>
#endforeach
#endif
#endforeach
</select>
</div>
</div>
{{ Form::submit('Add to Cart', array('class' => "btn btn-dark btn-lg btn-block")) }}
</form>
This is my function
public function getAddToCart(Request $request, $id)
{
$menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
$contacts = Contact::all();
$product = Product::find($id);
$supplier_code = $request->supplier_code;
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id, $supplier_code);
$request->session()->put('cart', $cart);
return view('public.shopping-cart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice, 'menus_child' => $menus_child, 'contacts' => $contacts, 'supplier_code' => $supplier_code]);
}
This is my product list page where the user will click on a product to get its details.
<div class="col-xl-9 col-lg-8 col-md-12 product-list">
<div id="parent">
<div class="row grid-container">
#foreach($products as $product)
<?php
$cat_slug = "";
?>
#foreach($product->category as $category)
<?php
$cat_slug .= " ".$category->slug
?>
#endforeach
<?php
$product_image = getImagesArray($product->image);
?>
<div class="product_items col-xl-2 col-lg-4 col-md-4 col-sm-4 {{ $cat_slug }}">
<a href="{!! route('product.item', [$product->slug]) !!}">
<div>
#if(!empty($product_image))
<img src={!! "product_images/products/$product_image[0]" !!}>
#endif
<p>
{!! $product->title !!}
</p>
</div>
</a>
</div>
#endforeach
</div>
</div>
</div>
I'm not sure if I need to add anything else, so if I do need to please let me know
Change this:
<a href="{!! route('product.item', [$product->slug]) !!}">
to this:
<a href="{{ route('product.item', $product->id) }}">

logout and TokenMismatchException in Laravel 5.4

I have controller where I save invoice, but it is option to add item, bank and company. When I want to save the item and return to the invoice view, there is a problem:
TokenMismatchException
in VerifyCsrfToken.php (line 68)
or I refresh page I get an error: HttpException
This is my controller:
public function create()
{
$banks = Bank::all();
$methods = Payment_method::pluck('name_payment', 'id');
$users = User::pluck('name', 'id');
$items = Item::all();
$vats = Vat::all();
$companies = Company::all('name', 'id');
$numberProform = $this->setNumberProform();
if (isset($_COOKIE["addInvoiceForm"])) {
$previousData = $_COOKIE["addInvoiceForm"];
$previousData = unserialize($previousData);
return view('invoice.addInvoice', compact('items', 'companies', 'users', 'vats', 'numberProform', 'methods', 'banks', 'previousData'));
}
return view('invoice.addInvoice', compact('items', 'companies', 'users', 'vats', 'numberProform', 'methods', 'banks'));
}
public function store(Request $request)
{
if ($request->add === 'item') {
setcookie('addInvoiceForm', serialize($request->input()), time() + (86400 * 30), "/");
return redirect('addItem');
}
$this->validation($request);
$input = Req::all();
$invoice = Invoice::create($input);
$i = 1;
while ($request->has('item_' . $i)) {
$invoice->items()->attach([$request->{'item_' . $i} => ['quantity' => $request->{'quantity_' . $i}]]);
$i++;
}
return redirect('listInvoice');
}
view addInvoice.blade.php:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1 col-sm-12 col-sm-offset-0">
<div class="panel panel-default">
{!! Form::open(['url' => 'saveInvoice', 'method' => 'post', 'class' => 'form-horizontal']) !!}
{{ csrf_field() }}
<div class="panel-heading">
<h2>Proforma nr {{$numberProform}}</h2>
</div>
<div class="form-group">
<div class="col-sm-12">
{!! Form::hidden('number', $numberProform) !!}
<span>UWAGA: PRO FORMA nie jest dokumentem księgowym i nie jest podstawą do odliczenia VAT.</span>
</div>
</div>
<div class="panel-body">
<div class="form-horizontal">
<div class="row">
{!! Form::hidden('proforma', "1") !!}
{!! Form::hidden('user_id', \Illuminate\Support\Facades\Auth::user()->getAuthIdentifier()) !!}
<div class="form-group {{ $errors->has('place_issue') ? ' has-error' : '' }}">
<div class="col-sm-4 control-label">
{!! Form::label('place_issue', 'Miejsce:') !!}
</div>
<div class="col-sm-6">
{!! Form::text('place_issue', $previousData['place_issue'], ['class' => 'form-control']) !!}
#if ($errors->has('place_issue'))
<span class="help-block"><strong>{{ $errors->first('place_issue') }} </strong></span>
#endif
</div>
</div>
<div class="form-group {{ $errors->has('date_issue') ? ' has-error' : '' }}">
<div class="col-sm-4 control-label">
{!! Form::label('date_issue', 'Data wystawienia:') !!}
</div>
<div class="col-sm-6">
{!! Form::date('date_issue', $previousData['date_issue'], ['class' => 'form-control']) !!}
#if ($errors->has('date_issue'))
<span class="help-block"><strong>{{ $errors->first('date_issue') }} </strong></span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('date_payment') ? ' has-error' : '' }}">
<div class="col-sm-4 control-label">
{!! Form::label('date_payment', 'Zapłata do:') !!}
</div>
<div class="col-sm-6">
{!! Form::date('date_payment', $previousData['date_payment'], ['class' => 'form-control']) !!}
#if ($errors->has('date_payment'))
<span class="help-block"><strong>{{ $errors->first('date_payment') }} </strong></span>
#endif
</div>
</div>
<div class="form-group {{ $errors->has('description') ? ' has-error' : '' }}">
<div class="col-sm-4 control-label">
{!! Form::label('description', 'Opis:') !!}
</div>
<div class="col-sm-6">
{!! Form::text('description', $previousData['description'], ['class' => 'form-control']) !!}
#if ($errors->has('description'))
<span class="help-block"><strong>{{ $errors->first('description') }} </strong></span>
#endif
</div>
</div>
{{------------FIRMA------------}}
<div class="form-group {{ $errors->has('company_id') ? ' has-error' : '' }}">
<div class="col-sm-4 control-label">
<label for="company_id">Nazwa firmy</label>
</div>
<div class="col-sm-6">
<select name="company_id" id="company_id" class="form-control">
#foreach($companies as $company)
<option
value="{{$company->id }}" {{ $company->id==$previousData['company_id'] ? 'selected' : '' }}>
{{ $company->name }}
</option>
#endforeach
</select>
#if ($errors->has('company_id'))
<span class="help-block"><strong>{{ $errors->first('company_id') }} </strong></span>
#endif
</div>
<div class="col-sm-2">
<a href="{{ url('addCompany') }}" class="btn btn-success">
<span class="glyphicon glyphicon-plus"></span>
</a>
</div>
</div>
<div class="form-group">
<div class="col-sm-4 control-label">
{!! Form::label('bank_id', 'Bank:') !!}
</div>
<div class="col-sm-6">
<select class="form-control control-label" name="bank_id">
#foreach($banks as $bank)
<option value="{{$bank->id}}" {{ $bank->id==$previousData['bank_id']?'selected':''}}>
{{$bank->label}} ({{$bank->number_bank}});
</option>
#endforeach
</select>
</div>
<div class="col-sm-2">
<a href="{{ url('addBank') }}" class="btn btn-success">
<span class="glyphicon glyphicon-plus"></span>
</a>
</div>
</div>
<div class="form-group {{ $errors->has('item_id') ? ' has-error' : '' }}">
<div class="col-sm-4 control-label">
{!! Form::label('item_1_id', 'Dodaj przedmioty:') !!}
</div>
<div class="col-sm-6">
<select class="form-control control-label search-item">
#foreach($items as $item)
<option value="{{$item->id}}">
{{$item->name}}
</option>
#endforeach
</select>
</div>
<div class="col-sm-2">
<button type="submit" name="add" value="item" href="{{ url('addItem') }}" class="btn btn-success">
<span class="glyphicon glyphicon-plus"></span>
</button>
</div>
</div>
<div class="col-sm-12">
<div class="items-table-wr">
<table class="table table-hover items-table">
<tr>
<th>nazwa</th>
<th>vat</th>
<th>jednostka</th>
<th>cena netto</th>
<th>cena brutto</th>
<th>ilość</th>
<th>suma</th>
<th></th>
</tr>
<tbody class="items-list">
</tbody>
</table>
</div>
</div>
<div class="form-group {{ $errors->has('payment_method_id') ? ' has-error' : '' }}">
<div class="col-sm-4 control-label">
{!! Form::label('payment_method_id', 'Metoda płatności:') !!}
</div>
<div class="col-sm-6">
{!! Form::select('payment_method_id', $methods, ['class' => 'form-control']) !!}
#if ($errors->has('payment_method_id'))
<span class="help-block"><strong>{{ $errors->first('payment_method_id') }} </strong></span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-sm-6 col-sm-offset-4 text-right">
<button type="submit" class="btn btn-primary">Zapisz proformę
<span class="glyphicon glyphicon-ok"></span></button>
</div>
</div>
{{--<pre>--}}
{{-- {{print_r($previousData)}}--}}
{{--</pre>--}}
{!! Form::close() !!}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
I add that the form is csrf token ({{csrf_field() }}).
I have no idea what it is, someone can help me? :)

Invalid argument supplied for foreach with laravel 5.4

I'm getting Invalid argument supplied for foreach() error in my view after publishing a post and the code i use for looping in my controller is this:
public function edit($id)
{
$post = Post::find($id);
$categories = Category::all();
$cats = array();
foreach ($categories as $category) {
$cats[$category->id] = $category->name;
}
$tags = Tag::all();
$tags2 = array();
foreach ($tags as $tag) {
$tags2[$tag->id] = $tag->name;
}
return view('admin.posts.edit')->withPost($post)->withCategories($cats)->withTags($tags2);
}
this is the only part i handle loops in my postcontroller edit section. And I know the issue is from Tags loop because when I remove the tags code in my view other part will show up correctly.
Oh and this is the loop i use in my view:
#extends('layouts.app')
#section('content')
<div class="col-md-8">
<div class="panel panel-default">
<div class="panel-heading">{{ $post->title }}</div>
<div class="panel-body">
<p><img src="{{ asset('uploads/' . $post->image) }}" alt="{{ $post->title }}" class="img-responsive" /></p>
<p>{!! $post->body !!}</p>
</div>
<div class="tags">
#foreach ($tags as $tag)
<span class="label label-default">{{ $tag }}</span>
#endforeach
</div>
</div>
</div>
#endsection
#section('sidebar')
<div class="col-md-4">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-info"></i> Post Info</div>
<div class="panel-body">
<dl class="dl-horizontal">
<label>URL:</label>
<p>{{ url('blog/'.$post->slug) }}</p>
</dl>
<dl class="dl-horizontal">
<label>Created On:</label>
<p>{{ date('M j, Y h:ia', strtotime($post->created_at)) }}</p>
</dl>
<dl class="dl-horizontal">
<label>Last Update:</label>
<p>{{ date('M j, Y h:ia', strtotime($post->updated_at)) }}</p>
</dl>
<dl class="dl-horizontal">
<label>Posted In:</label>
<p>{{ $post->category->name }}</p>
</dl>
<hr/>
<div class="row">
<div class="col-md-6">
{!! Html::linkRoute('posts.edit', 'Edit', array($post->id), array('class' => 'btn btn-warning btn-block')) !!}
</div>
<div class="col-md-6">
{!! Form::open(['route' =>['posts.destroy', $post->id], 'method' => 'DELETE']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger btn-block']) !!}
{!! Form::close() !!}
</div>
</div>
<hr/>
{!! Html::linkRoute('posts.index', '<< Back to Posts', [], array('class' => 'btn btn-primary btn-block')) !!}
</div>
</div>
</div>
#endsection
PS: I'm using Laravel 5.4
Post updated view!
You can't get tags via post so Try this...
<div class="tags">
#foreach ($tags as $tag)
<span class="label label-default">{{ $tag }}</span>
#endforeach
</div>
Try:
return view('admin.posts.edit')->with("post",$post)->with("categories",$cats)->with("tags",$tags2);
and at view
<div class="tags">
#foreach ($tags as $tag)
<span class="label label-default">{{ $tag->name }}</span>
#endforeach
</div>
in your table if your primary key name if not id you have to mention in your relationship Link
2.for your post has one category so you can show this into your view like this
{{ $post->category->name }}
if you get result you can use it. but seems you need tag key and value pair so use this methods
$tags = Tag::where('post_id',$post->id)->groupBy('id')->get();
or try your way
$tags = Tag::all();
$tags2 = array();
foreach ($tags as $tag) {
$tags2[$tag->id] = $tag->name;
}
return view('admin.posts.edit',compact($post,$tags2,$cats));
in your view try this
#foreach ($tags as $key => $tag)
<span class="label label-default">{{ $tag }}</span>
#endforeach

Blade from Database not working properly

I am using voyager backoffice with laravel, and i'm having a problem with the blade coming from the database, all the code works except the blade, all the blade code outside the database works well.
I've already used {{ }}, {!! !!}, {{{ }}}, Html_entity_decode () but nothing works.
Any help is appreciated.
Thank you
View:
#extends ('layout')
#section ('content')
#foreach ($pageContent as $page)
{!! $page->slug !!}
{!! $page->title !!}
{!! $page->body !!}
#endforeach
#endsection
body that came from db:
<div class="container contacts_content_container">
<div class="row">
<div class="col-sm-12 text-center">
<div class="content">
<h1>Contact US Form</h1>
#if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
</div>
#endif
{!! Form::open(['route'=>'contactus.store']) !!}
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
{!! Form::label('Name:') !!}
{!! Form::text('name', old('name'), ['class'=>'form-control', 'placeholder'=>'Enter Name']) !!}
<span class="text-danger">{{ $errors->first('name') }}</span>
</div>
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
{!! Form::label('Email:') !!}
{!! Form::text('email', old('email'), ['class'=>'form-control', 'placeholder'=>'Enter Email']) !!}
<span class="text-danger">{{ $errors->first('email') }}</span>
</div>
<div class="form-group {{ $errors->has('message') ? 'has-error' : '' }}">
{!! Form::label('Message:') !!}
{!! Form::textarea('message', old('message'), ['class'=>'form-control', 'placeholder'=>'Enter Message']) !!}
<span class="text-danger">{{ $errors->first('message') }}</span>
</div>
<div class="form-group">
<button class="btn btn-success">Contact US!</button>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
Controller:
<?php
namespace App\Http\Controllers;
use App\Page;
use Illuminate\Routing\Controller;
class PageController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
// get all the pages
$pages = Page::all();
// load the view and pass the pages
return view('public.about')
->with('pages', $pages);
}
public function slug($slug)
{
// get page where slug
$pageContent = Page::where('slug', $slug)->get();
// load the view and pass the page content
return view('public.' . $slug)
->with('pageContent', $pageContent);
}
}
Routes:
//Pages Routing With slug
Route::get('/{slug}','PageController#slug');
I'd say keeping templates in a DB is not a good idea, but if you really need it you can parse Blade template manually. There are multiple ways to do that and one of these is using compileString() method:
$html = Blade::compileString($page->template);
May be compileString() might help you.Try this
Blade::compileString('Your blade syntax from db {!! $variable !!}');

Resources