mardi 20 avril 2021

Upload Image In Laravel With jQuery AJAX

I am trying to upload photo in Laravel using jQuery AJAX. But it's always failed many times with error message

The photo must be an image. The photo must be a file of type: jpeg, png, jpg, gif, svg.

I have no idea why. What's wrong with my code below?

Controller

$validator = \Validator::make($request->all(), [
        'photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);
    
    if ($files = $request->file('image')) {
            //insert new file
            $destinationPath = 'public/product_images/'; // upload path
            $image_path = date('YmdHis') . "." . $files->getClientOriginalExtension();
            $files->move($destinationPath, $image_path);
        }

        $productId = DB::table('products')->insertGetId(
            [
                'product_photo' => $image_path
            ]
        );

View

    $("#photo").fileinput({
        theme: 'fa',
        uploadUrl: '',
        uploadExtraData: function() {
            return {
                _token: $("input[name='_token']").val(),
            };
        },
        allowedFileExtensions: ['jpg', 'png', 'gif'],
        overwriteInitial: false,
        maxFileSize: 2000,
        maxFilesNum: 5,
        slugCallback: function(filename) {
            return filename.replace('(', '_').replace(']', '_');
        }
    });
                        
    $('#saveBtnForCreate').click(function(e) {
        e.preventDefault();
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });

        $.ajax({
            url: "",
            method: 'post',
            enctype: 'multipart/form-data',
            cache: false,
            dataType: 'JSON',
            data: {
                photo: $('#photo').val()
            },
            success: function(result) {
                if (result.errors) {
                    $('.alert-danger').html(
                        'An error in your input!'
                    );
                    $.each(result.errors, function(key, value) {
                        $('.alert-danger').show();
                        $('.alert-danger').append('<strong><li>' + value +
                            '</li></strong>');
                    });
                } 
            }
        });
    });


via Chebli Mohamed

lundi 19 avril 2021

why EB throw me Not Found The requested URL was not found on this server?

I upload the laravel project in a zip file, and the configuration software are enter image description here

the estructure of the project is enter image description here

and the endpoint home of laravel response succesfully but if i want to call the api's throw the error Not Found The requested URL was not found on this server. i dind't have this error before but one the day start with this issue.



via Chebli Mohamed

Laravel 5.7 validation works still not

I have already asked question about Laravel 5.7 validation, however it still does not work quite right. the validation is not executed at all when sending the content.

 public function update(Request $request, Player $player)
    {
        if(Auth::check()){
                       
            $playerUpdate = Player::where('id', $player->id)
                                ->update([
                                       'first_name' => $request->input('fist_name'),
                                       'last_name' => $request->input('last_name')
                                ]);
 
            if($playerUpdate){
                return redirect()->route('players.show', ['player'=> $player->id])
                ->with('success' , 'player foo');
            }
            
 
        }
         
        return back()->withInput()->with('errors', 'Foo error');
        
        
        
    }

enter image description here

Thanks in advance



via Chebli Mohamed

Laravel 5.7 validation makes nothing

My function works by itself but the validation is not executed. Does anyone know what I forgot to add?

This is a snippet of my code:

namespace App\Http\Controllers;

use App\Player;
use App\Tournament;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;


    public function store(Request $request)
    {
      $request->validate([
    'first_name' => 'alpha|min:2|max:30',
]);
        
        
        if(Auth::check()){
        
        
        
            $foo = Foo::create([
                'first_name' => $request->input('fist_name'),
                'last_name' => $request->input('last_name'),
            ]);
 
            if($foo){
                return redirect()->route('foo.show', ['foo'=> $foo->id])
                ->with('success' , 'Foo created!');
            }
 
        }
         
        return back()->withInput()->with('errors', 'Error when creating the foo');
    }

Thanks in advance



via Chebli Mohamed

i try to show details from intermediate table through id rapport but the result is empty

public function show(RapportMP $rapportMP){
       $ligne = DB::table('ligne_rapport_m_p_s')
        ->join('rapport_m_p_s', 'ligne_rapport_m_p_s.rapportMP_id', '=', 'rapport_m_p_s.id')
        ->get()
        ->toArray();
        echo'<pre>';
        print_r($ligne) ;
       
       
}

i tried to join but nothing work the result is empty .can somoene be able to explain how can i show details of ligne_rapport through id rapport?




via Chebli Mohamed

laravel manually login works, but after redirection to home page session is not working

i am using laravel version 8, i did single signon, i created EventServiceProvider , in this provide i did login by email data, i can see by Auth::check command login is working, but when it redirected to home page Auth::check doesn't work, can anyone please help me why i am getting this issue ? here i uploaded my code, can anyone please help me how to resolve this issue ?

class EventServiceProvider extends ServiceProvider
{
    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
    ];
    public function boot()
    {
        try{
            Event::listen('Aacotroneo\Saml2\Events\Saml2LoginEvent', function (Saml2LoginEvent $event) {
                $messageId = $event->getSaml2Auth()->getLastMessageId();
                $user = $event->getSaml2User();
                $userData = [
                    'id' => $user->getUserId(),
                    'attributes' => $user->getAttributes(),
                    'assertion' => $user->getRawSamlAssertion()
                ];
                $userInfo = User::where('email',$userData['id'])->first();
                if($userInfo) {
                    //$loggedInUser = Auth::loginUsingId($userInfo->id);
                    $loggedInUser = Auth::login($userInfo);
                }
            });
        } catch (\Throwable $e) {
            echo $e->getMessage(); die;
            report($e);
        }
    }
}


via Chebli Mohamed

How to move laravel projevt from Wamp to lamp centoS 7

I need to move existing laravel 5 project to linux centos 7. I'm going to install lamp, copy source files and database backup and move them to new centos 7 server. Is there anything I should take into account? I moved a lot of pure PHP webpages but never laravel projects. Help appreciated :)



via Chebli Mohamed