lundi 28 septembre 2015

Laravel boot method to delete relationship tables

I have the following boot method setup in my user model:

/**
 * Boot the model.
 *
 */
public static function boot()
{
    parent::boot();

    static::deleting(function($user)
    {
        $user->roles()->detach();
        $user->supervisors()->detach();
        $user->types()->detach();
        $user->rates()->detach();
        $user->miscs()->detach();
        $user->timesheets()->delete();
    });
}

A user can have many timesheets so I have a hasMany relationship setup for timesheets in the user model.

/**
 * The timesheets that belong to the user.
 *
 * @return Object
 */
public function timesheets()
{
    return $this->hasMany('App\Models\Timesheet\Timesheet');
}

A timesheet can also have many data, so I have added a relationship in the timesheet model:

/**
 * The data that belong to the timesheet.
 *
 * @return Object
 */
public function data()
{
    return $this->hasMany('App\Models\Timesheet\DataTimesheet');
}

My question is, when I delete a user, it should delete the timesheet and all data associated with it. At the moment, when I delete a user it deletes the user and the timesheet but not the data (DataTimesheet).

When I delete a timesheet separately, the data gets deleted.

Can anyone suggest how I can delete the data as well when a user is deleted?



via Chebli Mohamed

Laravel 5.1 eloquent orm relationship

I have following setup:

Profile.php model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{

    public function user()
    {
        return $this->belongsTo('App\Models\User');
    }
}

User.php model:

<?php namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    public function profile()
    {
        return $this->hasOne('App\Models\Profile');
    }
}

AuthController.php:

public function signup(Request $request)
    {
        $user = new User;
        $profile = new Profile;
        $user->email = $request->input('email');
        $user->first_name = $request->input('first_name');
        $user->last_name = $request->input('last_name');

        $user->password = Hash::make($request->input('password'));

        $user->save();
        $role = Role::whereName('user')->first();

        $profile->city = $request->input('city'); ---> profile model
        $profile->gender = $request->input('gender'); --> profile model
      } 

How can I save to profile based on relationship? I can't directly save to profile due to foreign key constraint.

Thanks!!



via Chebli Mohamed

Add HTML page in Laravel

Is it necessary to use blade file in laravel or we can add .html page for UI purpose and if yes then how to do this.

This sounds like a stupid question, but still I want to know about this.



via Chebli Mohamed

How to use Cloud Storage to multipart upload Music/Video (for Laravel)?

I'm thinking using S3 Amazon for storage (if you guys have better one, don't hesitate to recommend). I could do a normal file upload, but let's say I'm uploading a very large video/music file (something > 20mb), I will hit php upload maxfilesize.

Yes I can just set php_max_filesize value but I am thinking something else.

How to break those files into smaller chunks, and upload each chunks (like how you download torrent files, they each have the download chunks)? And stopping and continuing them anytime the user wants?

This (http://ift.tt/1QIf6CF) does not work, because it needs to read the file ON THE SERVER first, before uploading it, so it's useless. Are there any Laravel/PHP/JS library for this?

Or, I wonder how youtube did it?



via Chebli Mohamed

Laravel 'Unresolvable dependency resolving [Parameter #0 [

Error on Laravel

Getting blank page Laravel version: 5.1.16 Error Logged with below message

[2015-09-28 09:37:48] local.CRITICAL: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Uncaught exception 'Illuminate\Contracts\Container\BindingResolutionException' with message 'Unresolvable dependency resolving [Parameter #0 [ $path ]] in class GrahamCampbell\Exceptions\ExceptionInfo' in /bootstrap/cache/compiled.php:1282

What is the root cause ? what is the solution for this problem ?



via Chebli Mohamed

Laravel 5 - Query with left join and subqueries

What I have

I have a working Query, which I can run successfully as plain SQL command on my SQL server.

SELECT
  i.hw_id
, i.hw_hostname
, i.hw_create_date created_at
, uc.username created_by
, he.hw_edited_date last_edit_date
, ue.username last_edit_by
FROM tbl_hw_inventar AS i
  INNER JOIN tbl_user AS uc ON i.hw_create_user_idfs = uc.id
  LEFT OUTER JOIN (
            SELECT
                  het.*
            FROM tbl_hw_edited AS het
                  INNER JOIN (
                        SELECT
                              hw_edited_client_idfs
                            , MAX(hw_edited_date) AS last_edit_dt
                        FROM tbl_hw_edited
                        GROUP BY hw_edited_client_idfs
                  ) AS mx ON het.hw_edited_client_idfs = mx.hw_edited_client_idfs
                              AND het.hw_edited_date = mx.last_edit_dt
      ) AS he ON i.hw_id = he.hw_edited_client_idfs
  LEFT OUTER JOIN tbl_user AS ue ON he.hw_edited_user_idfs = ue.id

Which gives me the following result:

| hw_id | hw_hostname | created_at          | created_by | last_edit_at        | last_edit_by |
| 1     | client-01   | 2015-09-28 08:15:36 | user1      | 2015-09-28 10:22:12 | user3        |
| etc. etc. etc.

What I need

Now I have to write this query as a laravel 5 command.

But there I don't know how.

The code

I tried with something like "where in" like this:

hardwareitems = DB::table('tbl_hw_inventar')
                        ->join('tbl_user', 'tbl_hw_inventar.hw_create_user_idfs', '=', 'tbl_user.id')
                        ->whereIn('tbl_hw_inventar.hw_id', function($query){
                            $query->select(DB::raw('MAX(hw_edited_id)'))
                                    ->from('tbl_hw_edited')
                                    ->whereRaw('tbl_hw_edited.hw_edited_client_idfs = tbl_hw_inventar.hw_id');
                        })
                        ->select('tbl_hw_inventar.*', 'tbl_hw_typ.hw_typ_title', 'tbl_user.username', 'tbl_hw_edited.*')
                        ->orderBy('tbl_hw_inventar.hw_id', 'asc')
                        ->get();

This code snippet is from a try with a smaller query, as I just wrote down the right query.

How can I convert my SQL query into the laravel query?

Thanks for any help in advance



via Chebli Mohamed

dimanche 27 septembre 2015

Laravel Close mongodb connection

I am using laravel 5.1 with jessenger mongodb. I am unusre if the connection to the mongodb is closed when not in use.if not what is the way i can close the connection.



via Chebli Mohamed