dimanche 29 mai 2016

JSON endpoints alongside regular actions in controllers

I have a QuizzesController which implements all of the standard restful actions: create, store, edit, etc. And I also have additional presentQuestion and answerQuestion:

public function presentQuestion 
{
    // . . .
    return response()->json($question);
}

public function answerQuestion($quiz_id)
{
    // . . .        
    $this->handleAnsweredQuestion($question_id);
}

Is it a good practice to mix the JSON endpoints with regular php actions in controllers, or can this design cause any unexpected problems in the future?



via Chebli Mohamed

Whats the better approach in managing large repeating csv data in laravel?

I am adding a function to our existing Laravel application managing financial transaction related data. Each week we are getting the data in csv files using the below given predefined format. I am wondering which approach I should use to load that data into the database

  1. Should I load the csv data straight into a single table using same fields in the csv format? or;
  2. Should I split the table into originators, and destination?

e.g. csv format: transaction_id, date, Originator, origin_id, currency, amount, destination, destination_id,

This is especially important considering that in the end of the day, users will need to search for entities and their associations using an ID number or entity name

If it helps, we get millions of records in each csv file often with same individuals and entities repeating on either side of the transaction.

Thanks in advance :)



via Chebli Mohamed

How to display data returned from ajax response as a view in laravel

everyone I'm new to Ajax, and want to return a partial view in laravel in response of an ajax call, so when I click on a link to display the data in a modal it does not work and saying trying to get property of none object. any help. this is my code:

<tr>
        <td style="border-right: 1px solid #ddd;"><?php echo $i;?></td>
         <td></td>
         <td></td>
         <td></td>
        <td> &nbsp; </td>
        <td></td>
         <td></td>
        <td></td>
        <td><span>$</span> </td>
        <td><span>$</span><?php echo $sale->quantity*$sale->unit_price;?> </td>
    <td>
       <a href="#bill" class="btn btn-xs btn-green mr-5" type="button" tabindex="0" data-toggle="modal" data-target="#reportModal" onclick="load_modal_data('','/sales/bill','billContent')">
                                            <i class="fa fa-pencil"> Bill</i></a>
                                        <a href="/purchase/item" class="btn btn-xs btn-green mr-5"><i class="fa fa-search"> View</i></a>
                                        <a href="#" class="btn btn-xs btn-lightred"><i class="fa fa-remove"> Del</i></a>
                                    </td>
                                </tr>

the Js and Ajax:

function load_modal_data(identity, route,target_tag)
{

    $.ajax({
        headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') },
        url: route,
        type:'post',
        data:{ id: identity},
        success:function(result){
            console.log(result)
            $('#'+target_tag).html(result);
        }
    })
}

the controller:

public function bill()
{
    $id = Input::get('identity');
    $sales = DB::table('sales')
        ->join('brands','brands.bid','=','sales.brand_id')
        ->join('units','units.unit_id','=','sales.unit_id')
        ->join('categories','categories.cat_id', '=','sales.category_id')
        ->join('customers','customers.cid','=','sales.c_id')
        ->select('sales.*','brands.brand_name','categories.category_name','customers.fname','customers.lname','units.unit_name')
        ->where('sales.sale_id',$id)->first();
    $returnHTML = view('partials.item-bill')->with('sales',$sales)->render();
    return response()->json(array('success'=>true, 'html'=>$returnHTML));
}

Note: I'm using Laravel 5.2



via Chebli Mohamed

Laravel Download from S3 To Local

I am trying to download a file that I stored on S3 to my local Laravel installation to manipulate it. Would appreciate some help.

I have the config data set up correctly because I am able to upload it without any trouble. I am saving it in S3 with following pattern "user->id / media->id.mp3" --> note the fact that I am not just dumping files on S3, I am saving them in directories.

After successfully uploading the file to S3 I update the save path in my DB to show "user->id / media->id.mp3", not some long public url (is that wrong)?

When I later go back to try and download the file I am getting a FileNotFoundException at S3. I'm doing this.

$audio = Storage::disk('s3')->get($media->location);

The weird thing is that in the exception it shows the resource that it cannot fetch but when I place that same url in a browser it displays the file without any trouble at all. Why can't the file system get the file?

I have tried to do a "has" check before the "get" and the has check comes up false.

Do I need to save the full public URL in the database for this to work? I tried that and it didn't help. I feel like I am missing something very simple and it is making me crazy!!



via Chebli Mohamed

Laravel migrations: create referenced table before making a reference to it

Here is my migration method:

public function up()
{
    Schema::create('items', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->integer('item_type_id')->unsigned();
        $table->integer('character_class');
        $table->integer('character_race');
        $table->integer('required_level');
        $table->integer('quality');
        $table->integer('durability');
        $table->integer('buy_price');
        $table->integer('sell_price');
        $table->timestamps();
    });

    Schema::table('items', function($table) {
        $table->foreign('item_type_id')->references('id')->on('item_types')->onDelete('cascade');
    });
}

The problem is that migration for the item_types table is after items migration. So there is no item_types table while creating items table, then migration will fail at creating foreign key. Is there a way to delay foreign constraints and run them after table creations? Or I have to separate the foreign constraints to another migration?! Thanks.



via Chebli Mohamed

Store uploaded images with Laravel 5

I'm trying to upload, convert and store an image in Laravel using Image Magick.

Inside App\Http\Controllers\ArticleController:

$image = $this->storeMainImage($request->file('thumbnail'));

The function:

private function storeMainImage($file) {
  $folder = 'uploads/images/'; <--- ?????
  $code = uniqid();
  $thumb_code = $folder . 'thumb_' . $code . '.jpg';
  $image_code = $folder . $code . '.jpg';
  if(@is_array(getimagesize($file))){
    exec('convert '.$file.'  -thumbnail 225x225^ -gravity center -extent 225x225  -compress JPEG -quality 70  -background fill white  -layers flatten  -strip  -unsharp 0.5x0.5+0.5+0.008  '.$thumb_code);
    exec('convert '.$file.'  -compress JPEG -quality 70  -background fill white  -layers flatten  -strip  -unsharp 0.5x0.5+0.5+0.008  '.$image_code);
    return $image_code;
  } else {
    return false;
  }
}

I don't get any errors with this, but I have no idea if it's actually uploading the file and where abouts it's storing it.



via Chebli Mohamed

Laravel5.0/5.1 application deployment error

When I transfer my laravel5.1 application to the server the following error occurs

Internal Server Error.
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, ectlink@gmail.com and inform them of the time the error occurred, 
and anything you might have done that may have caused the error. 
More information about this error may be available in the server error log.

Application Version Apache version: Apache/2.2.31 PHP version: 5.6.14 MySQL version: 5.1.73

System Info Distro Name: CentOS release 6.7 (Final) Kernel Version: 2.6.32-573.18.1.el6.i686 Platform: i686



via Chebli Mohamed