lundi 29 août 2016

Laravel5: Why my model "Media" require table named "media" instead of "medias"?

From the doc of laravel, it claims:

Now, let's look at an example Flight model, which we will use to retrieve and store information from our flights database table

So I wrote:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Media extends Model
{
    //
}

But when I run my application, it gives that error:

QueryException in Connection.php line 729:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'myapp.media' doesn't exist (SQL: select * from `media`)

It requires a table named media instead of medias as document said. So why that errors happened? Do I accidentally open option flag which changed the default name mapping from model to table or something similar?

Any suggestion will be appreciated.


Environment:

  • Laravel Framework version 5.2.43
  • PHP 7.0.8-2+deb.sury.org~xenial+1


via Chebli Mohamed

Laravel 5.1 one form two submit buttons

I am using Laravel 5.1, and I would like to make a form with two submit buttons - Save and Save draft.

But when I post my form I have all the fields except the submit value.

I read that Laravel won't put the submit button value in the POST when the form was sent via ajax, so could you please help me how to do this?

Here are some code how it looks like now:

{!! Form::open(['url' => 'offer/create', 'method' => 'post', 'id' => 'offer-create']) !!}

....
here are my fields
....

{!! Form::submit( 'Save', ['class' => 'btn btn-default', 'name' => 'save']) !!}

{!! Form::submit( 'Save draft', ['class' => 'btn btn-default', 'name' => 'save-draft']) !!}

In my routes.php I have:

Route::controller('offer', 'OfferController');

Thanks in advance



via Chebli Mohamed

Date Not Inserted In Database Using Angular js And Laravel

Here i am using angular js iand trying to insert attendence from form attendance-form.blade.php in table attendance on button click. Now the problem is that as i click on button create there is no insertion of data in table attendance.Below is the code i done

In attendance-form.blade.php

<div ui-view="attendanceForm">
<div class="mdl-grid demo-content" ng-controller="AttendanceFormController">
    <div class="demo-charts mdl-color--white mdl-shadow--2dp mdl-cell mdl-cell--12-col mdl-grid">
        <form>
            <div class="form-group label-static is-empty">
                <h3>Create Attendance</h3>
                <div class="form-group label-static is-empty">
                    <input type="text" name="user_id" class="attendance-form form-control"
                           id="i2" placeholder="user_id" ng-model="attendance.user_id">
                </div>
                <div class="form-group label-static is-empty">
                    <input type="text" name="in_or_out" class="attendance-form form-control"
                           id="i2" placeholder="in_or_out" ng-model="attendance.in_or_out">
                </div>
                <div class="form-group label-static is-empty">
                    <input type="text" name="comment" class="attendance-form form-control"
                           id="i2" placeholder="comment" ng-model="attendance.comment">
                </div>
                <div class="attendance-form form-group label-static is-empty ripple-container">
                    <input type="button" value="Create" class="btn btn-raised btn-primary" ng-click="abc()">
                </div>

            </div>
        </form>
    </div>
</div>

In AttendanceController.js

app.controller('AttendanceFormController', function ($scope, AttendanceService) {
$scope.attendance = {};
$scope.attendance.user_id
$scope.attendance.in_or_out;
$scope.attendance.comment;

$scope.loadAttendance = function () {
    $scope.user = AttendanceService.get({id:$scope.params.id});
}

$scope.abc = function () {

                                attendance = {user_id:$scope.attendance.user_id,
                                in_or_out:$scope.attendance.in_or_out,
                                comment:$scope.attendance.comment,

    }

    AttendanceService.post(attendance).then(function(response){
        alert('Attendance successfully added in the system.'+'\n'+'Check console.log for response.');
        console.log(response.data);
    });

}

});

In AttendanceFactory.js

app.factory('AttendanceRESTClient', function ($resource) {
return $resource('api/attendance/:id', {id: '@id'}, {
    'query': {method: "POST", isArray: false }
});

})

In AttendanceService.js

var AttendanceService = app.service('AttendanceService', function (AttendanceRESTClient) {
return {
    get: function(id) {
        return AttendanceRESTClient.get({id:id.id}).$promise;
    },

    getEdit:function(id){
        return AttendanceRESTClient.getEdit({id:id.id})
    },

    post: function(attendance){
        return AttendanceRESTClient.save(attendance).$promise;
    },

    all: function(){
        return AttendanceRESTClient.query().$promise;
    },

    edit: function(attendance){
        return AttendanceRESTClient.edit(attendance)
    }
}

})



via Chebli Mohamed

How to enableQueryLog Database Laravel after app start.

I want get Query in controller laravel but i must DB::enableQueryLog() in function i using DB::getQueryLog() . I want DB::enableQueryLog() when app start. To be DB::getQueryLog() without DB::enableQueryLog() in controller.

Please help! Thank so much



via Chebli Mohamed

Laravel 5 group by relationship's column

I am using Laravel 5.2. I have 2 models : Vehicle and Brand

Eloquent Relationships are as follows:

Vehicle belongsTo Brand

Brand hasMany Vehicles

I wanted the count of vehicles in each Brand with Brand name after applying a set of filters to the vehicles list.

My code:

        $result = Vehicle::ApplyFilters($conditions)  // this is a scope applied initially
                ->with(['brand' => function($query) {
                    return $query->groupBy('brand_name');
                }])->get();

Unfortunately not getting desired result. It is listing the vehicles and not grouped by brand.

I am a newbie. Any idea to make it work?



via Chebli Mohamed

Append form action with input variable

I have a form whose action is

<form action="/food/vegetable" method="POST" >

My controller method takes vegetable name as parameter

I would like to append the input vegetable name to the action. for example

<form action="/food/vegetable/{vegetableName}" method="POST" >

Is it possible to do this ? or is there a better way ? Please guide.



via Chebli Mohamed

dimanche 28 août 2016

Laravel localization problems 5.2+

This is my middleware.

class BeforeMiddleware{

public function handle($request, Closure $next)
{
    // Perform action

    App:setLocale(LC_ALL,Session::get('locale'));

    return $next($request);
}

If i don't place LC_ALL as first parameter in setLocale i get this error.

"setlocale() expects at least 2 parameters, 1 given"

If i put LC_ALL as the first parameter localization doesn't change. Version of laravel is 5.2+



via Chebli Mohamed