lundi 16 novembre 2020

Relation query undefined method

In my setting model class i have this

public function user()
    {
        return $this->hasMany('App\User');
    }

and in my user model class i have this

 public function settings()
    {
        return $this->belongsTo('App\settings', 'id');
    }

I tried both these queries, trying to get the user information but failed.

$data = DB::table('settings')
                        ->where('id', '=', $id)
                        ->get()
                        ->toArray();

and

$table = \App\settings::where('id', '=', $id);
        $query = $table->user()
                       ->get()
                       ->toArray();

I'm getting this error

Call to undefined method Illuminate\Database\Eloquent\Builder::user()

How do you do relation query? Sorry new to laravel here.



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

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.



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

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.



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

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


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

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.



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

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!



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

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);


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