mercredi 7 septembre 2016

Updating MySQL Laravel 5.1 using Ajax Post

for some reason, I can not get a record within MySQL to update using Ajax. The record is a VARCHAR(1000). There has to be a syntax error. But I can not see it. I have tried everything - any ideas ? Thanks !

$(".edit_comment").change(function(){

    var comment = $(".edit_comment").val()  // This is just text from a text box. 
    var $id = $("#customer_id").val();
    $.ajax({
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
        type: "POST",
        url: 'update_comment/'+$id,
        data: comment,
        success: function () {
            console.log(comment, $id);  // I can see the correct text & id here 
            alert('Your Comment is Updated')
       },
        error: function () {
            alert('there has been a system level error - please contact support')
        }
    });

Laravel Controller:

    public function update_comment(Request $request, $id){

   $comment = $request->get('edit_comment');
    Quotation::where('id', $id)
        ->update(['comment' =>$comment]);
     }



via Chebli Mohamed

Laravel 5.1 create 5 minute cookie then return view

I want to set 5 minutes cookie then return to view,

But if i use view in Response, cookie loses. And when i back to index page, i can't see that cookie. Because it lost.

But if i return response without view, it works perfectly.

$response = new Illuminate\Http\Response();
return $response->withCookie(cookie('yes', 'value', 5));

This works perfectly.

But this is not:

$response = new Illuminate\Http\Response(view('somepage'));
return $response->withCookie(cookie('yes', 'value', 5));

How can i create view after set up cookie?



via Chebli Mohamed

how to generate powerpoint in laravel dynamically?

i am facing problem in creating power point slide dynamically in Laravel. I don't have any idea which package to use for power point. Please suggest me.



via Chebli Mohamed

Implicit model binding in laravel 5.1

Implicit model binding in laravel 5.1 described very poor. When I am trying to do like in manual I always get error ReflectionException in RouteDependencyResolverTrait.php line 81: Class App\Http\Controllers\App\News does not exist
Of course it is namespace issue, but I cannot understand to fix it. I tried to comment $namespace, but it is not helped me.

<?php namespace App\Providers;

use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    protected $namespace = 'App\Http\Controllers';

    public function boot(Router $router)
    {
        parent::boot($router);
        $router->model('newsitem', 'App\News');
    }

here is the error description

Whoops, looks like something went wrong.
1/1 ReflectionException in RouteDependencyResolverTrait.php line 81: Class App\Http\Controllers\App\News does not exist

    in RouteDependencyResolverTrait.php line 81
    at ReflectionParameter->getClass() in RouteDependencyResolverTrait.php line 81
    at ControllerDispatcher->transformDependency(object(ReflectionParameter), array('newsitem' => object(News)), array('newsitem' => object(News))) in RouteDependencyResolverTrait.php line 61
    at ControllerDispatcher->resolveMethodDependencies(array('newsitem' => object(News)), object(ReflectionMethod)) in RouteDependencyResolverTrait.php line 44
    at ControllerDispatcher->resolveClassMethodDependencies(array('newsitem' => object(News)), object(NewsController), 'getItem') in ControllerDispatcher.php line 162
    at ControllerDispatcher->call(object(NewsController), object(Route), 'getItem') in ControllerDispatcher.php line 112
    at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
    at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114
    at ControllerDispatcher->callWithinStack(object(NewsController), object(Route), object(Request), 'getItem') in ControllerDispatcher.php line 69
    at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\NewsController', 'getItem') in Route.php line 203
    at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134
    at Route->run(object(Request)) in Router.php line 708
    at Router->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
    at Pipeline->then(object(Closure)) in Router.php line 710
    at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 675
    at Router->dispatchToRoute(object(Request)) in Router.php line 635
    at Router->dispatch(object(Request)) in Kernel.php line 236
    at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 50
    at VerifyCsrfToken->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
    at ShareErrorsFromSession->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
    at StartSession->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
    at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
    at EncryptCookies->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
    at CheckForMaintenanceMode->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
    at Pipeline->then(object(Closure)) in Kernel.php line 122
    at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
    at Kernel->handle(object(Request)) in index.php line 54



via Chebli Mohamed

Laravel 5.1 Move Uploaded File

I have a form with a file upload field and a hidden field to store the uploaded file name as follows:

<form enctype="multipart/form-data" action="http://localhost:8000/create" method="POST"> 
   // other form fields     

    <input id="img_logo" type="hidden" value="" name="img_logo">
    <input type="file" name="file" id="file" class="inputfile"/>
    <label for="file" class="btn btn-primary" >Upload</label>

    <div id="img-thumb"></div> 

    <input type="submit" value="Submit">
</form>

My ajax file upload method populates the filename of the hidden img_logo field as follows after a successful upload:

this.on("success", function(file, response) {
    $('input[name="img_logo"]').val(response.file);
    $("#img-thumb").html("<img src='/img/tmp/"+response.file+"' />");
}

This is the upload method which gets called when a file is uploaded

public function upload(Request $request) {

    if($request->hasFile('file')) {

        //upload an image to the /img/tmp directory and return the filepath.
        $file = $request->file('file');

        $tmpFileName = time() . '-' . $file->getClientOriginalName();

        $tmpFilePath = '/img/tmp/';

        Image::make($file->getRealPath())->resize(360, 180)->save(public_path($tmpFilePath) . $tmpFileName);

        return response()->json([
            'success' => true,
            'file' => $tmpFileName
        ] , 200);

    } 
    else {
            return response()->json(false, 200);
    }
} 

When I submit the form I want to move the image from img/tmp directory to img/logos directory and rename the file by prefixing the article id. I can determine if a file has been uploaded by checking the img_logo hidden input value as follows:

Public function create(Request $request)
{
   $article = Article::create($request->all());

   if ($request->has('img_logo')) {

        // How do i copy the file from img/tmp to img/logos with new name

        $fileName = $article->id . '-' . Request::input('img_logo');

        $article->img_logo = $fileName;

        $article->save();
    } 


}



via Chebli Mohamed

\Auth::user() is null in 5.3.6?

This is about Laravel 5.3.6

I am able to login successfully and I can check Auth User after login. I can show the exact location where Auth::guard() has current user object. Below are the details.

  1. Go to Login Controller
  2. Go to AuthenticatesUsers Trait
  3. Go to sendLoginResponse method. User reaches here successfully because user is authenticated successfully.

here I can check $this->guard()->user() has current user value. But when control reaches to Role controller....I tried to access it like this dd(Auth::guard()); and value was null. I also added reference below in Role Controller.

use Illuminate\Support\Facades\Auth;

Below is my route for Role Controller.

Route::group(['middleware' => ['auth']], function () {
    Route::get('/Roles',              
        array (
            'uses'  => 'Website\Role\RoleController@index',      
            'as'    => 'Roles'
        )
    );
});

Did you face this kind of issue in Laravel 5.3.6?



via Chebli Mohamed

mardi 6 septembre 2016

Laravel Sender or From not working as expected

I have this issue in the laravel 5.1 mail. Though I already assigned a different email, it is still getting the smtp user address. To explain more here are my configs:

in my .env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_FROM_ADDRESS=whatiwant@mydomain.com
MAIL_FROM_NAME=myname
MAIL_USERNAME=validgmail@gmail.com
MAIL_PASSWORD=mypassword

my code for sending mail

$message->from(getenv('MAIL_FROM_ADDRESS'), getenv('MAIL_FROM_NAME'));
$message->sender(getenv('MAIL_FROM_ADDRESS'), getenv('MAIL_FROM_NAME'));
$message->to($to, $name)->subject($subj);

in my config/mail

'from' => ['address' => getenv('MAIL_FROM_ADDRESS'), 'name' => getenv('MAIL_FROM_NAME')],

So supposedly when it was sent to the recipient it should show that the sender now will be from "myname<whatiwant@mydomain.com>" instead it shows "myname<validgmail@gmail.com>".

PS: In gmail their's an option to Show Original and from there it shows this:

Received: from local.com ([my_ip])
        by smtp.gmail.com with ESMTPSA id *******
        for <recipient@gmail.com>
        (version=TLS1 cipher=**** bits=128/128);
        Tue, 06 Sep 2016 20:25:36 -0700 (PDT)
From: myname <validgmail@gmail.com>
X-Google-Original-From: myname <whatiwant@mydomain.com>

Please help..



via Chebli Mohamed