I have a table of customers where i need to generate unique id for each customer manually rather auto increment it in mysql. How to achieve this? I was suggested the below code with uniqid(md5(spl_object_hash($custApp)), false); function:
$data = file_get_contents("php://input");
$custApp = json_decode(base64_decode($data));
$custApp->Id = uniqid(md5(spl_object_hash($custApp)), false);
But i am not getting how to use this function in my code
use sha1($nombre_costumer) and save the value how your id unique
Related
I have inserted multiple machine ids( separated with , ) in a single cell which I got from a multi select dropdown, now i have all the machine ids but i want to fetch all the data related to those machines( stored in table name 'machines' ) and show the data in the same page for all the ids.
Hear is the image of my table
public function ShowProducts(Request $Request){
$ProductTitle = $Request->id;
$MillDetails = DB::table('plants')->where('PlantName',"=",$ProductTitle)->first();
$Plants = DB::table('plants')->where('status',"=",'Active')->get();
$AvailableMachines = $MillDetails->Machines;
return $riun = explode(',',$AvailableMachines);
// return view('Site.mill',compact('MillDetails','Plants'));
}
now I need help with fetching and showing the all the machines and related data to it.
well I am new to Laravel so any help will be really appreciated
Try this
$MillDetails = DB::table('plants')->where('PlantName',"=",$ProductTitle)->first();
Extract machine ids from MillDetails and you can directly use in Machine model.
$machines = Machine::whereIn('id',explode(',',$MillDetails->machine_ids))->get(); // use Model and column name as per yours
You can make function like this way. it will give you your disered output.
add this code in your plant model file Plant.php
public function getMachinesAttribute()
{
$machines= explode(',',$this->machines);
return Machine::find($machines); //here Machine is your App\Models\Machine
}
you can now use it like this way
$MillDetails = Plant::where('PlantName',"=",$ProductTitle)->first(); //here Plant is your App\Models\Plant
$MillDetails->Machines;
may this will help you ...
here is my code that i write in route file
`Route::get('/{username}/ajax-lead', function(){
$brand_id = Input::get('brand_id');
$table_data = DB::table('users')->where('brand_id',$brand_id)->get();
$table_name = $table_data[0]->table_name;
$table_lastRecords = DB::table($table_name)->get();
$columns = Schema::getColumnListing($table_name);
$table_lastRecords = DB::table($table_name)->get();
return Response::json($table_lastRecords);
});`
here is my view file
`{{Form::open(array('url'=>'','files'=>true))}}
client list *
Select Clients
#foreach($brand as $userLeads){
brand_id }}">{{ $userLeads->name }}
}
#endforeach
{{Form::close()}}
$('#brand_name').on('change',function(e){
// body...
console.log(e);
var brand_id = e.target.value;
//ajax Call
$.get('/{username}/ajax-lead?brand_id=' + brand_id,function(data){
//success data
console.log(data);
});
});
`
i want to get table_name column value in response. i use json for response.
ANSWER: Your easiest solution would be to return an array of data, where the first value is the table_name and the second value is the collection of lastRecords. It would look something like this:
$responseArray[0] = $table_name;
$responseArray[1] = $table_lastRecords;
return json_encode($responseArray);
Then, in your view, you would parse your Json response and let table_name = responseData[0] and lastRecords = responseData[1]. Of course, you could also use non-numeric indices to make the code more readable, such as responseData['table_name'], but those semantics are up to you.
ADDITIONAL INFO:
With that said, there are several issues with your code that I feel I should point out. You should become more familiar with Eloquent queries, as using them properly will greatly simplify your code. For instance, these two lines:
$table_data = DB::table('users')->where('brand_id',$brand_id)->get();
$table_name = $table_data[0]->table_name;
Could be re-written as:
$table_name = User::where('brand_id',$brand_id)->first()->table_name;
But this makes me ask the question, is there only one table per brand? If so, why do you store this information with the user, and not in a 'brands' table? The way that this would make the most sense would be to have a Brands model that has a table_name column, and you would get the table name with this simple query:
$table_name = Brand::find($brand_id)->table_name;
In addition, you should avoid doing this type of work in your routes file. Anything not related specifically to ROUTING the user should be handled in the controller or model. Then, within your controller, you should be utilizing the User or Brand model in your queries by importing App\User and App\Brand.
Is there anyway to remove empty form value form request->all() method ?
This is what i am trying to do on update,In other words only post filled form values.
$data = request()->except(['_token','id']);
DB::table($table)->where('id',$id)->update($data);
Note:I have dynamically generated columns so i think i can't use those column in except parameters's list.
This updates all the column of the row,but all i want to update only those column that's value is filled and for the remaining columns/fields keep the same old value
Have a look at array_filter
// All posted data except token and id
$data = request()->except(['_token','id']);
// Remove empty array values from the data
$result = array_filter($data);
// update record
DB::table($table)->where('id', $arr)->update($result);
Hope this helps.
// app/controllers/GiftsController.php
public function update($id)
{
// Grab all the input passed in
$data = Input::all();
// Use Eloquent to grab the gift record that we want to update,
// referenced by the ID passed to the REST endpoint
$gift = Gift::find($id);
// Call fill on the gift and pass in the data
$gift->fill($data);
$gift->save();
}
I found this piece of code in this tutorial and works like charm. I hope it helps.
In my table I have a field that needs to be the same value as the id field which is auto increment and then the value is concatenated with certain strings.
How do I do this in store function of my controller?
I'm thinking of doing update right after insertion but I don't know how to get the inserted id, and that might be a problem if two or more users doing the same insertion into database at the same time.
Thanks
UPDATE:
in my store controller:
public function store(Request $request)
{
$kartukeluarga = new kartukeluarga();
$kartukeluarga->nomorkk = $request->input("nomorkk");
$kartukeluarga->kepalakeluarga = $request->input("kepalakeluarga");
$kartukeluarga->alamat = $request->input("alamat");
$kartukeluarga->save();
return redirect()->route('kk.index')->with('message', 'Item created successfully.');
}
the new field I want to add is 'rekamMedik' which doesn't need an input form but automatically concatenate the id and this string "kk-value of the id"
You can save details and then update that column.
If you have model Test.
$test = new Test();
$test->column_1 = "abc";
$test->column_2 = "def";
$test->column_x = ""; // Set Empty to the column where you will save id later
$test->save();
$test->column_x = "string".$test->id;
$test->save();
This might work.
How to avoid querying relationship after object creation?
$store = new Store();
$store->name = 'Store 1';
$store->save();
return response()->json($store->products());
Laravel is querying products table. I would like to avoid that since I know there is none. This is just an example.
return response()->json([]); is not an option. In real world I really need to use $store->products().
Use the wasRecentlyCreated property of model to identify if the model was inserted during the current request lifecycle.
$data = $store->wasRecentlyCreated ? [] : $store->products();