mardi 15 juin 2021

API Based Observer in Laravel

I want to make API based observer in laravel . I am using laravel 5.5. Basically I display data from API. My expectation is If there inserts any new data then laravel application fetch that on that time. Here one application is for provide API and another application is for fetch data. Here database only use api application



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

Laravel 8 Cannot Insert Or Update Data In A Certain Table

I'm new to Laravel, and I have been struggling to insert data into a certain table, hods. The table only has two fields, a unique id, and a foreign key called user_id.

Below is the code for the Hod model:

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Hod extends Model
{
    use HasFactory;

    protected $table = 'hods';

    protected $primaryKey = 'id';

    protected $fillable = 'user_id';
}

Below is the code for the method in my controller where I want the query to take place:

    public function createHOD(Request $request)
    {
        // Validate Form Input
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users,email,except,id',
            'password' => 'required|string|min:8|confirmed',
        ]);

        // If Validation Is Successful
        if (!$validator->fails()) {

            // Create User THIS QUERY WORKS FINE    
            $user = User::create([
                'name' => $request->input('name'),
                'email' => $request->input('email'),
                'password' => Hash::make($request->input('password')),
                'user_type' => "HOD"
            ]);

            // Attaching 'HOD' Role To User
            $user->attachRole('HOD');

            // Adding The User To The HOD Table NOT WORKING
            $hod = new Hod();
            $hod->user_id = $user->id;
            $hod->save();

            // TRIED THIS TOO, ALSO NOT WORKING
            DB::table('hods')->insert(
                ['user_id' => $user->id],
            );

            return response()->json(['success' => 'HOD Successfully Created.']);
        } else {
            // Return Error Messages
            return response()->json(['error' => $validator->errors()->all()]);
        }
    }

I'm making use of AJAX, below is the code that handles that aspect:


            $("#hodSubmit").click(function(e) {
                e.preventDefault();

                var _token = $("input[name=_token]").val();
                var name = $("input[name=hod_name]").val();
                var email = $("input[name=hod_email]").val();
                var password = $("input[name=hod_password]").val();
                var password_confirmation = $("input[name=hod_confirmation]").val();

                $.ajax({
                    url: "",
                    type: 'POST',
                    data: {
                        _token: _token,
                        name: name,
                        email: email,
                        password: password,
                        password_confirmation: password_confirmation
                    },
                    success: function(data) {
                        if ($.isEmptyObject(data.error)) {
                            $("#hodForm")[0].reset();
                            printSuccessMsg("hod");
                        } else {
                            printErrorMsg(data.error, "hod");
                        }
                    }
                });
            });

Please let me know if you require additional code or further explanation. Thanks



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

Transform CURL in Http facade

I have a big problem to transform CURL to Http facade in laravel. So I have the code :

$httpParams = [
    'textData'  =>  $xml->asXML(),
    'xmlFile'   =>  new \CurlFile($params['file']->getPathName())
];
$curlHandle = curl_init('http://url.com');
curl_setopt($curlHandle, CURLOPT_HEADER, false);
curl_setopt($curlHandle, CURLOPT_POST, true);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $httpParams);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$curlResponse = curl_exec($curlHandle);
curl_close($curlHandle);
dump($curlResponse); die();

I have the xml response and status is 200.

Next I using postman (is working just fine), I have response data the xml: enter image description here

When I try to do with Laravel, I have the status 200 but the response is "". I tried multiple ways but still did't work :((

  $http = Http::withHeaders([
    'Content-Type' => 'multipart/form-data;',
  ])->withOptions([
    'debug' => true,
  ]);
  $httpParams = [
    'textData'  =>  $xml->asXML(),
    'xmlFile'   =>  new \CurlFile($params['file']->getPathName())
  ];
  dump($http->send('post', $endpoint, $httpParams)->body());die();

Have you an idea how to modify this code in order to works fine with Http facade also ? Thx in advance.



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

Laravel 5.5 login with google got error Cannot use lexical variable $eventName as a parameter name

Cannot use lexical variable $eventName as a parameter name enter image description here



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

Laravel excel library(Maatwebsite) : How to create a drop down with FromArray Export

I'm using Laravel Excel to export excel with larvel. I need a dropdown for a specific column.

My export file,

use App\MyModel;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;

class MyModelExport implements FromArray, WithHeadings
{
    public function array(): array
    {
        $branches = MyModel::get([
            "title",
        ]);

        $data = [];
        $districts = [
           '1' => 'Dis 1',
           '2' => 'Dis 2',
        ];

        foreach ($items $key => $item) {

            $data[$key]['title'] = $item->title;
            $data[$key]['districts'] = $districts;
        }

        return $data;
    }

    public function headings(): array
    {
        return [
            'Title',
            'Districts'
        ];
    }
}

What I need is to add these $districts as a dropdown inside excel. Is there a way to do it?(Sadly this code only prints all array values in single cell)



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

alternate to first() to check full object in laravel

I use first() to check first text in string but i want to check whole string. what is method to do this?

suppose i have word "hello world" then its only checking "hello" how to check full name like "hello world"

I use get(), plunk(), slice() but it doesn't work for me.

private function getBrandIdFromName($brandName){
  $brand = Brand::query()->where('slug',$brandName)->filter($brandName);
        if($brand){
            return $brand->id;
        }else{
            $newBrand = [
                'name'=>$brandName,
                'is_active'=>"1",
                'meta'=>[
                    'meta_title'=>null,
                    'meta_description'=>null,
                ]
            ];
            $brand = Brand::create($newBrand);
            return $brand->id;
        }
}


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

lundi 14 juin 2021

how to check whole text not only first text in laravel

I use first() to check first text in string but i want to check whole string. what is method to do this?

I use get(), plunk(), slice() but it dosen't work for me. any help kindly appreciate.



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