Laravel 5.6 foreach not showing any data - laravel-5.6

I want to ask. I use foreach for showing my data from database. But nothing happen on view. Here is my code :
public function page($page_url = null){
if($page_url == "villas"){
$villa = Pages::where(['url_page' => $page_url])->first();
$postvilla = Posts::where(['id_page' => $villa->id_page]);
return view('front.villa', compact(array('villa', 'postvilla')));
}else if($page_url == "admin"){
return redirect('/admin/home');
}
}

You can pass variables in compact like
return view('front.villa', compact('villa', 'postvilla'));

Related

Make authorization Laravel

Im got error in section elseif, i want make HOD looking only at its own department data
public function index(User $user)
{
if(auth()->user()->role == 'Admin')
{
$form = Form::all();
}
elseif(auth()->user()->positions == 'HOD')
{
$form = Form::all()->department('user_id', \Auth::user()->id)->get();
}
else
{
$form = Form::where('user_id', \Auth::user()->id)->get();
}
return view('form.index', ['list_form' => $form]);
}
what should i change in elseif code ?
Try to do a dd() on auth()->user()->positions if it returns nothing, the relation between User model and Positions doesnt exist, or is set up wrong.

How to insert array data in laravel 5.4

I have a product table and device table.
I have joined these two tables where I have selected device.id & product.name, now I want to insert this data into my existing device table using device.id.
I want to insert multiple dropdown values in my database. Can anyone tell me how can I solve this problem? Below is my Controller Code-
foreach ($request->all() as $value)
{
$deviceById= Device::find($request->id);
if($request->destination_type == 'dealer')
{
$deviceById->destination_location=$value->dealer_id;
}else
{
$deviceById->destination_location=$value->office_id;
}
$deviceById->save();
}
flash('Device Dispatch added successfully!')->success()->important();
return redirect()->back();
You can make array of all dropdowns value and convert into a json string and store it in database
You can do something like this. Change according to your requirement. It just a logic demo.
$deviceById= Device::find($request->id);
$destination = array();
foreach ($request->all() as $value)
{
if($request->destination_type == 'dealer')
{
$destination[] = $value->dealer_id;
}else
{
$destination[] = $value->office_id;
}
}
$jsonString = json_encode($destination);
$deviceById->destination_location = $jsonString
$deviceById->save();
flash('Device Dispatch added successfully!')->success()->important();
return redirect()->back();
I solve it this way.I had used Elequent ORM
if($request->destination_type == 'Dealer')
{
DB::table('devices')
->whereIn('id',$request->device_list)
->update(['dealer_id'=>$request->destination_location,'device_status'=>"Dispatch"]);
flash('Device Dispatch dealer added successfully!')->success()->important();
}else
{
DB::table('devices')
->whereIn('id',$request->device_list)
->update(['office_id'=>$request->destination_location,'device_status'=>"Dispatch"]);
flash('Device Dispatch office added successfully!')->success()->important();
}

Laravel 5.4 Collection Map Return Values

I'm having trouble creating a function on collection map with return values.
public function getCollectionFakeId($collection, $fieldNames){
$optimus = $this->optimus;
$result = $collection->map(function($item, $key) use ($optimus, $fieldNames) {
return [
$fieldNames[0] =>$optimus->encode($item->id),
$fieldNames[1] => $item->lastname
];
}) ;
dd($result);
return json_decode(json_encode($result), FALSE);
}
As you can see the return fieldNames[0] is being hardcoded. I don't know how many fieldNames it will received. I need to return those fieldnames with obfuscated Id. So basically The only changed is the Id. Here is the screenshot.
As you can see the fieldNames are just 2 but what if it becomes 5 or 6. I don't really know how many fieldNames they are going to pass in the parameter. How can I return it. Thanks.
In case someone will encounter this problem. Here is my solution...
public function getCollectionFakeId($collection, $fieldNames){
$optimus = $this->optimus;
$result = $collection->map(function($item, $key) use ($optimus, $fieldNames) {
$mapFieldNames = array_map(function($v) use ($optimus, $item) {
if( $v == 'id'){
return $optimus->encode($item->id);
}
else{
return $v;
}
}, $fieldNames);
return $mapFieldNames;
}) ;
dd($result);
return json_decode(json_encode($result), FALSE);
}
The result is the same. AWESOME!

Code igniter batch_insert

I have an issue where I want to make a survey app where questions are retrieved from the database and looped in a form with radio buttons where by on submission I get a multi dimensional array. Could some one help with the process of doing such a process. From form to controller and then to the model?
Here is my method
public function registerSelfAdmin(){
$fieldsData = $this->db->field_data('response');
foreach ($fieldsData as $key => $field){
$datacc = array( $field->name => $this->input->post($field->name) );
$this->db->insert('response', $datacc);
}
return ($this->db->affected_rows() != 1) ? false : true;
// $query=$this->db->insert_batch('response',$newData,'question_id');
// return ($this->db->affected_rows() != 1) ? false : true;
}

Laravel 4 hasMany with WHERE clause

Not sure if this is the correct way to add an additional query to the hasMany argument but was unsuccessful. Is this possible?
public function menuItems($parent=false){
if($parent){
$menuItems = $this->hasMany('MenuItem')->where('parent',$parent);
}else{
$menuItems = $this->hasMany('MenuItem');
}
return $menuItems;
}
When called using
$menu_items = $menu->menuItems(0);
This just seems to return an empty array when passed a parent. Even though data with MenuItem->parent = 0 exists
Do I need to some way distinguish I'm asking for my linked items "parent" and not the main models "parent"
public function menuItems(){
return $this->hasMany('MenuItem');
}
Called with
$menu_items = $menu->menuItems()->where('parent', 0)->get();
I am not sure about the query part but at first wouldn't passing a 0 to the function still register the $parent variable as false? So maybe just check if the $parent is not null.
public function menuItems($parent = null){
if(!$parent == null)){
$menuItems = $this->hasMany('MenuItem')->where('parent',$parent);
}else{
$menuItems = $this->hasMany('MenuItem');
}
return $menuItems;
}
In PHP 0 = FALSE, change this
if( $parent ){
for this
if( $parent !== false ){

Resources