i pass value from laravel controller to angular and then show data from my database to blade view laravel. i want to try if condition with angular data from angular controller how can i do it??
i try this code:
<td>
#if(#{{pending.step1}}=='Pending')
// here some code///
#endif
</td>
how can i use if condition with laravel blade and angular??
I think you can try this if you need use for angular if condition :
<td ng-if="pending.step1 =='Pending'">
----------- code here -------------------
</td>
OR you can use laravel if condition then you should use this :
<td>
#if(pending.step1 =='Pending')
// here some code///
#endif
</td>
Hope this work for you!
We can use an Angular variable in blade file like this:
Normal use
Angularjs variable = data;
#{{ data }}
Use in condition
Angularjs variable = data;
#if( #data == 0 )
#else
#endif
Related
sorry if the question is kind of newbie. I am new to php and laravel, still trying to learn through tutorial.
I am trying to pass the 'No' in my database to the url, so that the url when I clicked on Daftar, it will show
http://127.0.0.1:8000/search/{No}
Webpage designed
I did try to put it this way in my href tag but did not manage to get the result I want
here is my code
search.blade.php
#if(isset($namelist))
<table class="table table-hover">
<thread>
<tr>
<th>No</th>
<th>Nama</th>
<th>ID</th>
<th>Tindakan</th>
</tr>
</thread>
<tbody>
#if(count($namelist) > 0)
#foreach($namelist as $nama)
<tr>
<td>{{ $nama->No }}</td>
<td>{{ $nama->Name }}</td>
<td>{{ $nama->ID }}</td>
<td>
<a href="search/".$nama[No]>DAFTAR</a>
</td>
</tr>
#endforeach
#else
<tr><td>Tiada rekod ditemui, sila daftar secara manual di kaunter pendaftaran</td></tr>
#endif
</tbody>
</table>
#endif
</div>
</div>
</div>
</body>
</html>
searchController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class searchController extends Controller
{
function search(request $request){
if(isset($_GET['query'])){
$search_text = $_GET['query'];
$namelist = DB::table('namelist')-> where ('ID','LIKE','%'.$search_text.'%')->paginate(100);
return view('search',['namelist'=>$namelist]);
}
elseif(isset($_GET['query'])){
$search_text1 = $_GET['query'];
$namelist = DB::table('namelist')-> where ('No','LIKE','%'.$search_text1.'%')->paginate(100);
return view('search',['namelist'=>$namelist1]);
}
else{
return view('search');
}
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\searchController;
use App\Http\Controllers\daftar;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
route::get('/search',[searchController::class, 'search'])->name('web.search');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Thank you
You have multiple ways to do that. In my opinion, the simplest way would be
DAFTAR.
Actually, what you have already done. Only with the Bladesyntax. And there is a small mistake in your example. Namely, your double quotes. <a href="search/".$nama[No]>DAFTAR</a> should be:
DAFTAR or better DAFTAR.
For the sake of completeness. the most elegant way would be to work with components.
Try this
<td>
DAFTAR
</td>
Enter the variable at given_variable_here above.
Also, you did not prepare the route to accept the passed variable in your web.php. This can be corrected like this:
Route::get('/search/{No}',[searchController::class, 'search'])->name('web.search');
Lastly, I'm not too sure about capitalizing the 'N' in the No you want to use. Should you have problems, start by placing these in lowercase. And if you're using VS Code make sure to add the extensions Laravel Extra Intellisense, Laravel Blade Snippets and Laravel Snippets. They are a great help. Let me know if this helps.
I have many to many relationship between users and products table and I set up a view (welcome.blade.php) that when I click on the product name, which is a link, it is supposed to redirect me to page where my delete form is so I can delete that particular product, but I get 404 not found page. I suspect that error is somewhere in my routes but I can't seem to find the problem. Also when I click on some product my url says project/destroy/1 which I think is good. Here is my code:
web.php:
Route::get('/home', 'HomeController#index')->name('home');
Route::post('/store', 'HomeController#store')->name('store');
Route::delete('/destroy/{$id}', 'HomeController#destroy')->name('destroy');
destroy.blade.php:
<div class="col-md-12">
<form action="destroy/{{ $product->id }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</div>
welcome.blade.php:
#if($products)
<table class="table">
<thead>
<th>#</th>
<th>Product Name</th>
<th>Owner Of The Product</th>
<th>Created At</th>
</thead>
<tbody>
#foreach ($products as $product)
<tr>
<td>{{ $product->id }}</td>
<td>
{{ $product->files }}
</td>
<td>
#foreach ($product->users as $user) {{ $user->name }}
#endforeach
</td>
<td>{{ date('M j, Y', strtotime($product->created_at)) }}</td>
</tr>
#endforeach
</tbody>
</table>
#endif
HomeController.php:
<?php
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class HomeController extends Controller
{
public function destroy(Product $product)
{
$product->users()->detach();
$product->delete();
return view('destroy');
}
}
You defined the following route in your file.
Route::delete('/destroy/{$id}', 'HomeController#destroy')->name('destroy');
This creates a DELETE route. DELETE routes are meant to be accessed by APIs. Links in anchor tags use GET routes. Clicking on
{{ $product->files }}
makes the router try and match GET /destroy/{$id} which is not defined, so it throws either an exception if debug is on or a 404 if not. You can see this in action by looking at the network tab of your browser's developer console.
Unless you add another GET route, you'll keep getting 404s.
Route::get('/destroy/{$id}', 'HomeController#destroy')->name('product.destroy-form');
will make the following link work.
{{ $product->files }}
Also, in the destroy method you're returning the view without passing any variables but in destroy.blade.php you seem to be using $product. Don't forget to add it!
You are nearly there, you just have a step too many.
Good call on the form and using the delete method, this is absolutely what you are supposed to do
However where you are going wrong is in using the link to go to a separate page with the form on to delete. You are better using a little javascript so the link submits a hidden form from the link.
<a onclick="document.getElementById('delete-form-{{$product->id}}').submit();}">{{ $product->files }}</a>
<form id="delete-form-{{$product->id}}" action="{{ route('destroy', ['id' => $product->id]) }}" method="POST" style="display: none;">
#csrf
#method('delete')
</form>
You can do the method you have but you need an additional route::get request to a page that loads the form and then you can submit the form on that page to delete.
I think it is because to delete a product using a new page the first thing to do is navigate to that page which you are not doing. You are instead going directly to the delete function using this
Route::delete('/destroy/{$id}', 'HomeController#destroy')->name('destroy');
You need to define a route that lets you go to that page first which u can do by creating a route like this:
Route::get('/delete/{$id}', 'HomeController#delete')->name('delete');
You should create a new function delete in the HomeController that just returns the destroy.blade.php. Something like this.
$product= Product::find($id);
return view('destroy')->with('product', $product);
where $id is the product id and Product is the model that you are using.
I am having a problem with variables devielve vue.js me ... I explain better
<tr
#if(#{{users.id}} != 1) // this is the error
Class="danger"
#else
class="success"
#endif
>
I can not define a #if (# {{user.id in laravel
before defined
#foreach($datos as $dato)
<tbody id={{$dato->id}}>
<tr
#if($dato->id !=1)
class="danger"
#else
Class="success"
#endif >
and it worked but I had to put a select box, and Use varaible vue.js to collect and return the data I...
excuse my English is not my mother tongue and hinders me
I do not believe you can use vuejs code (such as your #{{users.id}}) inside a laravel #if() because everything inside the #if() is being treated as PHP code. If it makes sense in your particular application, make use of vuejs's v-if implementation, such as <div v-if="users.id !== 1"> INSERT WHATEVER HERE </div>
So I have a page setup with a table of items. In each row there is a link. When the link is clicked I want to pass the ID of them through to the controller so I can pass it on to another view. Although I can't figure out how to do this.
This is the code in my item view
#foreach($items as $item)
<tr>
<td>{{$item->title}}</td>
<td>{{$item->genre}}</td>
<td>{{$item->description}}</td>
<td>View</td>
</tr>
#endforeach
As you can see there is a link which leads me to the rent view. In the controller this is all I have.
public function rent()
{
return view('rent');
}
Any help would be appreciated thanks.
I'd probably do it something like this.
#foreach($items as $item)
<tr>
<td>{{$item->title}}</td>
<td>{{$item->genre}}</td>
<td>{{$item->description}}</td>
<td>View</td>
</tr>
#endforeach
And then inside your controller you can accept the the value you are passing.
public function rent($value)
{
return View::make('new-view')->with('value', $value);
}
and then inside your new-view.blade.php
<p> The value I passed is: {{ $value }} </p>
Read more about Laravel url helpers here https://laravel.com/docs/5.1/helpers#urls
You can use route() helper:
#foreach($items as $item)
<tr>
<td>{{ $item->title }}</td>
<td>{{ $item->genre }</td>
<td>{{ $item->description }}</td>
<td>View</td>
</tr>
#endforeach
As alternative you can use link to action:
{{ action('RentController#profile', ['id' => $item->someId]); }}
And sometimes it's useful to use url() helper:
{{ echo url('rent', [$item->someId]) }}
More on these helpes here.
If you're using Laravel 5 with Laravel Collective installed or you're on Laravel 4, you can use these constructions to generate URLs with parameters.
PS: If you're using tables for layout, don't do it. You should learn DIVs, because it's really easy now to build cool layout with DIVs using Bootstrap framework which is built-in Laravel.
I have a loop foreach in a blade template where i print data from a specific model, the problem is I am not able of getting the value of "$pedido->proveedor()->first()->name" in the code giveing me this error "ErrorException (E_UNKNOWN) Trying to get property of non-object (View: C:..":
#foreach($pedidos as $pedido)
<tr>
<td>
{{ $pedido->id }}
</td>
<td>
{{ $pedido->proveedor()->first()->name }}
</td>
<td>
{{ date('d/m/Y', $pedido->fecha) }}
</td>
<td>
<a onclick="return confirm('deseas borar este registro?')" class="btn btn-danger btn-xs fullButton">Borrar</a>
</td>
</tr>
#endforeach
The weird thing here is when I code this "$pedido->proveedor()->first()" in side the loop of the template I get an object like this:
{"name":"nombre","domicilio":"domicilio","cp":"46006","poblacion":"poblacion","ciudad":"ciudad","pais":"pais"}
but coding this "$pedido->proveedor()->first()->name" I get the error:
the data is sent from a controller:
public function listPedidos()
{
$pedidos = Pedido::all();
// this next pice of code shows me i can get the name as spected but only from php
// foreach($pedidos as $pedido){
// ddd($pedido->proveedor()->first()->name);exit;
// }
return View::make('pedidos/pedidos-list')->with('pedidos', $pedidos);
}
Another weirder thing is that I have the same code with different model and it is working.
Thanks in advance for any help. ;)
You should use:
{{ $pedido->proveedor->name }}
Problem solved:
the previos answer was ok. I can use:
{{ $pedido->proveedor()->first()->name }}
or
{{ $pedido->proveedor->name }}
but because in one of the way of the loop there was not content to refer to i.e($pedido->proveedor didn't exixt) I use this:
{{ isset($pedido->Proveedor->name )?$pedido->Proveedor->name :''; }}