jeudi 1 novembre 2018

Laravel js validation on Send button not in Save button in the form

I'm using laravel 5.6 and using JS Validator for client and server side validation. My problem is validation should only work when the form is submitted(Send). When form is saved, validation should not exist. Please help me to fix this issue.

My View as follows

    <div class="form-group row">
        <div class="col-md-11" align="right"><input type="submit" name="submit" class="btn btn-primary" value="Save" /></div>
        <div class="col-md-1" align="right"><input type="submit" name="submit" class="btn btn-success" value="Send" /></div>
    </div>

JS Validation

{!! JsValidator::formRequest('App\Http\Requests\FormValidation') !!}



via Chebli Mohamed

How get data in method from others tables?

enter image description here

This is my simple db schema. Now in my group table i have 'group_A' and 'group_B'. In questions table i have 10 questions 5 for group_A and 5 for group_B. Users table is one user with ID = 1. What i try to do is get data like this:

SELECT answer
FROM answers
JOIN questions q ON (q.id = answer.question_id)
JOIN group g ON (g.id = q.group_id)
WHERE user_id = 1 AND g = 'group_A'

I have model in users class and i would like create method to get answers depend from group:

public function getAnswers($group) {
        return $this->hasMany('App\Answers', 'question_id', 'id');
    }

How can i get this in that method ? Should i create method group in questions class ?



via Chebli Mohamed

How to modify request text in laravel

I can't think of a good title I'm so sorry but I need help.

I have a field that is added by jquery depending on how many questions are present in the database, so for example I have 3 questions in the db i will have 3 answer fields too, it will look like this:

answer_1 answer_2 answer_3

since i am not sure how many questions there can be, i need to loop the $request->answer_1, 2 and 3 in the controller to store the answers. but i'm not sure how to do it. I have tried:

for($i = 0; $i < total.count.of.answers; i++){
    $x = $i + 1;
    $answer = new Answer;
    $answer->answer = $request->answer_{$x};
    $answer->save();
}

but not working. How do I achieve this?

(note i can already send the answers to the backend using jquery, problem is only how i can pull it in the controller)



via Chebli Mohamed

How to highlight current active menu in Laravel 5.6 app?

I am using following bootstrap menu items with My laravel application,

<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<!------ Include the above in your HEAD tag ---------->

<div class="container">
    <div class="row">
        <div class="col-sm-3 col-md-3">
            <div class="panel-group" id="accordion">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4 class="panel-title">
                            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne"><span class="glyphicon glyphicon-folder-close">
                            </span> Content Management</a>
                        </h4>
                    </div>
                    <div id="collapseOne" class="panel-collapse collapse in">
                        <div class="panel-body">
                            <table class="table">
                                <tr>
                                    <td>
                                        <span class="glyphicon glyphicon-pencil text-primary"></span><a href="">Category</a>
                                    </td>
                                </tr>

                                <tr>
                                    <td>
                                        <span class="glyphicon glyphicon-pencil text-primary"></span><a href="">Brand</a>
                                    </td>
                                </tr>

                                <tr>
                                    <td>
                                        <span class="glyphicon glyphicon-pencil text-primary"></span><a href="">Model</a>
                                    </td>
                                </tr>

                                <tr>
                                    <td>
                                        <span class="glyphicon glyphicon-flash text-success"></span><a href="">Province</a>
                                    </td>
                                </tr>

                            </table>
                        </div>
                    </div>
                </div>
</div>

and using this menu bar with @include('menubar) with other blade files. now I need highlight current menu item when I visit each pages. how can I do this?



via Chebli Mohamed

How can I make a way to return error message of record not available?

I am trying to fetch user cars, which is working fine. But I want to add if-statement which should return a error message no record found. I am trying as like below

$UserCars = User::with('cars')->findOrFail(request()->user()->id);
if ($UserCars !== null) {
    $Result = $UserCars->cars;
}else{
    $Result = response()->json(['data' => 'Resource not found'], 404);
}

return new CarResource($Result);

The problem is it is working when if-statement true (means when found record in database) but in case of false condition it returns html page with following message

Sorry, the page you are looking for could not be found.

CarResource is API resource which I am using for API's.

Can someone kindly guide me about this I would appreciate. Thank you



via Chebli Mohamed

How to improve laravel routing?

I am working on some ecommerce project in laravel framework where my category route is below:-

Route::get('/category/{catname}', 'SearchController@searchCatProducts');

When i access website its working like this:-

www.example.com/category/men

But our client wants it should like this :-

www.example.com/men

if i create Roue like:-

Route::get('/{catname}', 'SearchController@searchCatProducts');
and other routes will also reflect like aboutus, contactus,
 Route::get('/aboutus', 'CMScontroller@aboutus');

Can anyone help me. Thanks in advance.



via Chebli Mohamed

Why append method not working with empty array in VueJS

I have a component with empty data array

data: () => ({
    selectedResource : [],
}),

but when i try to add some item to this array i got an error

    selectResource(resource){
        console.log(this.selectedResource);
        this.selectedResource.append(resource)
    }

this.selectedResource.append is not a function



via Chebli Mohamed