Php 我认为情况
在我看来,我还有一个问题。 如果不起作用,则此条件Php 我认为情况,php,laravel,laravel-5,laravel-5.6,Php,Laravel,Laravel 5,Laravel 5.6,在我看来,我还有一个问题。 如果不起作用,则此条件: @if( ! empty($news)) <div class="container"> <div class="row justify-content-center"> <h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
:
@if( ! empty($news))
<div class="container">
<div class="row justify-content-center">
<h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
</div>
</div>
@else
<h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Last News :</h1>
<div class="col-md-8">
<div class="row">
@foreach($news as $new)
<div class="card" style="width: 18rem; margin-left:30px;">
<img class="card-img-top img-responsive" style="height:160px;" src="img/{{$new['picture']}}.jpg" alt="Card-news-{{$new['id']}}">
<div class="card-body">
<h5 class="card-title">{{$new['title']}}</h5>
<p class="card-text">
@if(strlen($new['content'])>150)
{{substr(strip_tags($new['content']),0,150)}}...
@else
{{$new['content']}}
@endif
</p>
<a href="#" class="btn btn-primary">More.</a>
@if(@admin)
<a href="{{action('NewsController@edit', $new['id'])}}" class="btn btn-warning">Edit</a></td>
<form action="{{action('NewsController@destroy', $new['id'])}}" method="post">
@csrf
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
@endif
</div>
</div>
@endforeach
</div>
</div>
@endif
控制器:
<?php
namespace App\Http\Controllers;
use App\News;
use Illuminate\Http\Request;
class NewsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$news=News::all();
return view('home',compact('news'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('news.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$news = new News();
$news->title=$request->get('title');
$news->content=$request->get('content');
$news->picture=$request->get('picture');
$news->save();
return redirect('news')->with('success', 'Les Informations ont bien été ajoutées.');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$news = News::findOrFail($id);
return view('news');
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$news = News::find($id);
return view('news.edit',compact('news','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)
{
$news= News::find($id);
$news->title=$request->get('title');
$news->content=$request->get('content');
$news->picture=$request->get('picture');
$news->save();
return redirect('news');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$news = News::find($id);
$news->delete();
return redirect('news')->with('success','La News à bien été supprimée.');
}
}
您可以尝试:
@if($news->count())
//enter your code here if you have news
<h1 style="color:#fff;text-decoration:underline;margin-left:25px;">Last News :</h1>
<div class="col-md-12">
<div class="row">
@foreach($news->sortByDesc('created_at') as $new)
<div class="card" style="width: 18rem; margin: 10px 0 10px 20px;">
<img class="card-img-top img-responsive" style="height:160px;" src="img/{{$new['picture']}}.jpg" alt="Card-news-{{$new['id']}}">
<div class="card-body">
<h5 class="card-title">{{$new['title']}}</h5>
<p class="card-text">
@if(strlen($new['content'])>150)
{{substr(strip_tags($new['content']),0,150)}}...
@else
{{$new['content']}}
@endif
</p>
<a href="#" class="btn btn-primary">More.</a>
@admin
<a href="{{action('NewsController@edit', $new['id'])}}" class="btn btn-warning">Editer</a>
<form action="{{action('NewsController@destroy', $new['id'])}}" method="post">
@csrf
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Supprimer</button>
</form>
@endadmin
</div>
</div>
@endforeach
</div>
</div>
@else
//enter your code here if you don't have news
{{--<div class="col-md-12">--}}
{{--<div class="row">--}}
<h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
{{--</div>--}}
{{--</div>--}}
@endif
@if($news->count())
//如果您有新闻,请在此处输入您的代码
最新消息:
@foreach($news->sortByDesc('created_at')作为$new)
{{$new['title']}
@如果(strlen($new['content'])>150)
{{substr(strip_标签($new['content']),0150}}。。。
@否则
{{$new['content']}
@恩迪夫
@管理员
@csrf
供给者
@终端管理员
@endforeach
@否则
//如果您没有新闻,请在此处输入您的代码
{{----}}
{{----}}
任何消息!
{{----}}
{{----}}
@恩迪夫
我希望它适用于您您的变量<代码>$news
是一个数组?您是否看到任何错误?我没有任何错误,并且我的变量$news在我的NewsControllera中包含DB信息。我可以看到,只有当变量<代码>$news
为空时,才会执行else部分。尝试循环$news
变量的位置。您应该将@if(!empty($news))
更改为@if(empty($news))
我已经测试了@if($news),我也有同样的问题
@extends('layouts.app')
@section('content')
@if(empty($news))
{{--<div class="col-md-12">--}}
{{--<div class="row">--}}
<h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
{{--</div>--}}
{{--</div>--}}
@else
<h1 style="color:#fff;text-decoration:underline;margin-left:25px;">Last News :</h1>
<div class="col-md-12">
<div class="row">
@foreach($news->sortByDesc('created_at') as $new)
<div class="card" style="width: 18rem; margin: 10px 0 10px 20px;">
<img class="card-img-top img-responsive" style="height:160px;" src="img/{{$new['picture']}}.jpg" alt="Card-news-{{$new['id']}}">
<div class="card-body">
<h5 class="card-title">{{$new['title']}}</h5>
<p class="card-text">
@if(strlen($new['content'])>150)
{{substr(strip_tags($new['content']),0,150)}}...
@else
{{$new['content']}}
@endif
</p>
<a href="#" class="btn btn-primary">More.</a>
@admin
<a href="{{action('NewsController@edit', $new['id'])}}" class="btn btn-warning">Editer</a>
<form action="{{action('NewsController@destroy', $new['id'])}}" method="post">
@csrf
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Supprimer</button>
</form>
@endadmin
</div>
</div>
@endforeach
</div>
</div>
@endif
@endsection
<?php
/*
* Route Resource
*/
Route::resource('news', 'NewsController');
/*Route HOME*/
Auth::routes();
Route::get('/', 'HomeController@index')->name('home');
Route::get('/news', 'NewsController@index')->name('news');
@if($news->count())
//enter your code here if you have news
<h1 style="color:#fff;text-decoration:underline;margin-left:25px;">Last News :</h1>
<div class="col-md-12">
<div class="row">
@foreach($news->sortByDesc('created_at') as $new)
<div class="card" style="width: 18rem; margin: 10px 0 10px 20px;">
<img class="card-img-top img-responsive" style="height:160px;" src="img/{{$new['picture']}}.jpg" alt="Card-news-{{$new['id']}}">
<div class="card-body">
<h5 class="card-title">{{$new['title']}}</h5>
<p class="card-text">
@if(strlen($new['content'])>150)
{{substr(strip_tags($new['content']),0,150)}}...
@else
{{$new['content']}}
@endif
</p>
<a href="#" class="btn btn-primary">More.</a>
@admin
<a href="{{action('NewsController@edit', $new['id'])}}" class="btn btn-warning">Editer</a>
<form action="{{action('NewsController@destroy', $new['id'])}}" method="post">
@csrf
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Supprimer</button>
</form>
@endadmin
</div>
</div>
@endforeach
</div>
</div>
@else
//enter your code here if you don't have news
{{--<div class="col-md-12">--}}
{{--<div class="row">--}}
<h1 style="color:#fff;text-decoration:underline;margin-left:20px;">Any News !</h1>
{{--</div>--}}
{{--</div>--}}
@endif