How to populate a DropDown in Laravel 5 - php

I need to get the id and Name about Costumers to DropDown in Laravel5 , in Laravel4 I use the following code.
public function create()
{
$data = ['groups' => Cliente::lists('name', 'id')];
return view('projects.create')->with('data',$data);
}
After , In the View I show with the following
{{ Form::select('group', $groups) }}
But in the controller Client I have this
<?php namespace App\Http\Controllers;
use App\Project;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
class ClienteController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$projects = Cliente::all();
return $projects;
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
}
When I execute the view create , I can see the following error
Class 'App\Http\Controllers\Cliente' not found
Finally I use this but I need to show the name and id about client in the view not only the id, any solutions for this ?
public function create()
{
$data = ['Selecciona el ID del ususario' => \DB::table('clientes')->lists('id','id')];
return view('projects.create')->with('groups',$data);
Then , the view.
{!!Form::select('user_id', $groups) !!}

Related

Laravel Mail Class Swift_Message does not exist

I'm trying to send email in laravel 5.5. I did everything as in https://laravel.com/docs/5.5/mail, but I get this error:
Class Swift_Message does not exist
I created mailable using
php artisan make:mail OrderShipped
and edited the mailable
public function build()
{
return $this->from('example#example.com')
->view('emails.orders.shipped');
}
anc created nearly empty view
<div>Hi </div>
and sending like this
<?php
namespace App\Http\Controllers;
use App\Order;
use App\Mail\OrderShipped;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
class OrderController extends Controller
{
/**
* Ship the given order.
*
* #param Request $request
* #param int $orderId
* #return Response
*/
public function ship(Request $request, $orderId)
{
$order = Order::findOrFail($orderId);
// Ship order...
Mail::to($request->user())->send(new OrderShipped($order));
}
}

Get model with hasMany is not working in Laravel

I try to print on my view all the atleti associated with a squadra x but the method $squadra->atleti() always returns me ant empty array.
Have you got some ideas to get this working?
Down below you can find my code and my db structure.
Squadra is team in english. One team can be associated to many atleti (it stands for players).
I think there is some problems with the foreign key.
Thank you for your help.
<?php
//Squadra Model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Squadra extends Model
{
protected $connection = 'custom_mysql';
protected $table = 'squadra';
/**
* Get the Allenatore for the Squadra.
*/
public function allenatore()
{
return $this->belongsTo('App\Models\Allenatore');
}
/**
* Get the Atleta for the Squadra.
*/
public function atleti()
{
return $this->hasMany('App\Models\Atleta');
}
}
<?php
//Atleta Model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Atleta extends Model
{
protected $table = 'atleta';
protected $connection = 'custom_mysql';
/**
* Get the Atleta for the Squadra.
*/
public function squadra() {
return $this->belongsTo( 'App\Models\Squadra' );
}
}
<?php
//Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class AtletaController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function getSquadra($id)
{
$squadra = Squadra::find($id);
return view('custom.squadra.dettaglio', compact('squadra'));
}
//View
#foreach( $squadra->atleti() as $atleta )
<tr class="success">
<td>{{ $atleta->id }}</td>
<td>{{ $atleta->nome}}</td>
<td>{{ $atleta->cognome}}</td>
</tr>
#endforeach
Here it is the output of dd($squadra)
Your are sending the atleti to the view, instead of the squadra. Try to change your controller function to this:
public function index()
{
$squadra = Squadra::firstOrFail(); // You need a paremeter in a route that defines which squadra to show. This will show the first one.
return view('custom.atleta.index', compact('squadra'));
}

Strange error "Routes not defined"

I'm learning Laravel 5.4 and I can't get around this. I've added two routes in my view blade like this
Write post
Then in my route web.php file I have
Route::resource('/backend/blog', 'Backend\BlogController');
In HomeController#index where I loading index page which has the button above, like this
<?php
namespace App\Http\Controllers\Backend;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
class BlogController extends BackendController
{
protected $limit = 5;
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::with('category', 'author')->latest()->paginate($this->limit);
$postCount = Post::count();
return view("backend.blog.index", compact('posts', 'postCount'));
}
...
}
HomeController in Backend dir holds
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Requests;
use Illuminate\Http\Request;
class HomeController extends BackendController
{
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('backend.home');
}
}
backend.home has the a href above which generates the error.. Why this happen?
Full error message
ErrorException in UrlGenerator.php line 304:
Route [backend.blog.create] not defined. (View: /var/www/blog/resources/views/backend/home.blade.php)
Route::resource('/backend/blog', 'Backend\BlogController', [
'names' => [
'create' => 'backend.blog.create'
]
]);
route() helper make named route
routing
Route::POST("/backend/blog/create", "Backend\BlogController#create")->name("backend.blog.create");
in view
{{ route('backend.blog.create') }}

Laravel controller parameter's purpose?

namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Show the profile for the given user.
*
* #param int $id
* #return Response
*/
public function showProfile($id)
{
return view('user.profile', ['user' => User::findOrFail($id)]);
}
}
Why do we put parameters on laravel controllers when we can use "Input::get(id)"?
Can anyone give a sample on a situation where I need to use parameters on controller?
In your condition when you are using Input::get('id'), the request to show the profile will be something like
yourdomain.com/profile?id=2
But if you want to show the profile of a user based on something like this one,
yourdomain.com/profile/2
or
yourdomain.com/profile/john
you need a controller parameter.
In the seconds cases you can simply then, bind your parameter to the User model.
public function showProfile(User $user)
{
return view('user.profile', ['user' => $user);
}

How to send data/variable from one method to another in the same controller in laravel 5?

I have 2 functions/methods in the same controller in Laravel like this..
<?php namespace App\Http\Controllers;
use App\Http\Requests;
//use App\Http\Requests\CreatRole;
use App\Http\Controllers\Controller;
use Input;
use Redirect;
use Illuminate\Http\Request;
use App\UserRole;
class UserController extends Controller {
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function role()
{
return view('user.role');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function roleProcess(Requests\CreatRole $request)
{
$input = Input::all();
$role = new UserRole;
$role->role_name = Input::get('role-name');
$role->role_number = Input::get('role-number');
$role->save();
//redirect to add page;
return Redirect::to('/home/role/add')->with('message','Role is added');
}
}
And My routes.php have route as
Route::get('/home/role/add','UserController#role');
What I am trying to do is adding and listing the role can be done from same view i.e on the same web page. I want to show the success message when the role is added. I think to do this we have to passes the success message from roleProcess to role but I don't know how to pass the variables or data from one method to another in laravel.
If I did not misunderstand your question.
you want the word "Role is added" to be shown in "user.role" view after the form submitted.
try to put following code inside "user.role" view
#if(Session::has('message'))
{{ Session::get('message') }}
#endif

Categories