lundi 16 novembre 2020

Store data into database using request json string in laravel

I am having a table name Student ( id , name , division ). To store data into this table I am sending json string as a request to the api in laravel.

Request json string is,

{
"name":"abc",
"division":"a",
"city":"xyz"
}

Controller Code

 public function registerStudent(Request $request){

    $requestData = $request->json()->all(); 
    $studentModel = Student::create($requestData);

 }

Student Model

class Student extends Model
{
    protected $fillable = [
        'id', 'name','division'
    ];

   
}

When i execute this code , i get the following error,

Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'city' in 'field list' (SQL: insert into `Student`... 

now my question here is, in what way I can store the data into database from json request with having extra keys into json object/array.



via Chebli Mohamed

Laravel route not found when route exists

When I try to access my laravel site I get this error in the console.

Laravel development server started: <http://127.0.0.1:8000>
[Mon Nov 16 10:39:15 2020] PHP Fatal error:  Uncaught InvalidArgumentException: Route [home] not defined. in /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:389
Stack trace:
#0 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(822): Illuminate\Routing\UrlGenerator->route('home', Array, true)
#1 /Users/threeaccents/code/src/gitlab.com/few/bodylove/storage/framework/views/e071ac62e490c49233841ae8b6b3906075bc0187.php(6): route('home')
#2 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(43): include('/Users/threeacc...')
#3 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(59): Illuminate\View\Engines\PhpEngine->evaluatePath('/Users/threeacc...', Array)
#4 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/View/View.php(142): Illuminate\Vi in /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php on line 389
[Mon Nov 16 10:39:15 2020] PHP Fatal error:  Uncaught InvalidArgumentException: Route [home] not defined. in /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:389
Stack trace:
#0 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(822): Illuminate\Routing\UrlGenerator->route('home', Array, true)
#1 /Users/threeaccents/code/src/gitlab.com/few/bodylove/storage/framework/views/e071ac62e490c49233841ae8b6b3906075bc0187.php(6): route('home')
#2 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(43): include('/Users/threeacc...')
#3 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(59): Illuminate\View\Engines\PhpEngine->evaluatePath('/Users/threeacc...', Array)
#4 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/View/View.php(142): Illuminate\Vi in /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php on line 389

But route home is clearly defined. I even set it at the top of my route file to make sure nothing else was overriding it.

routes/web.php
<?php

// New Home Page for App
Route::get('/', 'Web\HomeController@index')->name('home');

...

with php artisan route:list

+--------+----------------------------------------+--------------------------------------------------------------------------------------------+--------------------------------------------+------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Domain | Method                                 | URI                                                                                        | Name                                       | Action                                                                                         | Middleware                                                                                                                                                                        |
+--------+----------------------------------------+--------------------------------------------------------------------------------------------+--------------------------------------------+------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|        | GET|HEAD                               | .well-known/apple-developer-merchantid-domain-association                                  |                                            | App\Http\Controllers\Web\ApplePayDomainVerificationController                                  | web                                                                                                                                                                               |
|        | GET|HEAD                               | /                                                                                          | home                                       | App\Http\Controllers\Web\HomeController@index                                                  | web                                                                                                                                                                               |

Update

I've cleared route cache, deleted vendor folder, re-installed PHP but nothing seems to solve the issue.

I've tried it on my work laptop (same setup) and everything works. It seems to be an issue with my current station but I'm not sure what would be causing it. I'm using PHP 7.2 on a mac os 10.14.4

I also thought maybe it was an overall system issue but if I create a new laravel project everything works as expected so it does seem to be a project specific issue.



via Chebli Mohamed

my project laravel when i click submit in form with errors in input my view refresh multiple time so i can't see message errors

this my view code here i try to do form with validate and div with type of errors:

@if(count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li></li>
            @endforeach
        </ul>
    </div>
@endif
<form action="add" method="POST">
 <!--Securite-->
    Product name <input type="text" value="" class="form-control " name="name" placeholder="enter product">
<br>
    Product Price <input type="text"  class="form-control " value="" name="price" placeholder="enter price">
<br>
<input type="submit" value="Add Product">
</form>
@endsection


via Chebli Mohamed

laravel validation, how to add validation rule on client side?

//html code
<form>
<input type="radio" id="defult" name="price_type" value="default">
<label for="defult">Default Price</label><br>
<input type="radio" id="custom" name="price_type" value="custom">
<label for="custom">Custom Price</label><be>

<input placeholder="Custom Price" class="form-control" name="custom_price">
</form>

$('input[type=radio][name=price_type]').change(function() {
    if (this.value == 'default') {
        //make custom_price optional
    }
    else if (this.value == 'custom') {
        //make custom_price required
    }

});

Actually, I have a radio box for the custom price or default price, if the user selects the custom price then want to make input[name=custom_price] required or if the user selects default price then make input[name=custom_price] optional.



via Chebli Mohamed

Laravel \Illuminate\Filesystem\Filesystem File:files() count only the files that have specific extension e.g jpg png in all subfolders

i can count files in the folder with this :

 \Illuminate\Filesystem\Filesystem\File::files($path)

then i can count all files in all subfolders (with specific storage)

 foreach ($directories as $directory) {

     $path = Storage::disk($this->disk)->path($directory);

     $files = \Illuminate\Filesystem\Filesystem\File::files($path);
            if ($files) {
                $count += count($files);
            }
     }
}

i can count all files in this folder

How can i count only png or jpeg files with minimum overload?

thanks in advanced!



via Chebli Mohamed

get page number in object of laravel

i have object given below and i wanted to pagination in this how can i get

$productListArray = array();
 $productListObject = ((object)[
                "id"=>$productList->id, 
                "title"=>$productList->title,
                "slug"=>$productList->slug,
                'categoryName'=>$categoryName[0]->cat_title,
                'brand'=>$brandName[0]->brandname,
                'minMrp'=>$minMrp,
                'maxMrp' =>$maxMrp,
                'minSellingPrice' => $minSellingPrice,
                'maxSellingPrice' => $maxSellingPrice,
                'rating'=>$productList->rating,
                'rating_count' => $productList->rating_count,
                'image' => $img[0]
            ])->paginate();
       array_push($productListArray, $productListObject);
    }
    return response()->json($productListArray, 200);


via Chebli Mohamed

Search in the related model in Laravel

I have post table like this :

id | post_title | category_id

and this is category table

id | category_title

this is the relation between these two which is inside post model :

  public function category()
    {
        return $this->belongsTo(Category::class, 'category_id', 'id');
    }

I want to get the record of post table where category_title or post_title matches the keyword entered by user.

I'm retrieving data something like:

Post::where(['title'=>$request->title])->with('category')->paginate(10);

but here it is only fetching Post title but i also want it to search it in category title.

Any help is highly appreciated.



via Chebli Mohamed