How to send sms in laravel 5.8 - laravel-5.8

Look at my codes
web.php
Route::get('/mobileServices',['as' => 'mobileServices'], function (){
Alert::message('Welcome back!');
});
blade.php
<tr>
<td>Mobile Services </td>
</tr>
I tried but I et this error.
Function () does not exist

Try naming your route like this...
Route::get('/mobileServices', function (){
Alert::message('Welcome back!');
})->name('mobileServices');
https://laravel.com/docs/5.8/routing#named-routes

Related

I try to show variable from the controller to blade in laravel but the result is "Undefined Variable"

I try to show variable from the controller to blade in laravel but the result is "Undefined Variable $bedrijven , i tried to not use as bedrijf but with no avail
enter image description here"
The controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Bedrijf;
class Bedrijven extends Controller
{
//
function viewLoad()
{
return Bedrijf::all();
return view ('list',['Bedrijven'=>$data]);
}
}
//
{
//
function account (Request $req)
{
return $req->input();
}
}
The blade file
<html>
<table>
<tr>
<td>Id</td>
<td>bedrijven_id</td>
<td>Body</td>
</tr>
#foreach($bedrijven as $bedrijf)
<tr>
<td>{{$bedrijf['id']}}</td>
<td>{{$bedrijf['bedrijven_id']}}</td>
<td>{{$bedrijf['body']}}</td>
</tr>
#endforeach
</table>
</body>
</html>
</div>
</body>
</html>
the route
Route::get('list', [Bedrijven::class, 'show']);
in your controller there is not a $data variable that's why it shows a error
to fix it try this:
function viewLoad()
{
$data = Bedrijf::all();
return view ('list',['Bedrijven'=>$data]);
}
instead of :
function viewLoad()
{
return Bedrijf::all();
return view ('list',['Bedrijven'=>$data]);
}
Minor spelling mistake and the methods controller does not match the route definition. It is quite easy to use compact, to create the array for the view data.
public function show() {
$bedrijven = Bedrijf::all();
return view ('list', compact('bedrijven'));
}
Now the blade code will match the compacted data.
#foreach($bedrijven as $bedrijf)

linking in the laravel join function

I get data from two different tables with Join function. But the link code for the edit page does not work.
index.blade.php file
#foreach($data as $row)
tr>
<td>{{$row->customer_name}}</td>
<td>{{$row->customer_phone}}</td>
<td>{{$row->product_name}}</td>
<td>{{$row->sale_date}}</td>
<td>{{$row->delivery_date}}</td>
<td>
<a href="{{route('orders.create'),[$row['order_id']]}}">
<button class="btn btn-info btn-sm fa fa-edit"></button></a>
</td>
</tr>
#endforeach
web.php file
Route::group(['namespace'=>'orders', 'prefix'=>'orders', 'as'=>'orders.'], function (){
Route::get('/', 'indexController#index')->name('index');
Route::get('/create', 'indexController#create')->name('create');
Route::post('/create/', 'indexController#store')->name('create.post');
Route::get('/detail/{id}', 'indexController#show')->name('detail');
Route::get('/edit/{id}', 'indexController#edit')->name('edit');
Route::post('/edit/{id}', 'indexController#update')->name('edit.post');
});
indexController.php file
public function index()
{
$data = DB::table('orders')
->select('orders.customer_name', 'orders.customer_phone', 'products.product_name', 'orders.sale_date','orders.delivery_date')
->join('products', 'orders.product_id', '=', 'products.product_id')
->get();
return view('orders.index', compact('data'));
}
ErrorException (E_ERROR)
Cannot use object of type stdClass as array (View: D:\xampp\htdocs\personality\resources\views\orders\index.blade.php)
You have an stdClass object, you can't get parameters from it using array syntax.
$object->parameter; // object
$array['element']; // array
So instead of this:
$row['order_id']
Can you try this:
$row->order_id
Pass the ID of a model as a second argument to route and access the attribute of a model (object) with the arrow operator
<a href="{{ route('orders.create', $row->order_id) }}">
Hope this helps

how to display search result in laravel

i can not show search result using laravel. below is the code for route, controller and views:
route:
Route::resource('search', 'SearchController');
controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Order;
use Redirect;
use Auth;
use DB;
class SearchController extends Controller
{
public function index(Request $request)
{
$order = Order::all();
return view(search.index', compact(search));
}
public function search(Request $request)
{
$search = $request->search;
$order = DB::table('order')
->where('custName','like',"%".$search."%");
return view('search.index',compact('search'));
}
}
view
<form action="search" method="GET">
<input type="text" name="custName" class="form-control" <br>
<button type="submit" class="btn btn-primary">SEARCH</button>
</div>
</div>
<table class="table table-bordered" width="500">
<tr>
<td><font color="white">RESULT :</font></td>
</tr>
#foreach($order as $order)
<tr>
<td>{{ $order->custName }}</td>
<td>{{ $order->orderStatus }}</td>
</tr>
#endforeach
</table>
directory location
c:/xampp/htdocs/web/resources/views/search/index.blade.php
show error
Undefined variable: order (View: c:\xampp\htdocs\web\resources\views\search\index.blade.php)
above is my code to search record in the database. but the result always undefined variable.
how to display the searched result using code above. thank you
In your Controller search method, assign search result to $orders
$orders = DB::table('order')
->where('custName','like',"%".$search."%");
then send it to the view:
return view('search.index',compact('orders'));
and in the view do this:
#foreach($orders as $order)
<tr>
<td>{{ $order->custName }}</td>
<td>{{ $order->orderStatus }}</td>
</tr>
#endforeach
It will solve your problem.
There are some issues with the code you have shared, I try to tell the ones I see.
there is a misspelling in Route::resource where instead of search you coded seearch
as for search method it is not used anywhere by the route you provided. You can see more details on Route::resource for methods and views at this question
now considering that you are loading index.blade.php from the public function index method, the usage of compact is troubled. At this page, you can see how compact and with are different and how to use it. The main problem here is that you are not actually passing the order to your view at all.
The last but not least, it is good practice to name items having a collection of values plurally. For example here the order should change to orders. This would prevent the misuse of it in foreach. In your foreach, the name of the iterating array and the temp variable keeping the data should be different, or otherwise, they would have conflict. Here you have 2 variables called order in the same scope.
for instance i have records :
a
aa
aaa
b
c
d
e
the code show all record no1 until no7.
but now its solved i fixed the code in my controller in index() section as below
public function index()
{
return view('search.index', compact('order'));
}
public function search(Request $request)
{
$search = $request->search;
$order = DB::table('order')->where('custName', 'like', "%".$search."%")->get();
return view('search.result', compact('order'));
}

Problems with undefined variable in Laravel

I keep getting Undefined variable: cars when loading the page in Laravel.
I have the following code:
web.php
Route::get('/cars', function () {
$cars= Car::select('brand')->get();
return view('test.cars')->with(compact($cars));
});
If I put dd(cars); before return view the collection is outputted.
In cars.blade.php I have the following code:
<select name="car">
#foreach($cars as $car)
<option>{{ $car->brand}}</option>
#endforeach
</select>
and I get Undefined variable: cars, I also removed the dropdown from blade template and I tried {{ dd($cars) }} but get the same error.
Try the below code :
Route::get('/cars', function () {
$cars= App\Car::pluck('brand');
return view('test.cars',['cars'=>$cars]);
});
OR
Route::get('/cars', function () {
$cars= App\student::pluck('first_name');
return view('test.cars', compact('cars'));
});
Note: Car model is present in App folder here.
The correct syntax for passing variables to views is either:
view('test.cars', ['cars' => $cars])
or
view('test.cars')->with('cars', $cars)
the "with" function requires a name to be passed first, followed by the variable.

Named route not working with url

I have a route that works perfectly
Route::get('downloadsPage', function()
{
$downloads = Download::get();
return View::make('index', array('downloads' => $downloads));
});
Route::get('/buy/{id}', function($id)
{
$download = Download::find($id);
return View::make('buy', array('download' => $download));
});
with a
return View::make('downloadsPage');
that is located in my controller.
public function afterSignin() {
if (Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password')))) {
return Redirect::intended('downloadsPage')->with('message', 'Thanks for signing in');
}
The problem is I also have a nav view file for a link for the same route
<li>Store</li>
and I get the error
http://postimg.org/image/bhxezsh3n/
The error is coming from not correctly setting the route in the view I think.
I also have a foreach loop in the main view that uses the parameters passed in the routes.
#foreach ($downloads as $download)
<tr>
<td>{{ $download->name }}</td>
<td>£{{ round($download->price/100) }}</td>
<td>Buy</td>
</tr>
#endforeach
I am using named routes and am obviously missing something that is probably a simple fix.
I tried re-writing the routes and adding the respective controller functions with get and post and passing parameters but that did not work.
That's not a named route, a named route would be:
Route::get('downloadsPage', ['as' => 'downloadsPage', function()
{
$downloads = Download::get();
return View::make('index', array('downloads' => $downloads));
}]);
Notice the 'as' => 'route.name', that is what defines the name of the route.
Read more in the docs.

Resources