vendredi 1 avril 2016

Use Pivot Table query in Laravel 5.0

I'm using pivot table in my laravel 5.0 application, this is my database :

Temoignages

Schema::create('temoignages', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('personne_id')->unsigned();
        $table->foreign('personne_id')->references('id')->on('personnes');
        $table->text('temoignage');
        $table->double('note', 3, 1);
        $table->string('etat');
        $table->timestamps();
    });

Types

Schema::create('types', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('nom');
        $table->timestamps();
    });

So I have this pivot table generated :

Schema::create('temoignage_type', function (Blueprint $table) {
        $table->integer('temoignage_id')->unsigned()->index();
        $table->foreign('temoignage_id')->references('id')->on('temoignages')->onDelete('cascade');
        $table->integer('type_id')->unsigned()->index();
        $table->foreign('type_id')->references('id')->on('types')->onDelete('cascade');
        $table->primary(['temoignage_id', 'type_id']);
    });

These are my models :

Type

class Type extends Model {

    protected $guarded = [];

    public function temoignages()
    {
        return $this->belongsToMany('App\Temoignage');
    }
}

Temoignage

class Temoignage extends Model {

    protected $guarded = [];

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

    public function types()
    {
        return $this->belongsToMany('App\Type');
    }
}

What I want to do, is to get in my edit function of TemoignagesController, all the Types associated. I tried many things like :

$temoignage->types()->get();

But I have an empty collection. Can someone can help or has already solve this problem ?

Thanks you :)

EDIT 1 : I followed this : http://ift.tt/1KOEout



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1M78EIk
via IFTTT

Laravel: get translation from language file containing dot

Is it possible to get translation from language file that contains (.)dot?

For example, The language directory looks like

/lang
      /en
          pagination.php
          phone.history.php

     /es
          pagination.php
          phone.history.php

phone.history.php file contains dot and want to get translation for the key 'name' from phone.history.php file.

Lang::get('phone.history.name')

It doesn't work.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1RRKIeo
via IFTTT

Laravel auth::login in EventListener doesn't persist

I have a Laravel 5.2 app where I'm trying to login a user in an Event Listener. All routes are wrapped in web middleware, I'm using database for the session and I see it creates an entry when the event listener executes, however accessing other pages later behave as if there's no user in session.

Just to test out my code, I added the same listener code in a controller route and that works, it persists. I'm suspecting this has to do with the web middleware not being available to an event listener.

What do I have to do to get the event listener login to persist across the app ?

// EventListener, doesn't persist
public function handle(Saml2LoginEvent $event)
{
    $user = $event->getSaml2User();
    $laravelUser = User::where('mail', $user->getAttributes()['mail'][0])->where('active', 1)->first() ;
    Auth::guard('web')->login($laravelUser);
}

// UserController, persists !
public function home(Request $request){
    $laravelUser = User::where('mail', 'test@mail.com')->where('active', 1)->first() ;
    Auth::guard('web')->login($laravelUser);
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1qakwjh
via IFTTT

Laravel 5 trying to write query to subtract one column from the other and sort by the result

I'm trying to subtract one column from another and sort by the new AS variable. I suspect my 'minus operation' isn't correct in my query. It fails with the message:

Unknown column 'used_vehicles.selling_price' in 'order clause'

My controller:

public function showUsed()
{

$vehicles = DB::table('used_vehicles')
        ->join('dealerships', 'used_vehicles.dealer_id', '=', 'dealerships.id')
        ->join('makes', 'used_vehicles.make_id', '=', 'makes.id')
        ->join('models', 'used_vehicles.model_id', '=', 'models.id')
        ->join('used_vehicles_categories', 'used_vehicles.category_id', '=', 'used_vehicles_categories.id')
        ->select('used_vehicles.vehicle_price - used_vehicles.discount AS used_vehicles.selling_price')
        ->where('used_vehicles.archived', 0)
        ->orderBy('used_vehicles.selling_price', 'ASC')
        ->paginate(30);

    return view('/pages/used/index', ['vehicles' => $vehicles]);

}

Any idea what is wrong?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1RREUld
via IFTTT

Login Form not working Laravel 5.2

I created a login form and a registration form in my eComerace Application but when I submit my login form ( sign in ) it returns nothing ! I have typed username and password correct but my login page is not working.

Routes

Route::get('users/signin', 'UsersController@getSignin');
Route::post('users/signin', 'UsersController@postSignin');

Route::resource('users', 'UsersController');


Route::get('/', 'StoreController@index');
Route::get('store/category/{id}', 'StoreController@getCategory');
Route::get('store/search', 'StoreController@getSearch');

Route::get('/admin', function () {
    return view('welcome');
});
Route::resource('store', 'StoreController');
Route::resource('admin/categories', 'CategoriesController');
Route::resource('admin/products', 'ProductsController');

UsersController

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use App\Http\Requests;
use App\Category;
use App\product;
use Hash;
use View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;

class UsersController extends Controller
{
    public function __construct(){
        parent::__construct();
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getSignin(){

        return View::make('users.signin');
    }

    public function index()
    {
        return Hash::make('ifti');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return View::make('users.newaccount');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
            $user = new User;
            $user->name = Input::get('name');
            $user->email = Input::get('email');
            $user->password = Hash::make(Input::get('password'));
            $user->save();
            return Redirect::to('users/signin')->with('message','Thank you for creating new account.Sign in now');
    }




    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    public function getSignout(){
        Auth::logout();
        return Redirect::to('users/signin')->with('message','Signouted!');
    }

    public function postSignin(){
        $password = Hash::make(Input::get('password'));
        $credentials = array('name' => Input::get('name'), 'password' => $password);

        if(Auth::attempt($credentials)){
            return Redirect::to('http://localhost/ecom/')->with('message','Thanks for signin');
        }
        return Redirect::to('users/signin')->with('message','Was Incorrect DATA!');
    } 


}

Signin View

    {!! Form::open(array('url' => 'users/signin' , 'method' => 'post')) !!} 

                          <div class="form-group">
                            <label for="username">User Name:</label>
                            {!! Form::text('name') !!}
                          </div>

                          <div class="form-group">

                            <label for="username">Password:</label>
                            {!! Form::password('password') !!}
                          </div>

                          <button type="submit" class="btn btn-default">Sign IN</button>
  {!! Form::close() !!}

New Account View

<div class="col-md-8 col-lg-8 col-sm-12 col-xs-12">
                   {!! Form::open(array('url' => 'users/create' , 'method' => 'post')) !!} 

                      <div class="form-group">
                        <label for="username">User Name:</label>
                        <input type="username" class="form-control" name="name" id="name">
                      </div>
                      <div class="form-group">
                        <label for="email">Email:</label>
                        <input type="email" class="form-control" name="email" id="name">
                      </div>
                      <div class="form-group">
                        <label for="password">Password:</label>
                        <input type="password" class="form-control" name="password" id="name">
                      </div>

                      <button type="submit" class="btn btn-default">Create New Account</button>
                   {!! Form::close() !!}

    </div>



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1X3W0up
via IFTTT

Laravel homestead suddenly stopped using 8000 port suffix when submitting forms

I have a form that looks like this:

<form method="POST" action="{{ route('flyers.store') }}" enctype="multipart/form-data" class="col-md-6">
        @include('flyers.form')
    </form>

Throughout the entirety of the project, this worked. It would post to my local development url http://ift.tt/1UIafHC.

Suddenly it's posting to http://ift.tt/1Y45gyH.

I'm not sure what would cause this. Any suggestions?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1UIafHE
via IFTTT

Validate whether multiple fields are unique simultaneously in laravel 5 using request class

This my request class rules.

return [
        'title' => 'required|unique:event_cals,title',
        'eventDate' => 'required|date|after:yesterday',
        'venue' => 'required',
        'time' => 'required',
        'type' => 'required',
        'event_date' => 'unique:event_cals,event_date,NULL,id,venue,{$request->venue},time,{$request->time}'
    ];

There I need to clarify the last rule. It is not working. There I need to check if there any row without same eventDate, venue and time altogether. Simply, There can't be any events with same values for all the eventDate, time and venue.

Below is the db. enter image description here

I need to avoid entering such rows because above I mentioned three fields values are equal. Above request class final rule is my attempt for that. Please can anybody knows to solve that problem



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Y45dTI
via IFTTT