So my problem is that if i try to save only one value like "Location in the layout" of a post it works, but the moment i am saving an array I'm getting something like ["value 1" , "value 2"] in the DB, therefore it cant be read and in the CRUD-Controller there is no data saved when i am editing.everything works perfectly except the value i am getting from the data. I would appreciate any help, methods or alternatives
edit.blade.php
<div class="form-group">
<label>Select Post Location</label>
<select
class="form-control select2 select2-hidden-accessible"
multiple=""
data-placeholder="Select Locations"
style="width: 100%"
tabindex="-1"
aria-hidden="true"
name="post_locations[]"
id="post_locations"
>
<option value="TopBox-GR" #if ($post->post_locations == "TopBox-GR") selected #endif>TopBox-GR</option>
<option value="TopBox-C3" #if ($post->post_locations == "TopBox-C3") selected #endif>TopBox-C3</option>
<option value="TopBox-C4" #if ($post->post_locations == "TopBox-C4") selected #endif>TopBox-C4</option>
</select>
</div>
home controller
public function index()
{
$posts = post::where([['status',1], ['post_locations','TopBox-GR']])->get();
return view('user.blog',compact('posts'));
}
and view:
<div class="topbox-c4">
#foreach ($posts as $post)
<a href="{{ route('post',$post->slug) }}">
<div class="image-topbox-c4-container">
<img src="{{ Storage::disk('local')->url($post->image)}}" />
</div>
<div class="text-topbox-c4-container">
<h3>{{$post->title}}</h3>
<p>
{{$post->subtitle}}
</p>
</div>
</a>
#endforeach
</div>
post.controller:
public function update(Request $request, $id)
{
$this->validate($request,[
'title'=>'required',
'subtitle'=>'required',
'slug'=>'required',
'body'=>'required',
'image'=>'required',
'post_locations'=>'required',
]);
if($request->hasFile('image'))
{
$imageName = $request->image->store('public');
}
$post = post::find($id);
$post->image = $imageName;
$post->title = $request->title;
$post->subtitle = $request->subtitle;
$post->slug = $request->slug;
$post->post_locations = $request->post_locations;
$post->body = $request->body;
$post->status = $request->status;
$post->tags()->sync($request->tags);
$post->categories()->sync($request->categories);
$post->save();
return redirect(route('post.index'));
}
Firstly you should get selected locations in $selectLocations variable and all locations in $allLocations variable for edit.blade.php
In edit.blade.php, you can display like this:-
#foreach($selectLocations as $value)
<select class="form-control" name="post_locations[]" multiple=''>
<option value="">Select Location</option>
#foreach($allLocations as $val)
<option #if($value['id'] == $val->id) {{ 'selected' }} #endif value="{{ $val->id }}">{{ $val->name}}</option>
#endforeach
</select>
#endforeach
Related
working with laravel 6 and need edit category options here.
PostController.php
public function edit($id)
{
$post = Post::find($id);
$categories = Category::all();
$cats = array();
foreach ($categories as $category) {
$cats[$category->id] = $category->name;
}
return view('posts.edit')->withPost($post)->withCategories($cats);
}
and edit.blade.php
<div class="form-group">
<label for="exampleFormControlSelect1">Category</label>
<select class="form-control" name="category_id" id="category_id">
#foreach($categories as $cats)
<option value="{{$post->category->id}}">{{$post->category->name}}</option>
#endforeach
</select>
</div>
but when I am going to edit page in the edit category options display only current category item. I need display all categories in the table? how could I manage this?
simply like this :
Controller
public function edit($id)
{
$post = Post::findOrFail($id);
$categories = Category::all();
return view('posts.edit', compact(['post', 'categories']));
}
View
<div class="form-group">
<label for="exampleFormControlSelect1">Category</label>
<select class="form-control" name="category_id" id="category_id">
#foreach($categories as $cat)
<option value="{{$cat->id}}" #if($cat->id === $post->category_id) 'selected' #endif >{{$cat->name}}</option>
#endforeach
</select>
</div>
I make it in 1 route with
in route
Route::get('/all-students', 'AdminController#studentList')->name('admin.student');
Route::post('/all-students', 'AdminController#searchByClassRoll')->name('search-by-class-roll');
in controller funtion student list
public function studentList(Request $request)
{
Session::put('url.intended2', URL::current());
Session::put('url.intended', URL::previous());
if (isset($request->class)) {
if (isset($request->roll)) {
if ($request->class == 'all') {
$students = DB::table('students')
->where('roll', '=', $request->roll)
->orderBy('id', 'DESC')
->paginate(20);
} else {
$students = DB::table('students')
->where('roll', '=', $request->roll)
->where('class', '=', $request->class)
->orderBy('id', 'DESC')
->paginate(20);
}
} else {
if ($request->class == 'all') {
return redirect()->route('admin.student');
} else {
$students = DB::table('students')
->where('class', '=', $request->class)
->orderBy('id', 'DESC')
->paginate(20);
}
}
} else {
$students = DB::table('students')
->orderBy('id', 'DESC')
->paginate(20);
}
return view('admin.student-list')->with('students', $students);
}
post method
public function searchByClassRoll(Request $request)
{
$class = $request->class;
$roll = $request->roll;
if (isset($request->class)) {
if (isset($request->roll)) {
return redirect()->route('admin.student', ['class' => $class, 'roll' => $roll]);
} else {
return redirect()->route('admin.student', ['class' => $class]);
}
}
}
in blade
#if(method_exists($students,'links'))
{!! $students->links() !!}
#endif
normally when I click page 2 it returns
all-students?page=2
when I search something all-students?class=2 and then i click on pagination page it returns again all-students?page=2 and remove the search data from URL.
form from view
<form action="{{ route('search-by-class-roll') }}" method="post">
{{ csrf_field() }}
<input type="hidden" name="type" value="student_list">
<div class="row gutters-8">
<div class="col-4-xxxl col-xl-4 col-lg-3 col-12 form-group">
<div class="ui-alart-box">
<div class="default-alart">
<div class="result" role="alert">
#if(app('request')->input('class') || app('request')->input('roll'))
See All<span> | </span>
#endif
Showing results {{$students->count()}} of {{$students->total()}} entries
</div>
</div>
</div>
</div>
<div class="col-3-xxxl col-xl-3 col-lg-3 col-12 form-group">
<input type="number" name="roll" placeholder="Search by Roll..." class="form-control">
</div>
<div class="col-4-xxxl col-xl-3 col-lg-3 col-12 form-group">
<select class="select2 form-control" name="class" required>
<option value="all">All Classes</option>
<option value="baby">baby</option>
<option value="nursery">nursery</option>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
<option value="5">five</option>
<option value="6">six</option>
<option value="7">seven</option>
<option value="8">eight</option>
<option value="9">nine</option>
<option value="10">ten</option>
</select>
</div>
<div class="col-1-xxxl col-xl-2 col-lg-3 col-12 form-group">
<button type="submit" class="fw-btn-fill btn-gradient-yellow">SEARCH</button>
</div>
</div>
</form>
i want to made this because I want this in 1 URL just because if someone manually entered the URL with search data he can see the output. but in here I can't get the result with pagination from blade. when I turned into page 2 it remove the search variable from URL. and paginate the whole data. how can I get result with pagination after search?
There is the simpler way
Just add with your paginated array.
{{ $users->withQueryString()->links() }}
Coming directly to your controller. You don't need any other route or method you can handle this filtering on same route and in same method.
public function studentList(Request $request)
{
if(!empty($request)){
$query = $request->all();
$students = DB::table('students')->orderBy('id', 'DESC');
if(isset($request->class) AND $request->class != '' AND $request->class != 'all')
$students = $students->where('class', '=', $request->class);
if(isset($request->roll) AND $request->roll != '')
$students = $students->where('roll', '=', $request->roll);
$students = $students->paginate(20);
return view('admin.student-list', compact('students','query'));
}
$students = DB::table('students')
->orderBy('id', 'DESC')
->paginate(20);
return view('admin.student-list', compact('students'));
}
Change few things in your view
<form action="{{ route('admin.student') }}" method="get"> change route and method to get
{{ csrf_field() }} //remove this
Below your table inside view write this code. The appends() is most important otherwise pagination in your filtered data won't work.
#if(isset($query))
{{ $students->appends($query)->links() }}
#else
{{ $students->links() }}
#endif
Try use append on paginator blade
{{$students->appends(\Illuminate\Support\Facades\Input::except('page'))->links()}}
I made a admin panel with laravel 5.4 . I want to show my category list using category table in products items edit view page. This is my product items edit page controller.
public function edit($id)
{
$item = Item::findOrFail($id);
//$sub_cat = SubCat::all();
$sub_cat = SubCat::with('category')->get();
return view('admin.items.edit', compact(['item', 'sub_cat']));
}
And this is my product items edit page view selection box values show
<div class="form-group">
<label>Main Category</label>
<select class="form-control" id="main_category" name="main_category">
#if(!empty($sub_cat))
#foreach ($sub_cat as $pages)
<option value="{{ $item->id }}">{{ $pages->name }}</option>
#endforeach
#endif
</select>
</div>
I used a model relationship like this
public function category(){
return $this->belongsTo('App\SubCat');
}
How i show my saved category name with other category names into my selection box.
first of all get all the categories like this
public function edit($id)
{
$item = Item::findOrFail($id);
$sub_cat = SubCat::all(); //get all the categories
return view('admin.items.edit', compact(['item', 'sub_cat']));
}
in the blade file run loop on $sub_cat collection and replace $item->id to $pages->id
<div class="form-group">
<label>Main Category</label>
<select class="form-control" id="main_category" name="main_category">
#if(!empty($sub_cat))
#foreach ($sub_cat as $pages) //loop on $sub_cat
<option value="{{ $pages->id }}" {{ $item->category_id == $pages->id ? 'selected="selected"' : '' }}>{{ $pages->name }}</option>
#endforeach
#endif
</select>
</div>
I have multiple select box with same name inside loop in a form. I have added laravel validation to check select box selected or not. But validation error message is showing for all the select boxes. Please check image attached. Any help would be appreciated.
cart.blade.php
#forelse($carts as $key => $cart)
<div class="col-sm-4 col-sm-4-booking1 form-group {{ $errors->has('guest.*.sleeps') ? ' has-error' : '' }}">
<label>Sleep(s)</label>
<select class="form-control form-control-booking1 jsBookCalSleep" name="guest[{{ $cart->_id }}][sleeps]">
<option value="">Choose Sleep(s)</option>
#for($i = 1; $i <= 30; $i++)
<option value="{{ $i }}" #if($i == $cart->sleeps) selected #endif>{{ $i }}</option>
#endfor
</select>
#if ($errors->has('guest.*.sleeps'))
<span class="help-block"><strong>{{ $errors->first('guest.*.sleeps') }}</strong></span>
#endif
</div>
#empty
<p>No bookings in your cart</p>
#endforelse
CartController.php
public function store(CartRequest $request)
{
dd($request->all());
}
CartRequest.php
public function rules()
{
return [
'guest.*.sleeps' => 'required|not_in:0'
];
}
Try these
#php
$inputName='guest.'.$cart->_id.'.sleeps';
#endphp
#if ($errors->has($inputName))
<span class="help-block"><strong>{{ $errors->first($inputName) }}</strong></span>
#endif
I didn't tested it,
If not working let me know
Solution: We can solve this issue by using two function. a.collect and b. contains. See the example below.
<select class="form-control m-bootstrap-select m_selectpicker"
name="generic_ids[]" data-live-search="true" multiple>
<option value="">Select Generic</option>
#foreach($generics as $generic)
<option value="{{$generic->id}}"
#if(collect(app()->request->generic_ids)->contains($generic->id))
selected
#endif
>{{$generic->title}}</option>
#endforeach
</select>
Code
Output
I have a view show.php of my single product, when user click on ADD PRODUCT my controller CartController.php update my session cart and update my cartSession table,
Now i'm tryng to put new data (Color input,Size input, Quantity input) on my session and also in my CartSession table. But i dont know why it doesn't work.
-I think that the principal problem is that $request->get('input') doesn't pass to my CartController.php , i tried to return
$request->all() and there is not nothing.
CartController.php
namespace dixard\Http\Controllers;
use Illuminate\Http\Request;
use dixard\Http\Requests;
use dixard\Http\Controllers\Controller;
use dixard\Product;
use dixard\CartSession;
use dixard\CartItem;
use dixard\Shipping;
use Session;
// i think that all classes are ok
public function add(Product $product, CartSession $CartSession,Request $request)
{
$id = $request->get('id');
$cart = \Session::get('cart');
$product->quantity = $request->get('qty');
$product->size = $request->get('size');
$product->color = $request->get('color');
$cart[$product->id] = $product;
\Session::put('cart', $cart);
return $request->all(); // here i tried to get all inputs but it shows me nothing result
$subtotal = 0;
foreach($cart as $producto){
$subtotal += $producto->quantity * $producto->price;
}
$session_code = Session::getId();
$CartSession = new CartSession();
$session_exist = CartSession::where('session_code', $session_code)->orderBy('id', 'desc')->first();
if (isset($session_exist)) {
$s = new CartSession;
$data = array(
'subtotal' => $subtotal,
);
$s->where('session_code', '=', $session_code)->update($data);
}else {
$CartSession = new CartSession();
$CartSession->session_code = $session_code;
$CartSession->subtotal = $subtotal;
$CartSession->save();
}
//return $cart;
//salveremo tutte le informazioni nel array cart nella posizione slug
foreach($cart as $producto){
$this->saveCartItem($producto, $CartSession->id, $session_code, $cart);
}
return redirect()->route('cart-show');
}
Routes.php
Route::bind('product', function($id) {
return dixard\Product::where('id', $id)->first();
});
Route::get('cart/add/{product}', [
'as' => 'cart-add',
'uses' => 'CartController#add'
]);
show.php view
Get Data about the product is ok, title, description, color avaible, price ecc. all information pass to my view.
{!! Form::open(['route'=> ['cart-add', $product->id],'class'=>'form-horizontal form-label-left'])!!}
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="id" value="{{$product->id}}">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="p_color">Colore</label>
<select name="color" id="p_size" class="form-control">
#foreach($colors as $color)
<option value="{{ $color->color }}">{{ $color->color }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="size">Size</label>
<select name="size" id="p_size" class="form-control">
<option value="XS">XS</option>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="qty">Quantity</label>
<select name="qty" id="p_qty" class="form-control">
<option value="">1</option>
<option value="">2</option>
<option value="">3</option>
</select>
</div>
</div>
</div>
<div class="product-list-actions">
<span class="product-price">
<span class="amount">{{$product->price}}</span>
<input type="submit" class="btn btn-lg btn-primary" >
ADD PRODUCT
</input>
</div><!-- /.product-list-actions -->
{!! Form::close() !!}
Thank you for your help!
You have to explicitly set the post-method to be "get" or you need to change your router to accept the request as post. Even though you're calling the route by name, Form::open defaults to "POST"
https://laravelcollective.com/docs/5.2/html#opening-a-form
Option 1. Change
Route::get('cart/add/{product}',...
to
Route::post('cart/add/{product}',...
Option 2. Change
{!! Form::open(['route'=> ['cart-add', $product->id],'class'=>'form-horizontal form-label-left'])!!}
to
{!! Form::open(['method'=>'GET', 'route'=> ['cart-add', $product->id],'class'=>'form-horizontal form-label-left'])!!}