dimanche 1 septembre 2019

Laravel - Dyanmic Dropdown-list with a relational database

I'm just starting out laravel and I'm confused on how do I start configuring my drop-down list. I need to create a position with a department and section drop-down.

Here is the first filter to be used. Department Model

use App\SectionModel;

class DepartmentModel extends Model {
    protected $table = 'tbl_department';
    protected $fillable = ['department_name'];

    public function section () {
        return $this->hasMany('App\SectionModel', 'department_id'); 
    }
}

Second selection would be based from the selected department. Section Model

use App\PositionModel;

class SectionModel extends Model {
    protected $table = 'tbl_section';
    protected $fillable = ['department_id', 'section_name'];

    public function department () {
        return $this->belongsTo('App\DepartmentModel');
    }

    public function position () {
        return $this->hasMany('App\PositionModel', 'section_id');
    }
}

Position Model

class PositionModel extends Model {
  protected $table = 'tbl_position';
  protected $fillable = ['section_id', 'position_name', 'n_level', 'job_description'];

  public function section () {
    return $this->belongsTo('App\SectionModel', 'id');
  }
}

Position Controller

use Illuminate\Http\Request;
use App\PositionModel;
use App\EmployeeModel;
use App\DepartmentModel;
use App\SectionModel;

class PositionController extends Controller
{

  public function index()
  {
    $getEmployee = EmployeeModel::all();
   return view('DashboardView.main', compact('getEmployee'));
  }

  public function create()
  {
    $getPosition = PositionModel::with('section')->get();
    $getDepartment = DepartmentModel::all();
    $getSection = SectionModel::all();
    return view('PositionView.add', compact('getPosition', 'getDepartment', 'getSection'));
  }

Add.blade.php

            <div class="form-group col-md-12 row">
                <label class="col-md-4 col-form-label text-md-right"></label>
                <div class="col-md-6">
                    <select name="department" class="form-control">
                        <option value="0">Please Select Department</option>
                        @foreach ($getDepartment as $department)
                        <option value=""></option>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="form-group col-md-12 row">
                <label class="col-md-4 col-form-label text-md-right"></label>
                <div class="col-md-6">
                    <select name="section" class="form-control">
                        <option value="0">Please Select Section</option>
                        @foreach ($getSection as $section)
                        <option value=""></option>
                        @endforeach
                    </select>
                </div>
            </div>

Route

 Route::resource('position', 'PositionController');



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2lQo39h
via IFTTT

how to zip local folder and download it?

i need to compress a local folder in order for the user to download it, this folder is a small web page with sub folders and images in it.

What i need to do is:

1.- Create a text file (i already have this) 2.-Compress the folder that i want with the text file that i already created (this is where im stuck)

i tried with jszip-utils but it wont work.

function descargaPlantilla(){
    let zip = new JSZip();
     var filename = "imagenes.html";

    var blob = new Blob([result], {
        type: "text/plain;charset=utf-8"
    });

    saveAs(blob, filename);  


    SZipUtils.getBinaryContent("/Users/ricardowallacelimon/E4", function (err, data) {
        if (err) {
            throw err; // or handle the error
        }
        zip.file("/Users/ricardowallacelimon/E4", data, { binary: true });
    });




}

i get these errors:

TypeError: global.JSZipUtils is undefinedjszip-utils-ie.js:36:1ReferenceError: SZipUtils is not defined4 cenidet.js:180:5

ReferenceError: SZipUtils is not defined



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2ZIT6Cj
via IFTTT

Laravel 5 hasMany relation returning incorrect relations

I have a Yard model and Treatment model, I am using the following hasMany relationship to return treatments that are currently active:

public function activeTreatments() {
    return $this->hasMany('App\Models\Treatment')
    ->where(function ($q) {
        $q->where('expires_at','>=', Carbon::now('Pacific/Auckland'))
        ->where('completed_at','>', Carbon::now('Pacific/Auckland'));
    })
    ->orWhere('completed',false);   
}

For some reason when I add the ->orWhere('completed',false) the query returns all treatments not just the treatments associated with the specific yard. What am I doing wrong here?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Py5wgh
via IFTTT

Duplicate function update

I am trying to create a duplicate system, I have a form revision with a recording.

For example, I have to change the value of my field hour_start => 18:00

enter image description here

I want to replace by the value => 08:00

enter image description here

When, I validate the form I have an error message:

enter image description here

The value of my field hour start is always the same

enter image description here

In fact, my duplicate system must work for dates, but if I change a value of my field hour_start, it must pass...

public function update(revisionRequest $request, $id)
    {

        $exists = Revision::where('date_revision_start', $request->get('date_revision_start'))->where('date_revision_end', $request->get('date_revision_end'))->where('hour_start', $request->get('hour_start'))->where('hour_end', $request->get('hour_end'))->where('fk_motorbike', $request->get('fk_motorbike'))->where('fk_garage', $request->get('fk_garage'))->count();

        $date_revision_start = $request->get('date_revision_start');
        $date_revision_end = $request->get('date_revision_end');

        $conflict = Revision::where('fk_motorbike', $request->get('fk_motorbike'))->whereDate('date_revision_start', "<=" , $date_revision_start)
            ->whereDate('date_revision_end', ">=", $date_revision_start)
            ->first();

        $conflict2 = Revision::where('fk_motorbike', $request->get('fk_motorbike'))->whereDate('date_revision_start', "<=" , $date_revision_end)->whereDate('date_revision_end', ">=", $date_revision_end)->first();

        if(isset($conflict2) || isset($conflict)){
            return redirect()->route('revisions.index')
             ->with('error', 'Duplicate date! ');
        }


        else{
            $revisions = Revision::find($id);
            $revisions->date_revision_start = $request->get('date_revision_start');
            $revisions->date_revision_end = $request->get('date_revision_end');
            $revisions->hour_start = $request->get('hour_start');
            $revisions->hour_end = $request->get('hour_end');
            $revisions->fk_garage = $request->get('fk_garage');
            $revisions->fk_motorbike = $request->get('fk_motorbike');
            $revisions->save();
            return redirect()->route('revisions.index')
                    ->with('success', 'ok')->withInput();
        }

    } 

Thank you for your help.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2ZIF2bZ
via IFTTT

Invalid argument supplied for foreach() To-do app

i have a function that displays tasks of current user

public function test(Request $request)
{
    $user= auth()->user();

    return view('welcome')->with('tasks',$user->tasks);
}

This is the code that displays the posts in home view

@foreach($tasks as $task)
    <tr>
        <td class="table-text">
            <div></div>
        </td>
    </tr>
@endforeach

Am getting the error above



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Zukt7z
via IFTTT

How to make Laravel pagination without refresh page

I'm making an online exam and I want to paginate the questions in different pages but the problem is that when I go to the next page the previous answers gone because the page refresh so how can I make the Laravel pagination withou refresh?

running Laravel 5.8

controller code:

$questions = $exam->questions()->paginate(1);
return view('exam.exam',compact('questions','exam'));

$questions = $exam->questions()->paginate(1);
return view('exam.exam',compact('questions','exam'));

view code

   {!! $questions->render() !!}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2ZCKUr6
via IFTTT

Laravel Redis key prefix not working as intended

I have a rather weird problem with Laravel lately that when I am storing something in Redis and then get the keys and try to retrieve the values using that key, I get null. However when I manually remove the prefix from the key via substring or something similar, I am able to retrieve the data. Example:

Redis::hmset('users:'.$db_user->id,$db_user->toArray());

Then I do the following to retrieve the stored keys:

foreach(Redis::redis->keys('users:*') as $key){
        Redis::redis->hgetall($key); //returns null
        Redis::hgetall(substr($key,18))); //returns the stored values
    }

A key would come out as:

redisser_database_users:78

Which is to be expected given the prefix settings in Laravel. Now I wonder if this behaviour is intended or not. If it is, it doesn't make a whole lot of sense to me.

Can this be fixed somehow so I can use the keys directly without manually cutting off the prefix?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2HD1a1g
via IFTTT