mardi 1 novembre 2016

Laravel 5 how to return to a POST request with validation errors

I have a POST route in Laravel:

Route::post('/quote-summary', 'QuoteController@quoteSummary')->name('quote-summary');

On the view for this route I have a form that submits to another route:

Route::post('/proceed-to-payment', 'QuoteController@proceedToPayment')->name('proceed-to-payment');

Here is my controller method for proceed-to-payment:

public function proceedToPayment(Request $request) {

    if(empty($request->get('tick_statement_of_fact'))) {
        return redirect()->route('quote-summary')->with('tick_statement_of_fact', 'I am so frustrated.');


    } else {
        return view('quote.payment', compact('date_cover_required', 'expiry_date'));
    }

}

I am trying to redirect back to quote-summary but as it was a POST I am getting a MethodNotAllowedHttpException as the redirect seems to be doing a GET.

Any ideas on how I can return to my POST route with some validation error messages?



via Chebli Mohamed

Laravel Product tag Multiple table relationship

I have three Table

products

id | name | category_id | sub_category_id



product_tags

 id | tag_id| product_id 


tags

 id | name

Now I have only sub_category_id like 3.

now I want those tag names which exist in product_tags table and products category_id match with my category_id which come from sub_category_id like 3

I cant understand How to implement it.I am tying to use with,has,whereHas but nothing work



via Chebli Mohamed

Laravel 5.0 Query Builder - Where with multiple parameters

I've a Laravel 5.0 project, that I need to implement the following logic

$var1= 'A';
$var2= 'B';
$var3= 50;
$data = DataModel::select('attr1','attr2')->where(function($q){
    $q->where('attr3','like','%'.$var1.'%');
    $q->where('attr4','like',$var2.'%');
    $q->where('attr5','=',$var3);
})->get();

The problem is for the "Where" function $var1, var2 and $var3 are undefined variables.

My Question is, How can I pass multiple parameters to the where function?



via Chebli Mohamed

insert dynamic data in laravel-mongodb

i have json data from API like this

nama_lembaga: "Balai Besar Penelitian dan Pengembangan Pascapanen",
lembaga_induk: "Kementerian Pertanian",
kategori_lembaga: "Lembaga Pemerintah Kementerian (LPK)",
bentuk_lembaga: "Lembaga Tunggal",
fokus_bidang: "Pangan dan Pertanian",
tahun_penetapan: "2015",
tahun_daftar: "2014",

I need to store it in mongodb periodically using laravel 5.3 with this library laravel-mongodb I think the easy way is using create() method

$json = file_get_contents("API-address");
$obj = json_decode($json);
   foreach ($obj as $document) {
       puiModel::create((array)$document);
   }
}

but i get MassAssignmentException error which indicate i should declare the $fillable in model.

The problem is the indexes may be increased or reduced by the API owner. which means, i can not declare the $fillable value statically. How can i bypass the $fillable. ?



via Chebli Mohamed

ErrorException in HomeController.php line 27: Trying to get property of non-object

I'm on Laravel 5.3 and I'm using view and I intend to share a variable globally to all of my views and this global variable contents the user's info who is currently logged in.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use View;

//use models
use App\profile;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */

    public function __construct()
    {
        $this->middleware('auth');

        View::share('user_info',profile::where('username',Auth::user()->username)->get());

    }
    public function index()
    {
        $current_page = 'home';
        return view('pages.home');
    }
}

but unfortunately it gives me this error

ErrorException in HomeController.php line 27: Trying to get property of non-object

and the line 27 is this line

View::share('user_info',profile::where('username',Auth::user()->username)->first());

it seem's like the problem is 'Auth::user()->username' because If I manually specify the username, it gives me the collection I need.

Any ideas, help please?



via Chebli Mohamed

Laravel 5.3 Join table to Auth user class

i'm working on a CRM that should work with multi languages

my users table looks like this:

Users

id
first_name
last_name
email
password
**language_id**

Languages

id
name
short_name //en,fr,ru etc

i'm trying to get the user's language short name when the user is login i tried this:

Auth::user()->language()->short_name; and also this Auth::user()->language->short_name;

and it's not working, i add this to the User model:

public function language()
{
        return $this->belongsTo('App\Language');
}

and this part to the Language model:

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

what am i doing wrong?



via Chebli Mohamed

laravel 5 - pass php varaible to jquery or ajax or javascript

I search many in site but i could not find the answer so i post my question. I think for my question has exist easy asnwer but It's first time i use javascript , jquery and ajax and i don't know what to do :(! I use Laravel 5 I have a blade php Page and in this file i have a while that create many < div >s like this chart.blade.php:

<?php $id=0; ?>
@foreach($results as $row)
...

<?php 
 $i++; 
 $allGrade = findData($row);
 echo "<div id=\"container$id\" style=\"...\"></div>";
?>
...
@endforeach

this file create all of my divs with id container1, container2, container3, ... in some case create 100 divs , in some case 1200 divs and it's not constant!

in these divs i want to use a Bar charts , and i use http://highcharts.com/ . One of my question is what is the best way for doing this work? the top of this blade i use this java script function for highchart.js

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>
<script type="text/javascript" src="http://ift.tt/Sw98cH"></script>
<style type="text/css">
    $ {
        demo.css
    }
</style>
<script type="text/javascript">
    $(function () {
        for (var i = 0; i < 5000; i++) {
          // Some Alert ****test here ****
            //I put some alert work correctly




            $('#container' + i).highcharts({
                chart: {
                    type: 'bar'
                }
                , title: {
                    text: 'Historic World Population by Region'
                }
                , subtitle: {
                    text: 'Source: <a href="http://ift.tt/1hiu6or">Wikipedia.org</a>'
                }
                , xAxis: {
                    categories: ['Africa', 'America', 'Asia', 'Europe', 'Oceania']
                    , title: {
                        text: null
                    }
                }
                , yAxis: {
                    min: 0
                    , title: {
                        text: 'Population (millions)'
                        , align: 'high'
                    }
                    , labels: {
                        overflow: 'justify'
                    }
                }
                , tooltip: {
                    valueSuffix: ' millions'
                }
                , plotOptions: {
                    bar: {
                        dataLabels: {
                            enabled: true
                        }
                    }
                }
                , legend: {
                    layout: 'vertical'
                    , align: 'right'
                    , verticalAlign: 'top'
                    , x: -40
                    , y: 80
                    , floating: true
                    , borderWidth: 1
                    , backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF')
                    , shadow: true
                }
                , credits: {
                    enabled: false
                }
                , series: [{
                    name: 'Year 1800'
                    , data: [107, 31, 635, 203, 2]
    }, {
                    name: 'Year 1900'
                    , data: [133, 156, 947, 408, 6]
    }, {
                    name: 'Year 2012'
                    , data: [JSON.parse('<?php echo json_encode($allGrades[1]); ?>')]
    }]
            });
        }
    });
</script>
</head>

<body>
<div id="container4" style="min-width: 310px; max-width: 800px; height: 400px; margin: 0 auto"></div>
<div id="container2"></div>
<div id="container3"></div>
<div id="container1"></div> 
. 
.
.

<script src="http://ift.tt/TQup4m"></script>
<script src="http://ift.tt/1rNirHL"></script>
</body>

</html>

The value of allGrade[1] is 20 , and when I use this alert test in Alert test section as below

   var d = <?php echo json_encode($allGrades); ?>; //Don't forget the extra semicolon!
        //alert(d[1]);

  j++;
  var newJ = JSON.parse('<?php echo json_encode($allGrades); ?>');
  var c1 = newJ[1];
  var c2 =newJ[2];
  alert(c1);
  alert(JSON.parse('<?php echo json_encode($allGrades[1]); ?>'));

alerts work and shows data 20 in alert box!

I Expect for Data Year 2012 when i put c1 or put JSON.parse('') the chart data be 20 , But when i put these two items don't work!



via Chebli Mohamed