Laravel many to many relationship update - laravel

I have some hard time with many to many relationship when I try to update the pivot columns.
Here is my database 'order_product' table
order_product_table
First, I am trying to update the products of that order.
Order update form
Here is the HTML:
#if($products)
<select class="form-control kt-select2 products" name="products[]" required>
<option selected disabled>Select a product</option>
#foreach($products as $product)
<option value="{{ $product->id }}" data-price="{{ $product->selling_price }}" {{ $product->id === $order_product->id ? 'selected' : '' }}>{{ $product->name }}</option>
#endforeach
</select>
#endif
My spaghetti code...
public function update(Order $order)
{
$attributes = $this->validateOrder();
$order->update($attributes);
$products = \request('products');
$quantity = \request('quantity');
$price = \request('price');
$discount = \request('discount');
$total = 0;
if ($products) {
$order->products()->sync([$order->id => ['product_id' => $products]]);
}
$this->flashMessage('success', 'Your order was updated with success!');
return redirect()->back();
}
Try a lot of things, but it does not work ...

Edit your code like this:
public function update(Order $order)
{
$attributes = $this->validateOrder();
$order->update($attributes);
//should be an array of products ID
$products = \request('products');
$quantity = \request('quantity');
$price = \request('price');
$discount = \request('discount');
$total = 0;
foreach($products as $key => $productId) {
//normalize attributes
$attributes = [
'price' => $price[$key],
'quantity' => $quantity[$key],
'discount' => $discount[$key],
];
$order->products()->updateExistingPivot($productId, $attributes);
}
$this->flashMessage('success', 'Your order was updated with success!');
return redirect()->back();
}
For more information about updateExistingPivot() method, check Laravel documention

For the sync() method in many to many relation, you need to provide the relation ID as index of the array.
$syncable = [];
foreach ($products as $key => $productId) {
$syncable[$productId] = [
'price' => $attributes['price'][$key],
'quantity' => $attributes['quantity'][$key],
'discount' => $attributes['discount'][$key]
]
}
if ($syncable) {
$order->products()->sync($syncable);
}

Related

How can I get value from a select field?

I have a livewire CRUD component to make posts. It worked perfectly before I tried to add a choice of categories. This is a piece of code, where I try to choose a category:
<select class="form-select" aria-label="Default select example">
<option wire:model="category_id" selected>Выберите категорию</option>
#foreach($categories as $category)
<option value="{{ $category->id }}">
{{ $category->name }}
</option>
#endforeach
#error('category_id') <span class="text-red-500">{{ $message }}</span>#enderror
</select>
I can display all the categories and choose one, but I can't get it in the component. I have tried to use a checkbox element. I was even validated, whenever I chose a category or not. The select field isn't validated at all. I can just output all the categories from the db.
Here is my CRUD component.
<?php
namespace App\Http\Livewire;
use App\Models\Category;
use Livewire\Component;
use App\Models\Post;
use Livewire\WithPagination;
use Livewire\WithFileUploads;
class Posts extends Component
{
public $title, $categories, $category_id, $body, $post_id, $search, $img;
public $isOpen = 0;
use WithFileUploads;
use WithPagination;
public function mount()
{
$this->categories = Category::all();
}
public function render()
{
$search = '%' . $this->search . '%';
$posts = Post::where('title', 'LIKE', $search)
->orWhere('body', 'LIKE', $search)
->latest()
->paginate(5);
return view('livewire.posts.posts', ['posts' => $posts])->layout('layouts.app');
}
public function create()
{
$this->resetInputFields();
$this->openModal();
}
public function openModal()
{
$this->isOpen = true;
}
public function closeModal()
{
$this->isOpen = false;
}
private function resetInputFields()
{
$this->category_id = '';
$this->title = '';
$this->body = '';
$this->post_id = '';
}
public function store()
{
$this->validate([
'category_id' => 'required',
'title' => 'required',
'body' => 'required',
'img' => 'image|max:1024'
]);
Post::updateOrCreate(
['id' => $this->post_id],
['category_id' => $this->category_id,
'title' => $this->title,
'body' => $this->body,
'img' => $this->img->hashName(),
]);
if(!empty($this->img)) {
$this->img->store('public/docs');
}
session()->flash('message',
$this->post_id ? 'Пост успешно обновлен.' : 'Пост успешно создан.');
$this->closeModal();
$this->resetInputFields();
}
public function edit($id)
{
$post = Post::findOrFail($id);
$this->category_id = $post->category_id;
$this->post_id = $id;
$this->title = $post->title;
$this->body = $post->body;
$this->img = $post->img;
$this->openModal();
}
public function delete($id)
{
Post::find($id)->delete();
session()->flash('message', 'Пост успешно удален.');
}
}
Will setting a name for select solve the problem?
<select class="form-select" aria-label="Default select example" name="category_id">
<option wire:model="category_id" selected>Выберите категорию</option>
#foreach($categories as $category)
<option value="{{ $category->id }}">
{{ $category->name }}
</option>
#endforeach
#error('category_id') <span class="text-red-500">{{ $message }}</span>#enderror
</select>
Currently it doesn't seems like framework can identify the name for this input

Laravel - production.ERROR: Undefined variable: data

In my Laravel-5.8, I have this code:
public $filters = [
"all" => "all",
"logged_in" => "logged in users",
"not_logged_in" => "not logged in users",
];
public function user_logs($id = "")
{
$userCompany = Auth::user()->company_id;
$userEmployee = Auth::user()->employee_id;
$userId = Auth::user()->id;
$employeecode = Auth::user()->employee_code;
if ($id == 'all') {
$data = User::where('hr_status', 0)->where('company_id', $userCompany)->orderBy('last_login_at', 'desc')->get();
}
elseif ($id == "") {
$data = User::where('hr_status', 0)->where('company_id', $userCompany)->where('active', 0)->orderBy('last_login_at', 'desc')->get();
}
elseif ($id == "logged_in") {
$data = User::where('last_login_at', '>', today()->subDays(30))
->where('hr_status', 0)
->where('company_id', $userCompany)
->orderBy('last_login_at', 'desc')
->get();
}
elseif ($id == "not_logged_in") {
$data = User::where('last_login_at', '<', today()->subDays(30))
->orWhereNull('last_login_at')
->where('hr_status', 0)
->where('company_id', $userCompany)
->orderBy('last_login_at', 'desc')
->get();
}
$chart_settings = [
'chart_title' => 'Users By Months',
'chart_type' => 'line',
'report_type' => 'group_by_date',
'model' => 'App\\User',
'group_by_field' => 'last_login_at',
'group_by_period' => 'month',
'aggregate_function' => 'count',
'filter_field' => 'last_login_at',
'column_class' => 'col-md-12',
'entries_number' => '5',
];
$chart = new LaravelChart($chart_settings);
return view('report.report_user_login_logs.user_logs', compact( 'chart'))
->with('employees', $data)
->with('filters', $this->filters)
->with('selectedFilter', $id);
}
view blade:
view/report/report_user_login_logs/index.blade.php
<div class="form-group">
<select class="form-control" id="filter">
<option value="select">Select Search Criteria</option>
#foreach($filters as $filter)
<option value="{{$filter}}" #if($filter==$selectedFilter) selected #endif>{{ucfirst(trans($filter))}}</option>
#endforeach
</select>
</div>
<tbody>
#foreach($employees as $key => $employee)
<tr>
<td>
{{$employee->employee_code}}
</td>
<td>
{{$employee->first_name}} {{$employee->last_name}}
</td>
<td>
{{$employee->email}}
</td>
<td>
{{$employee->last_login_at}}
</td>
</tr>
#endforeach
</tbody>
<script type="text/javascript">
$(document).ready(function () {
$("#filter").change(function(e){
if ($(this).val()=== "select" ){
var url = "{{route('report.report_user_login_logs.user_logs')}}/"
}
else{
var url = "{{route('report.report_user_login_logs.user_logs')}}/" + $(this).val();
}
if (url) {
window.location = url;
}
return false;
});
});
</script>
route/web.php
Route::group(['prefix' => 'report', 'as' => 'report.', 'namespace' => 'Report', 'middleware' => ['auth']], function () {
Route::get('report_user_login_logs/user_logs/{id?}', 'ReportUserLoginLogsController#user_logs')->name('report_user_login_logs.user_logs');
});
I use the dropdown as filter to display the data on the table. The doropdown onchange:
<select class="form-control" id="filter">
<option value="select">Select Search Criteria</option>
#foreach($filters as $filter)
<option value="{{$filter}}" #if($filter==$selectedFilter) selected #endif>{{ucfirst(trans($filter))}}</option>
#endforeach
</select>
When I select "All", it works.
But when I select
"logged in users" as in "logged_in" => "logged in users",
or
"not logged in users" "not_logged_in" => "not logged in users",
I got this error:
production.ERROR: Undefined variable: data
How do I resolve it?
Thank you

Laravel: IF statement drop down list

So i currently have a drop down list where it will output a list of hotels based on its price. I now want to further develop the drop down list so that it can output a list of hotels based on price AND distance. How would i exactly create a drop down list that will output a list of hotels based on 2 or more criteria.
SearchController.php
public function index(Request $request)
{
$distances = DB::table('posts')->select('distance')->distinct()->get()->pluck('distance');
$prices = DB::table('posts')->select('price')->distinct()->get()->pluck('price');
$postsInRange = $request->has('distance')
? Post::where('distance', $request->distance)->get()
: [];
$postsInRange1 = $request->has('price')
? Post::where('price', $request->price)->get()
: [];
return view('Pages.search', [
'distances' => $distances,
'prices' => $prices,
'posts' => $postsInRange,
'posts' => $postsInRange1,
]);
}
public function store(Request $request)
{
// This will return all request data to your screen.
return $request->all();
return view('Pages.search');
}
Search.blade.php
<select name="distance" id="distance" class="form-control input-lg dynamic" data-dependent="state">
<option value="">Distance</option>
#foreach($distances as $distance)
<option value="{{ $distance }}">{{ $distance }}</option>
#endforeach
</select>
<br>
<select name="price" id="price" class="form-control input-lg dynamic" data-dependent="state">
<option value="">Price</option>
#foreach($prices as $price)
<option value="{{ $price}}">{{ $price}}</option>
#endforeach
So to clarify the drop down list works but only outputs one of the choices selected.
I think what you need is query filter
$post = new Post;
if ($request->has('price')) {
$post->where('price', $request->price);
}
if ($request->has('distance')) {
$post->where('distance', $request->distance);
}
return view('Pages.search', [
'distances' => $distances,
'prices' => $prices,
'posts' => $post->get()
]);
I assume that you need one list applying both filters.

Test Driven Laravel : Invalid Argument supplied for foreach

So i have a form with multiple fields like below
<ul class="list-group list-group-flush">
#foreach ($group as $perm)
<li class="list-group-item">{{$perm->name}}
<div class="float-right">
<select name="perms[{{$perm->id}}]" class="form-control">
<option value="1">Yes</option>
<option value="0" selected>No</option>
</select>
</div>
</li>
#endforeach
</ul>
My controller is like below
public function permission(int $id)
{
$permission = request()->perms;
foreach ($permission as $perm => $status)
{
if($status == 1)
{
//echo $perm . " " . $status;
$user_perm = User_perms::create([
'user_id' => $id,
'perm_id' => $perm,
]);
}
}
$user = Users::find($id);
return redirect($user->path());
}
This code does what I want but I have a test
public function permissions_applied_for_user()
{
$this->withoutExceptionHandling();
//create a user
$this->post('/users/add', $this->data());
$user = Users::first();
//first clear out all data from user_perm table for specific user
$response = $this->post('/users/permission/' . $user->id, [
'user_id' => $user->id,
'perm_id' => '1',
]);
$this->assertCount(1, User_perms::all());
$response->assertRedirect('/users/view/' . $user->id);
//$response->assertOk();
//second insert all new permissions into the table
}
which throws the exception invalid argument supplied for foreach any advice on what I'm doing wrong?

Laravel. Controller not getting the values from the Form

The controller is not getting the data from the FORM. I realise that the Form has by default a Post method, while the Route is using a Get, but if I change that, then the form will not display the form fields. Validation fails as the "required" does not get any values, so it returns to the same page. If I remove the validation filter, then it does go to the results page, but all it does is show ALL of the content of the table, since it is getting no parameters (where) from the Form. The weird thing is that in the past, it worked, but I must have messed up with some part of the code and now it doesn't. To save space here I have left out many fields which dont play a role in the problem.
The Form has three interdependent Fields Country, Region and Town, which are filled up alright.
FORM:
<form action = "{{URL::route('sacapropiedades')}} "class="form-horizontal" id="my_form" name="my_form">
<div class="form-group">
<div class="col-sm-3">
<label for="country">Pays</label>
<select name ="country" {{ (Input::old('country')) ?' value ="' . e(Input::old('country')). '"' : '' }} id = "country" class="form-control">
#foreach($countries as $country)
<option value="{{$country->country}}">{{$country->country}}</option>
#endforeach
</select>
</div>
<div class="col-sm-3">
<label for="town">Ville</label>
<select name ="town" {{ (Input::old('town')) ?' value ="' . e(Input::old('town')). '"' : '' }}id = "town" class="form-control">
</select>
</div>
</div><!-- END OF THIRD FORMGROUP -->
<div class="form-group">
<div class="col-sm-4">
</div>
<div class="col-sm-2">
<button type="submit" class="btn btn-success">Enviar</button>
<button type="reset" class="btn btn-danger">Borrar</button>
</div>
</div>
</form>
ROUTES
Route::get('realestate/listproperty', array(
'as' =>'sacapropiedades',
'uses' =>'countriesregionstownsController#findproperty'
));
CONTROLLER
public function findproperty(){
/*IT REPEATS THE COUNTRY QUERY ABOVE BECAUSE IT IS GOING TO USE IT
*ON THE RESULTS PAGE AND IT GIVES THE USER TO SELECT AGAIN OTHER COUNTRIES
*WITHOUT HAVING TO RETURN TO THE FIRST PAST PAGE*/
$countries = DB::table('properties')
->select('country')
->distinct()
->get();
/*FIRST VALIDATE INPUT DATA*/
$validator = Validator::make(Input::all(),
array(
'country' =>'required',
'regions' =>'required',
'transaction' =>'required',
'town' =>'required'
));
if($validator->fails()){
return Redirect::route('showrealestate')
->withErrors($validator)
->withInput();
}
else{
$country = Input::get('country');
$region = Input::get('regions');
$town = Input::get('town');
$transaction = Input::get('transaction');
$pricefrom = Input::get('pricefrom');
$priceto = Input::get('priceto');
$roomsfrom = Input::get('roomsfrom');
$roomsto = Input::get('roomsto');
$builtyear = Input::get('builtyear');
$swimming = Input::get('swimming');
$garden = Input::get('garden');
$garage = Input::get('garage');
$message = Input::get('message');
}
$country = DB::table('countries')->where('id_pais', $country)->pluck('nombre_pais');
$region = DB::table('regions')->where('id_region', $region)->pluck('nombre_region');
$town = DB::table('cities')->where('id_localidad', $town)->pluck('nombre_localidad');
$users = DB::table('users')
->join('properties', 'users.id', '=', 'properties.id_user_fk')
->select('users.email', 'properties.id_user_fk', 'properties.country', 'properties.region', 'properties.town',
'properties.price', 'properties.rooms','properties.m2','properties.swimming',
'properties.garden','properties.garage','properties.builtyear','properties.message',
'properties.pic1',
'properties.pic2', 'properties.pic3','properties.pic4','properties.pic5','properties.pic6');
if (!empty($country)) {
$users = $users->where('country', '=', $country);
}
if (!empty($region)) {
$users = $users->where('region', '=', $region);
}
if (!empty($town)) {
$users = $users->where('town', '=', $town);
}
if (!empty($transaction)) {
$users = $users->where('transaction', '=', $transaction);
}
if (!empty($pricefrom)) {
$users = $users->where('price', '>', $pricefrom);
}
if (!empty($priceto)) {
$users = $users->where('price', '<', $priceto);
}
if (!empty($roomsfrom)) {
$users = $users->where('rooms', '>', $roomsfrom);
}
if (!empty($roomsto)) {
$users = $users->where('rooms', '<', $roomsto);
}
if (!empty($builtyear)) {
$users = $users->where('builtyear', '>', $builtyear);
}
if (!empty($swimming)) {
$users = $users->where('swimming', '=', $swimming);
}
if (!empty($garage)) {
$users = $users->where('garage', '=', $garage);
}
if (!empty($garden)) {
$users = $users->where('garden', '=', $garden);
}
if (!empty($message)) {
$users = $users->where('message', '=', $message);
}
$users = $users->get();
return View::make('realestate.externa.listproperty', compact('users','countries'));
}
A post method is mandatory, otherwise Laravel will not redirect it to the correct method with the correct data. How was it working before? By luck, probably. :)
Route::get('realestate/listproperty', array(
'as' =>'sacapropiedades',
'uses' =>'countriesregionstownsController#findproperty'
));
Route::post('realestate/listproperty', array(
'as' =>'sacapropiedades',
'uses' =>'countriesregionstownsController#findproperty'
));
or
Route::match(array('GET', 'POST'), 'realestate/listproperty', array(
'as' =>'sacapropiedades',
'uses' =>'countriesregionstownsController#findproperty'
));
or
Route::any('realestate/listproperty', array(
'as' =>'sacapropiedades',
'uses' =>'countriesregionstownsController#findproperty'
));
Then you'll probably need to not validate on GET:
if (Request::getMethod() == 'POST')
{
$validator = Validator::...
}
EDIT:
Sorry I overlooked this problem:
Instead of writing your FORM tag manually, use Laravel's FormBuilder class:
<?php Form::open(array('route' => 'sacapropiedades', 'class' => 'form-horizontal', 'id' => 'my_form', 'name' => 'my_form')); ?>
The difference is that it will add the method for you and it will also add a csrf token to your form.

Resources