vendredi 16 avril 2021

Validation closure not firing?

I want to create a custom validation using closures in Laravel 5.6 as explained in the docs: https://laravel.com/docs/5.6/validation#using-closures

That is my code:

   public function store(Request $request)
    {
        \Illuminate\Support\Facades\Validator::make($request->all(), [
            'trainer' => [
                function ($attribute, $value, $fail) {
                    return $fail($attribute . ' is invalid.');
                },
            ],
        ]);

        if ($validator->fails()) {
          dd($validator->messages());
        }

        dd('NO ERROR??');
   }

Testing it using

$this->post('/my_test_route', []);

Returns

NO ERROR??

Why is this? If I change the code to

   public function store(Request $request)
   {
        Illuminate\Support\Facades\Validator::make($request->all(), [
            'trainer' => 'required',
        ]);

        dd('NO ERROR??');
   }

I get as expected:

Illuminate\Support\MessageBag^ {#2408
  #messages: array:1 [
    "trainer" => array:1 [
      0 => "The trainer field is required."
    ]
  ]
  #format: ":message"
}

What am I missing?



via Chebli Mohamed

Laravel optimize query

In my Database i hv column name type which contains the value Ship and Ship+ and status from 1 to 15 So if the value is Ship and check status value more than 8 and less than 13 if the value is Ship+ and check status value more than 8 and less than 15...So here how can i modify the below query to reduce the exection time.

    $start_time = microtime(true);
    CS::where('status','>', 8)->chunkById(100, function ($items) use (&$post) {
    foreach ($items as $item){
            $item= CS::where('awb', $item->awb)->first();

           
                $this->info('Status ' . $item->delivery);
                $this->info('Country` ' . $item->country_code);
      }
    });
    $end_time = microtime(true);
    $execution_time = ($end_time - $start_time);
    Log::info("total execution time=".'  '.$execution_time);


via Chebli Mohamed

Filter Data with Pivot Table Laravel Eloquent

I want to filter users based on their subscription_status which s stored in a pivot table. I have Three tables users , subscription_packages , subscription_package_user

 $user=User::with(['studentDetails','subscriptionsSatus.courses'])
                        ->withPagination($offset,$perPage)
                        ->get()
                        ->sortBy('first_name')->values();

this code return the response is

 [
{
    "id": 44,
    "first_name": "Abcd Test",
    "last_name": "Test lastname",
    "student_details": null,
    "subscriptions_satus": [
        {
            "id": 1,
            "name": "Gold",
            "price": 3000,
            "user_id": "2"
            "pivot": {
                "user_id": 44,
                "subscription_package_id": 1,
                "subscription_status": "on_free_trial",
                "expires_on": null,
                "id": 9
            },
            "courses": [
                {
                    "id": 18,
                    "title": "English Grammar for Class 3",
                    "price": 400,
                    "strikethrough_price": null,
                    "status": "draft",
                    "user_id": 2,
                    "image": "http://127.0.0.1:8000/courses/1615702915.png",
                    "description": null,
                    "pivot": {
                        "subscription_package_id": 1,
                        "course_id": 18,
                    }
                }
            ]
        }
    ]
}]

i want to return only users who having subscription_status =$filter.

$filter='acive'or 'on_free_trail'

my model is

public function subscriptionsSatus()
{
    return $this->belongsToMany(SubscriptionPackage::class)->withTimestamps()->withPivot('subscription_status','expires_on','id');
}


via Chebli Mohamed

jeudi 15 avril 2021

Laravel - RetaurantLoginController Exist But Error Show Restaurant Login Controller Does Not Exist

enter image description here

Controller does not exist error



via Chebli Mohamed

How should the Laravel query builder output of this query be?

How should the Laravel query builder output of this query be?

 Select T.price_default,price_discounted,price_date,room.hotel_room_id,hotel_lang.title,hotel_lang.description,hotel_lang.slug,hotel_lang.lang_code,hotel.showcase_image,hotel.address,hotel.hotel_id from (Select *,ROW_NUMBER() OVER (
                 PARTITION BY hotel_room_id
                 ORDER BY price_date ASC
           ) AS ROWNUMBER from hotel_room_prices Where price_date BETWEEN '$mindate' AND '$maxdate' ) AS T
                    left join hotel_rooms room on T.hotel_room_id=room.hotel_room_id
                    left join hotels hotel on room.hotel_id=hotel.hotel_id
                    left join hotel_langs hotel_lang on hotel.hotel_id=hotel_lang.hotel_id
                        WHERE T.ROWNUMBER=1 and hotel_lang.lang_code='$locale' and t.price_default >= $min_price and t.price_default <= $max_price and hotel_lang.title ILIKE '%$query%'


via Chebli Mohamed

A non-numeric value encountered while submitting a login form laravel 5.5

Please i created a login form for my forum website but i got this error while trying to submit ** (View: /Applications/XAMPP/xamppfiles/htdocs/laravel-forum-master-3/resources/views/auth/login.blade.php)** and also if i didn't check the remember me botton it will give me a reply of invalid credentials

below is my code in login.blade.php

<form action="" method="post" id="login">
    
    @if(!empty(app('request')->input('redirectTo')))
    <input type="hidden" name="redirectTo" value="" />
    @endif
    <div class="panel">
        <div class="inner">
            <div class="content">
                <h2 class="login-title">Login</h2>
                <fieldset class="fields1">
                    <dl>
                        <dt><label for="email">Email address:</label></dt>
                        <dd>
                            <input type="text" name="email" id="email" size="25" value="" class="inputbox autowidth" />
                            @if ($errors->has('email'))
                            <br /><span class="error"></span>
                            @endif
                        </dd>
                    </dl>
                    <dl>
                        <dt><label for="password">Password:</label></dt>
                        <dd>
                            <input type="password" id="password" name="password" size="25" class="inputbox autowidth" autocomplete="off" />
                            @if ($errors->has('password'))
                            <br /><span class="error"></span>
                            @endif
                        </dd>
                        <dd><a href="">I forgot my password</a></dd>
                    </dl>
                    <dl>
                        <dd><label for="autologin"><input type="checkbox" name="remember" id="autologin"   /> Remember me</label></dd>
                    </dl>
                    <dl>
                        <dt>&nbsp;</dt>
                        <dd><input type="submit" name="login" value="Login" class="button1" /></dd>
                    </dl>
                </fieldset>
            </div>
        </div>
    </div>
    <div class="panel">
        <div class="inner">
            <div class="content">
                <h3>Register</h3>
                <p>In order to login you must be registered. Registering takes only a few moments but gives you increased capabilities.</p>
                <hr class="dashed" />
                <p><a href="" class="button2">Register</a></p>
            </div>
        </div>
    </div>
</form>


via Chebli Mohamed

Is there any php function to find any url title or description?

I am new in data scraping, i am working on url to title scraping, actually i want to write a funtion that take a url/link as request and in return i get <title> </title> , og:title , og:description etc. all meta property

i am trying this funtion for scrape only title

/**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @throws ValidationException
     */
    public function getTitle(Request $request)
    {
        $this->validate($request, [
            'link' => 'required',
        ]);

        $link = $request->input('link');

        $str = @file_get_contents($link);
        if(strlen($str)>0){
            $str = trim(preg_replace('/\s+/', ' ', $str));
            preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title);
            $result = $title[1];
        }

        return Response::json([
            'message' => 'Get title',
            'data'    => $result,
        ], \Symfony\Component\HttpFoundation\Response::HTTP_OK);
    }

route

Route::post('request-title', 'BuyShipRequestController@getTitle');

Example what i request in input field:

Amazon-url

and what i want to my reutrn response

<title>Amazon.com: Seagate Portable 2TB External Hard Drive Portable HDD – USB 3.0 for PC, Mac, PS4, &amp; Xbox - 1-Year Rescue Service (STGX2000400): Computers &amp; Accessories</title>

and

<meta name="description"/> , <meta name="title"/>, <meta name="keywords" /> , link 

in return response i want only those meta properties content or value



via Chebli Mohamed