Php Dropdown menu - Connect to another DB and show them - php

i work with vuejs3 and laravel 9 on one project.
i have two DB_table. Employee and company.
Company has only id and company name.
Employee contains: id, first name, last name, email, and company_id as forign key, which was joined to id in company_table.
If you want to create a new employee, you have to enter above mentioned infos and select a company name from selectbox (which is shown in previously entered companies) and save it in the table. How can I do then?
#extends('mitarbeiter.layout')
#section('content')
<div class="card">
<div class="card-header">Mitarbeiter Page</div>
<div class="card-body">
<form action="{{ url('mitarbeiter') }}" method="post">
{!! csrf_field() !!}
<label>Vorname</label></br>
<input type="text" name="vorname" id="vorname" class="form-control"></br>
<label>Nachname</label></br>
<input type="text" name="nachname" id="nachname" v-model="mitarbeiter.nachname" class="form-control"></br>
<label>Email</label></br>
<input type="text" name="email" id="email" class="form-control"></br>
<label>Company</label></br>
<select>
<option value="">------Select Company-------</option>
<option name="companyname">
{{option.companyname}}
</option>
</select></br>
</br>
<input type="submit" value="Save" class="btn btn-success"></br>
</form>
</div>
</div>
#stop

You can use foreach like this :
<select name="company" class="form-control">
#foreach ($companies as $company)
<option value="{{ $company->id }}">{{$company->name}}</option>
#endforeach
</select>
and in the controller :
$company = Companies::find($id);
$employee = new Employee();
$employee->user_id = $request->company; //name of select
$employee->company_id = $id;
$employee->save();
hope this would help

Related

How to set the multiple default value in laravel livewire

i have a multiple product when i get the default value of that product, but when i get that value in input field the default value doesn't show ?
<x-filament::page>
<form wire:submit.prevent="submit">
<div>
#if (session()->has('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif
</div>
<select wire:model="user_type">
<option value="">Select User Type.....</option>
<option value="admin">Admin</option>
<option value="distributor">Distributor</option>
<option value="retailer">Retailer</option>
</select><br><br>
<input wire:model="name" type="text" placeholder="Name"><br><br>
<input wire:model="email" type="email" placeholder="Email Address"><br><br>
<input wire:model="password" type="password" placeholder="Password"><br><br>
<div align = "center">Products</div>
** #foreach($product as $val)
<br><br> {{ $val->name }} :
<input type="number" wire:model="default_price" value = "{{$val->default_price}}">
#endforeach<br><br>**
<button type="submit">
Submit
</button>
</form>
</x-filament::page>
Check these screencasts. They are very helpful! https://laravel-livewire.com/screencasts/data-binding
<input type="number" wire:model="default_price" value = "{{$val->default_price}}">
Livewire doesn't use value attribute!
If you want to update Product->default_price, you can use https://laravel-livewire.com/docs/2.x/properties#binding-models or if you want to only output the default_price, then remove wire:model.
Also #foreach($products as $val) $products should be plural,
and $val should be $product, because it is a product variable. Naming is important, if you don't want to confuse yourself and others constantly.

How to get any column entry from another table of database based on the entry taken in form?

I am new to laravel and facing an issue. I have a database and there are two tables named groups and clients. I have a form and there are three fields: name, email and group. The group is dropdown select which is populated by groups entries from the database. Now while creating a new client, I want my clients table to have relation with groups table. It will work like this:
Will enter the name, email and will select the group it will associate with. The 'Client' table will get the id (There is a id column in the database, which is on auto_increament) of the 'Groups' table.
I have created one to one relation and when I put group_id manually in the database, it is showing the respective group. But I want the code to get the group_id while creating new entry.
Here is the code which is creating new entry:
Student::create([
'name' => request('name'),
'email' => request('email'),
'group_id' => request('group_name')// I want the group Id here,
'user_id' => auth()->user()->id
]);
return back()->with('success', 'The registration is completed');
Here is the form:
#extends ('layouts.app')
#section('main')
<div class="col-md-6">
<form method="post" action="/student" class="">
#csrf
<div class="form-group">
<label for="name">Client Name:</label>
<input class="form-control" type="text" name="name" required>
</div>
<div class="form-group">
<label for="email">Client Email:</label>
<input class="form-control" type="text" name="email" required>
</div>
<div class="form-group">
<label for="group_name">Select Group:</label>
<select class="form-control" name="group_name">
#foreach ($groups as $group)
<option>{{ $group->group_name}}</option>
#endforeach
</select>
</div>
<div class="form-group ">
<input class="btn btn-primary" type="submit" name="submit">
</div>
</form>
</div>
#endsection
Here is the error code:
SQLSTATE[HY000]: General error: 1364 Field 'group_group_name' doesn't have a default value (SQL: insert into `students` (`name`, `email`, `group_id`, `user_id`) values (Nayan Chowdhury, nayan.1aacl#gmail.com, Marketing, 1))
You are doing wrong. Try
<select class="form-control" name="group_name">
#foreach ($groups as $group)
<option value="{{ $group->id }}">{{ $group->group_name}}</option>
#endforeach
</select>

Add multiple rows with relation many-to-many

I'm trying to save a multiple database records with image name and thumb(for now only text inputs). Every image has also a category/categories (made by multiselect and pivot table between image and categories with many to many relation). The question is: how to sync/attach those categories? I do not want to use foreach ::create, because it will generate unnecessary database requests. For now I have something like this:
my blade file (part in form)
<form action="/panel/images" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="row[1][title]">Image title</label>
<input type="text" name="row[1][title]" class="form-control" />
</div>
<div class="form-group">
<label for="row[1][thumb]">Thumb</label>
<input type="text" name="row[1][thumb]" class="form-control" />
</div>
<!-- date because insert methode don't create timestamps autimatically-->
<input type="hidden" name="row[1][created_at]" value="{{ $date }}" />
<input type="hidden" name="row[1][updated_at]" value="{{ $date }}" />
<div class="form-group">
<label for="multi[0][]">Categories</label>
<select name="multi[0][]" multiple>
#include('layouts.images_categories_select')
</select>
</div>
<div class="form-group">
<label for="row[2][title]">Image title</label>
<input type="text" name="row[2][title]" class="form-control" />
</div>
<div class="form-group">
<label for="row[2][thumb]">Thumb</label>
<input type="text" name="row[2][thumb]" class="form-control" />
</div>
<div class="form-group">
<label for="multi[1][]">Categories</label>
<select name="multi[1][]" multiple>
#include('layouts.images_categories_select')
</select>
</div>
<input type="text" name="row[2][created_at]" value="{{ $date }}" />
<input type="text" name="row[2][updated_at]" value="{{ $date }}" />
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
And the controller
public function store(Request $request)
{
$image = new Image;
$inputs = Input::get('row');
$multiSelect = Input::get('multi');
$image::insert($inputs);
$image->images_catetories()->sync($multiSelect);
return redirect()->route('newImage')->with('status', 'images added');
}
EDIT:
Right now it throws an error "SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: insert into image_images_category (0, image_id, images_category_id, 1) values (18, , 1, 19))"
where "image_images_category" is my pivot table and values 18, 1, 19 are from multiselect.

Laravel record display from database

I have this leftJoin in my Laravel project
products = DB::table('products')
->leftJoin('destinations','products.id','=','destinations.product_id')
->get();
Tables:
Product: id,name Destinations:id,product_id,destination,quantity,target_person
but i don't knwo how to display leftJoin,(i just want to display name form product and quantity,destination ,target_person from destinations)
and i want to add new record
This is my form :
<form action="." method="POST" id="formid">
{{ csrf_field() }}
<div class="form-group">
<label for="first_name">Product name</label>
<input class="form-control" name="name" >
</div>
<div class="form-group">
<label for="last_name">Quantity</label>
<input class="form-control" name='quantity'>
</div>
<div class="form-group">
<label for="last_name">Destination<label>
<input class="form-control" name='Destination'>
</div>
<div class="form-group">
<label for="last_name">Target Perosn</label>
<input class="form-control" name='target_person'>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
i never do this,please help me this,im beginner in this.
As you have mentioned, I just want to display name from product and quantity,destination ,target_person from destinations. You have to use table alias like:
products = DB::table('products as t1')
->leftJoin('destinations as t2','t1.id','=','t2.product_id')
->select('t1.name', 't2.quantity', 't2.destination', 't2.target_person')
->get();

Laravel pagination is not working with date search

I have a couple of search forms in my app in which pagination works great, but when it comes to date search I only get the first page, when I try to go to the second page I get an undefined $ticket variable. When I look at the URL I can see that it doesn't take the date values with him to the next page.
Here is my code:
<tbody class="searchTable">
#foreach($ticket as $tickets)
<tr>
<td>
{{Carbon::parse($tickets->created_at)->format('m/d/Y')}}
</td>
<td>{{$tickets->id}}</td>
<td>{{$tickets->clientName}}</td>
<td>
{{Carbon::parse($tickets->dueDate)->format('m/d/Y')}}
</td>
<td>{{$tickets->refNumber}}</td>
<td>{{$tickets->invoiceNumber}}</td>
<td>{{$tickets->jobLocation}}</td>
<td>{{$tickets->workDescription}}</td>
<td>{{$tickets->jobStatus}}</td>
</tr>
#endforeach
</tbody>
{!! $ticket->appends(Request::only('_token','search'))->render() !!}
This is the controller:
$ticket = DB::table('tickets')
->whereBetween('created_at', [$newDateFrom, $newDateTo])
->orderBy('created_at', 'desc')
->paginate(10);
return view('ticketsView', compact('ticket'));
Here is my solution:
I have the same view for different search requests so I append the results to the paginator so it will have it on the next pages as well. The name is the value of the name attribute in the search form. In my case I had multiple. Here is an example:
{!! $ticket->appends(Request::only(['dateFrom'=>'dateFrom', 'dateTo'=>'dateTo', 'search'=>'search', 'filter'=>'filter', 'dueDateFrom'=>'dueDateFrom','dueDateTo'=>'dueDateTo']))->render() !!}
So now if my search results will contain in the URL dateTo and dateFrom values for example then it will be saved to all pages. It's important to understand that these values come from the name attribute of your search form.
Here is an example:
<form class="form-horizontal" role="form" method="GET" action="/ticket/searchresults" accept-charset="UTF-8" enctype="multipart/form-data">
<div class="text-centered">
<p><strong>Search by dates:</strong></p>
</div>
<div class="form-group">
<label for="filter">Select Client (optional)</label>
<select class="form-control" name="filter" type="text">
<option disabled selected> -- select client -- </option>
#foreach($selectClient as $selectClients)
<option value="{{$selectClients->name}}">{{$selectClients->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="dateFrom">From:</label>
<input class="datepicker2 form-control" name="dateFrom" type="text" required/>
</div>
<div class="form-group">
<label for="dateTo">To:</label>
<input class="datepicker2 form-control" name="dateTo" type="text" required/>
</div>
<button type="submit" class="btn btn-primary"><span class="fa fa-fw fa-search"></span></button>
</form>

Categories